1) String s = "text"; This syntax will allocate memory for the "text" on the heap. and each time you assign this “text” to another variable, it will return the same memory reference every time. for Exp -
String aa = "text";
String bb = "text";
if(aa == bb){
System.out.println("yes");
} else {
System.out.println("No");
}
will print - Yes
but
Line s = new line ("text"); Always create a new memory location and return a new link each time. for Exp -
String aa = new String ("text");
String bb = new String ("text");
if(aa == bb){
System.out.println("yes");
} else {
System.out.println("No");
}
will print - No
source
share