-1

Can someone explain me on how String class behaves in memory management in java. I have lately heard about string comparison. how does two string with == operator and equals method differ.

example: String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello");

any suggestions on case 1 AND case3?

user2864740
  • 57,407
  • 13
  • 129
  • 202
Kumar Kailash
  • 1,327
  • 1
  • 10
  • 19

1 Answers1

3

== in Java compares the references of the 2 string objects, and not the contents. The equals methods does is the one that checks the content.

However, due due string interning, I believe that in the case that you listed, str1 == str2 withh be true because there is a single instance of that string literal stored in memory.

David Pilkington
  • 13,297
  • 3
  • 39
  • 67