Forums Register Login

comparing Strings

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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"
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
*****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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi,
My output is
str1!=str4
str1!=str2
(Using Jdk1.3.1)
What JDK Version are u using?
Clement
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1247 times.
Similar Threads
String == operator
doubt about equals vs ==
Can anyone explain what is == and equal( ) ?
regex help
equals versus ==
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:49:50.