What is the difference between these two following statements?
String s = \"text\";
String s = new String(\"text\");
One simple way to understand the difference is below:-
String s ="abc";
String s1= "abc";
String s2=new String("abc");
if(s==s1){
System.out.println("s==s1 is true");
}else{
System.out.println("s==s1 is false");
}
if(s==s2){
System.out.println("s==s2 is true");
}else{
System.out.println("s==s2 is false");
}
output is
s==s1 is true
s==s2 is false
Thus new String() will always create a new instance.