This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String handling

 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below:

String str1 = "Hello";
String str2 = "Hello";
if(str1 == str2)
System.out.println("str1 == str2 is true");

The output is:
str1 == str2 is true

Could you tell me why this is the output?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals -- i.e., occurrences in your source code of double-quoted Strings -- are pooled in the JVM. For each string literal, there will be one and only one instance of a String object used to represent it. This is described in JLS 3.10.5.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic