• 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

string and final confusion

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "str";
String s2 = "ing";

String concat = s1 + s2 ; // i think it returns new object and it is not placed in string constant pool..just guessing dont know exact reason..

System.out.println(concat == "string"); //false



final String s1 = "str";
final String s2 = "ing";

String concat = s1 + s2 ; // it reurns new object and place in string constant pool..just a guess..

System.out.println(concat == "string"); //true.....can anyone please help me the reason why both are giving unexpected answer


 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This depends on the compiler and is at this point an unreliable comparison. If the compiler gets both references from the String pool it will return true, if it doesn't, it will return false. To compare their true value, you must use equals method. Using equals operator will return true for some compilers (in reality, probably all compilers), but it's not an obligation according to the specification and should thus never be relied upon.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:String s1 = "str";
String s2 = "ing";

String concat = s1 + s2 ; // i think it returns new object and it is not placed in string constant pool..just guessing dont know exact reason..

System.out.println(concat == "string"); //false



final String s1 = "str";
final String s2 = "ing";

String concat = s1 + s2 ; // it reurns new object and place in string constant pool..just a guess..

System.out.println(concat == "string"); //true.....can anyone please help me the reason why both are giving unexpected answer



First read this... https://coderanch.com/t/454384/java/java/compile-time-constant

The two points of that topic that are important are... one, the concatenation of two compile time constants is a compile time constant. So, when you concat two compile time constant variables, it creates a compile time constant, which in turn, if it is used, the compiler will place it into the string pool.

And two, there are requirements that determine what is a compile time constant variable. In the first example, those requirements are not met. In the second example, those requirements are met.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:
final String s1 = "str";
final String s2 = "ing";

String concat = s1 + s2 ; // it reurns new object and place in string constant pool..just a guess..

System.out.println(concat == "string"); //true.....can anyone please help me the reason why both are giving unexpected answer



Interestingly, no. Or perhaps ... almost.

s1, s2, "str", "ing", and "string" are all compile time constants. The compiler knows what every one of these values are at compile time. And hence, it is like the code you had were ...



So, yes, you can can argue that an object is created and placed in the string pool... but the object is not created from the concatenation operation.

Also note that the "str" and "ing" objects are *not* created at all !!

Henry
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..now i got...
1>"str" and "ing" are compile time constant literal....
2>String s1,s2 are not compile time constant variable..
3>and final string s1,s2 are compile time constant variable
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Also note that the "str" and "ing" objects are *not* created at all !!


Are you sure about that ? The variables s1 and s2 still exist, so surely they are pointing at Strings "str" and "ing" respectively.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Henry Wong wrote:Also note that the "str" and "ing" objects are *not* created at all !!


Are you sure about that ? The variables s1 and s2 still exist, so surely they are pointing at Strings "str" and "ing" respectively.



Correct. The variables exist -- but interestingly, the fields don't exist. And if the variables were directly used, the objects would exist too.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic