This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!