• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final's Puzzle

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final String str1 = "1";
final String str2 = "1";
System.out.println(str1==str2);

Result is true.

Why? final variable is a clone in stack. So str1 != str2 ?
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this has something to do with Strings being immutable.

Like when you declare a String, str1 = "1";, a String object "1" will be placed in the String pool on the heap.

When you declare another with the same string value, str2 = "1";, str2 will refer to the same String object, "1", in the String pool. That's why it's equal.

[ June 25, 2008: Message edited by: Denise Saulon ]

[ June 25, 2008: Message edited by: Denise Saulon ]
[ June 25, 2008: Message edited by: Denise Saulon ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does not have anything to do with 'final'. If you would remove the 'final', you would see that the result is also 'true'.

String literals, such as the "1" in your example, are managed in an internal string pool. If you use the same string literal in your source code more than once (as you have "1" two times), then Java is smart enough to make only one String object, and re-use that object.

So your str1 and str2 both refer to the same String object. The == operator returns true when two variables refer to the same object.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to what Jesper has told, they are treated as compile time constants as the contents of Strings are well determined during the compile time itself. That's where there are being a part of String Literal pool referring the single String object.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic