wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparing Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparing Strings" Watch "comparing Strings" New topic
Author

comparing Strings

Yijun Xie
Greenhorn

Joined: Apr 22, 2002
Posts: 14
Hi everybody,
the following code
class Test{
public static void main(String [] args){
String str1="java";
String str2=str1+"";
String str3="ja";
String str4=str3+"va";
if(str1==str2)
System.out.println("str1==str2");
else
System.out.println("str1!=str2");
if(str3==str4)
System.out.println("str3==str4");
else
System.out.println("str3!=str4");
}
}
prints
str1==str2
str3!=str4
i don't know why,could u please explain this for me?
by the way what is the difference between run-time
and compile time?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Engine, Welcome to JavaRanch.
Please read our name policy and change your name properly.
Run-time: during the execution of the program. Here Exceptions and other Throwable objects can be thrown by the running program.
Compile-time: The compiler is doing his work. Compiler errors are possible.

"java" is equal to "java" + ""
"ja" is not equal to "java"


SCJP2. Please Indent your code using UBB Code
alex earnshaw
Ranch Hand

Joined: Nov 05, 2001
Posts: 60
Originally posted by Engine:

String str1="java";
String str2=str1+"";

Here str1 and str2 both contain "java", the JVM sees that the strings are the same so it does not create a new String object for str2, but just sets str2 equal to the same reference as str1 and therefore str1==str2 is true.
Originally posted by Engine:

String str3="ja";
String str4=str3+"va";

Here str3 contains "ja" and str4 contains "java", the JVM creates two different String objects, str3 references one, and str4 references the other. So the references are not equal so str3!=str4 is true.
BTW if you want to test the contents of String objects for equality you need to use the equals method.
Example: str1.equals(str2);
hope that helps
Alex
[ April 23, 2002: Message edited by: alex earnshaw ]
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
*****Your code*******
String str1="java";
String str2=str1+"";
String str3="ja";
String str4=str3+"va";
**********************
str1 = "java"
str2 = str1+""="java"+""="java"
str3 = "ja"
str4 = str3+"va"="ja"+"va"="java"
(str1==str2) is true("java"=="java")
(str3==str4) is false("ja"=="java")
When you compile code- this is compile time
When you run the compiled code-this is runtime
That's all.
Jamal
Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
What my understanding is ,the String = "java" and the rest are all string literals. These will be written out to String literal pool. If str1 is assigned "java" then java will be written to the pool and suppose str2=str1+"" this again yields "java". Now JVM looks in the string pool and sees if "java" is existing , if so (which is the case here)then str2 will point to the same address in the pool. Thats why (str1==str2) yields true.
suppose if it was a string object like
String str1=new String("java");
String str2=new String("java");
if(str1==str2) this will yield false as new string object is created each time and the address are different.
Please let me know if iam wrong.
Thanks


Raj<br />Sun Certified Java Programmer
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That's correct. If you're still having difficulty with this concept, do a search in this forum. This topic has been covered and recovered countless times.
Best of luck,
Corey


SCJP Tipline, etc.
Mahesh Mamani
Ranch Hand

Joined: Jun 25, 2001
Posts: 110
Hello,
A slight confusion here...str1 is NOT equal to str2 as stated in the mails....
Have tested out this program
public class test{
public static void main(String kk[]){
String str1 = new String("java");
String str2 = new String("java");
String str3="java";
String str4= str3+"";
String str5 = "java";
String str6 = str5;

if(str1 == str2)
System.out.println("STR1==STR2");
else
System.out.println("STR1!=STR2");
if(str3 == str4)
System.out.println("STR3==STR4");
else
System.out.println("STR3!=STR4");
if(str5 == str6)
System.out.println("STR5==STR6");
else
System.out.println("STR5!=STR6");
}
}

OUTPUT:
STR1!=STR2
STR3!=STR4
STR5==STR6
Pls have a loook...

Mahesh
Yijun Xie
Greenhorn

Joined: Apr 22, 2002
Posts: 14
i am sorry that there is something wrong with my previous code
class Test{
public static void main(String [] args){
String str1="java";
String str2=str1+"";
String str3="ja";
String str4=str3+"va";
if(str1==str2)
System.out.println("str1==str2");
else
System.out.println("str1!=str2");
if(str1==str4)
System.out.println("str1==str4");
else
System.out.println("str1!=str4");
}
}
the output is:
str1==str2
str1!=str4
how to explain this?
thanks
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
My output is
str1!=str4
str1!=str2
(Using Jdk1.3.1)
What JDK Version are u using?
Clement


best regards,<br />Steven<br />SCJP, SCEA
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Hi engine,
what u r not able to understand is str1!=str4
now str1 is "java"
str4 is str3+"va",which is "ja"+"va"="java"
but the computation of str4 is done at runtime,and hence a new reference is created for it,where as computation for str1 is done at compile time.
== operator compares the references,since the references are not the same,so str1!=str4
I hope that clears it up.
For further details,refer to JLS
Gautam
Yijun Xie
Greenhorn

Joined: Apr 22, 2002
Posts: 14
hi,Clement Ng
i am using jdk1.2.2,perhaps this accounts for the difference.i'll download jdk1.3.3 later.thanks a lot!
Yijun Xie
Greenhorn

Joined: Apr 22, 2002
Posts: 14
Yes, Clement NG
in jdk1.3.1 the output is different.
what w'll happen in jdk1.4.x?
i try to download it, but it is huge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing Strings
 
Similar Threads
doubt about equals vs ==
String == operator
Can anyone explain what is == and equal( ) ?
equals versus ==
regex help