This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

About string again  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
public class tstring2 {
public static void main(String args[]){
String st1 = "java";
String st2 = "java";
String st3 = "va";
String st4 = "ja";
String st5 = st4 + st3;
System.out.println(st1=="ja" + "va"); //true
System.out.println(st1==st2 ); //true
System.out.println(st1==st5); //false
System.out.println(st1.equals(st5)); //true
System.out.println(st1==st4 + st3); //false
System.out.println(st1.equals((st4 + st3))); //true
}
}
can any one tell me why the out puts look like (what I have mentioned in comments)and why the out put differs?
--thiru
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Literals are created in the Constant Pool and are resolved at compile time and created at class load time. They are also "re-used".
st1 points to the "JAVA" literal.
"JA" + "VA" points to the SAME literal. The References are the same.
st1 points to "JAVA".
st2 points to the same constant in the pool
st3 points to "VA"
st4 points to a different literal "JA"

The system does not resolve the concatenating of the two variable references in st4 and st3 until runtime, so it does not use the Constant Pool because that has already been set in stone and the system does not KNOW that an appropriate literal will exist there. So it makes another string instead.
(Nice to get to know the REAL you!)
 
thiru chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It might look silly...But I want to know, even if it gets resolved in run time, the value of both the string are same.So why it is still printing 'false' instead true.
Is it comparing whether two string variables are pointing to same value or it is comparing the real value of both the strings variable?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please is the question correct, because this question and the answers i am seeing looks opposite to the knowledge of the string i have.
i mean the use of (==) and (equals)
kaffo
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dear

Originally posted by thiru chakravarthi:
Hi,
It might look silly...But I want to know, even if it gets resolved in run time, the value of both the string are same.So why it is still printing 'false' instead true.
Is it comparing whether two string variables are pointing to same value or it is comparing the real value of both the strings variable?


the things is like this you are not comparing by means of "==" the value but the location where it gets stored,so st1 is pointing towards some different location & st5 or st4+st3 are pointing towards some other location.so the result is false.
But when you used the equal method you are comparing actual value so the result is true.
Love
Lokesh
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!