String is a reference data type and hence string literals are treated as object and get stored in the string constant pool. We must have to define any string within double-quote otherwise it will give compilation error. String can be represented in two form either with using ' new ' operator or without using new operator.
Two way of string representation in java :
1. Without using new operator
2. With using new operator.
Without using new operator
String can be represented without using new operator that means we are directly assigning string value to its reference. If we are defining like this then first it will search for the string literal whether it is present in the String pool or not. If it is present then simply it returns the reference of the existing object to the current reference. For ex-
public class A {
public static void main(String[] args) {
String s = "java";
String s1= "java";
System.out.println(s==s1); // true
System.out.println(s.hashCode()==s1.hashCode()); // true
}
}
In the above program with the first statement i.s String s="java", it will search in the string pool whether that string literal is present or not. If it is present in the pool then it will return the reference to the reference variable 's' which also starts pointing to that object.
With the second statement again it will start searching in the string pool , if present then return the reference variable and if not then create the object in the pool and then return the reference.
Important points regarding Creating string without new operator
1. First search in the string pool whether string literal is present or not.
2. If present in the pool then return the reference.
3. If not present in the pool then crate and object and return the reference of that object.
With using new operator
With the help of new operator it is definitely going to create an object. First it will search in the string pool that whether that string literal is present or not , if not present then it will create an object in the pool itself and if present then it will create an object in the heap memory and that memory will hold the reference of the already present literal in the pool. So with the above statement its confirm that with the use of new operator it is going to create an object but in heap or in pool it all depends upon the string literal's presence in the pool.
For ex-
public class A {
public static void main(String[] args) {
String s = new String("java");
String s1= new String("java");
System.out.println(s==s1); // true
System.out.println(s.hashCode()==s1.hashCode()); // true
}
}
In the above for the first statement it will search in the pool whether string literal " java " is present in the pool or not , if not then create the object in the pool itself and if present in the heap memory and return the reference of the already stored object to the reference variable " s ".
For the second its the string literal " java " is surely present in the pool so JVM will create object in the object in the heap memory and return the reference to that object and that object reference value is returned to the " s1 " reference variable.
Example with and without new operator
public class A {
public static void main(String[] args) {
String s="java"; //without new operator
String s1="java";
String s2 = new String("java"); // with new operator
String s3= new String("java"); // with new operator
System.out.println(s==s1); // true
System.out.println(s.hashCode()==s1.hashCode()); // true
}
}
From the above example:
String s = "java" ---> Create object in the String pool ( if not present )
String s="java"---> Return the reference of the object present in the pool .
String s1= new String("java") --> Create the String object in the heap.
String s2=new String("java")--> Create the String object in the heap.
Diagram showing how memory is assigned in the heap and string pool.
In the above diagram we can see that s2 and s3 contain the reference of the object created in the heap memory and which itself contains the reference of the string pool object.
But in case of without using new operator, always reference value is returned and it is not going to create any object in the heap memory. This is the major difference between the string object with and without using new operator.
0 comments:
Post a Comment