Skip to main content
3
Mark Rotteveel
  • 110.3k
  • 240
  • 160
  • 232
added 277 characters in body
Source Link
Chaipau
  • 381
  • 1
  • 7
  • 16

How does Java implement the below string comparisons

public class MyClass { public static void main(String args[]) { String a = "Chaipau"; String b = "pau"; System.out.println(a == "Chai"+"pau"); //true System.out.println(a == "Chai"+b); //false } } 

This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.

How does Java implement the below string comparisons

public class MyClass { public static void main(String args[]) { String a = "Chaipau"; String b = "pau"; System.out.println(a == "Chai"+"pau"); //true System.out.println(a == "Chai"+b); //false } } 

How does Java implement the below string comparisons

public class MyClass { public static void main(String args[]) { String a = "Chaipau"; String b = "pau"; System.out.println(a == "Chai"+"pau"); //true System.out.println(a == "Chai"+b); //false } } 

This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.

Post Closed as "Duplicate" by Alex Shesterov, Khemraj Sharma, luk2302, Thiyagu, Michael java
Source Link
Chaipau
  • 381
  • 1
  • 7
  • 16

Difference in the results of String comparison in Java?

How does Java implement the below string comparisons

public class MyClass { public static void main(String args[]) { String a = "Chaipau"; String b = "pau"; System.out.println(a == "Chai"+"pau"); //true System.out.println(a == "Chai"+b); //false } }