-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
  • 60,010
  • 15
  • 145
  • 220
Kumar Kailash
  • 1,385
  • 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,528
  • 3
  • 41
  • 73