Square CSS

Showing posts with label java tutorials. Show all posts
Showing posts with label java tutorials. Show all posts

Monday, May 25, 2020

@EnableCaching : Maintain Cache In Spring


Introduction : 

We can enable cache in spring by the use of @EnableCaching and @Cachable.


Refer Video Tutorial For Detail :




@EnableCaching


The annotation @EnableCaching should be used on the class level to configure the class for enabling cache. This annotation should be used to make it configurable for cache maintenance.



  @EnableCaching //provided annotation on class level
  public class CachingDemo{
         //Custom Code
  }


@Cachable

The annotation @Cachable should be used above the methods which needs to maintain cache.
Once the request is complete the response is stored in cache for same request coming in future.
The response is mapped to the key for further requests. 


@Cacheable("userInfo") //provided cacheable annotation along with userInfo as key
public String checkCache() { //Custom Code }


Conclusion


Major annotation to be used for caching in spring is @EnableCaching and @Cachable. The key should be provide where the cached is used so that the cache data should be mapped to the key.

Monday, October 8, 2018

StringBuilder in Java

Unlike String, StringBuilder is mutable in nature. The API of this class is compatible with StringBuffer except that the StringBuilder is unsafe in a multithreaded environment. It is not synchronized so should it should not be used where thread safety is of prime concern.


It has two important methods append() and insert().

The append("x") function appends "x" at the end of existing StringBuilder.
The insert(5, "x") function inserts the string "x" at the provided index of existing StringBuilder. In this case, it is 5.


  
   StringBuilder sb=new StringBuilder("Initial");  //Initial
   sb.append("Appended");  //InitialAppended
   sb.insert(7, "Inserted");   //InitialInsertedAppended         



To convert a StringBuilder to String we use toString() method. 


  String str = sb.toString();

                                                              


☛ Next >> String vs StringBuffer vs StringBuilder
                     
Let's have some basic idea about StringStringBuffer and StringBuilder. ...

String in Java

A string is immutable in Java. String class represent character strings, i.e. string class is implemented with the char array. We can create a string in two ways :

  
  String str1 = "playjavatutorials";
  String str2 = new String("playjavatutorials"); 


When a string is created using double quotes it checks for the same value in the string pool. 
If the value already exists in the string pool then it returns the reference else creates it's object and then places it in the string pool. In this way, JVM saves lots of memory by using the same string at many places and threads.

When a string is created with the new operator then it explicitly creates a string in the heap memory.

The string is a final class with its all fields as final except for 'private int hash'.

String overrides equals() and hashCode() method to verify if given two strings are same or not. The equals() method is case sensitive so if case-sensitive checks are not required then equalsIgnoreCase() method should be used. 


☛ Next >> StringBuffer in Java
                      
A StringBuffer is like String but it can be modified because of its m ...

Saturday, October 6, 2018

Data Types In Java!

It specifies the type and size of a variable. They are grouped into 2 types :

1. Primitive data types: They are of type boolean, char, byte, short, int, float, long and double. 
2. Non-primitive data types: They are interfaces, classes, and arrays.

Types of primitive data types:

Data Type Default Size  Default Value
boolean 1 bit FALSE
byte 8 bit 0
char 2 byte '\u0000'
short 2 byte 0
int 4 byte 0
float 4 byte 0.0f
long  8 byte 0l
double 8 byte 0.0d

Note: char uses 2 bytes of java and it has \u0000 as its default value because it java uses Unicode system not the ASCII code system. Unicode system has \u0000 as the lowest range so it is assigned as the default value.

☛ Next >> String in Java
                      A string is immutable in Java. String class represents character str  ...

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts