I was expecting output true but i am getting output as a false Can any one explain me this?
String st = "mah";
String st1 = "mah";
String test = st + st1;
String test1 = st + st1;
System.out.println(test == test1);
I was expecting output true but i am getting output as a false Can any one explain me this?
String st = "mah";
String st1 = "mah";
String test = st + st1;
String test1 = st + st1;
System.out.println(test == test1);
If the strings you're joining aren't compile time constants, you can't avoid creating a new string, because of String's immutability. try below-You will get true.
final String st = "mah";
final String st1 = "mah";
String test = st + st1;
String test1 = st + st1;
System.out.println(test == test1);