File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
Dear All,
I found it confusing with the following code.I have commented the results i expected, but its giving "false" for all comparisons[just i'm checking only for references]
Can anyone help me, please?
-------------------------My Assumptions--------------------------
Note: Case# = c#
In c1, s1 creates a new string object & s2, while trying to create a new string object, checks for the same literal & if its present should use the same.So, i opted "true".Also, seeing the result, "false", I assumed that by Java specifications definition , whenever a string is created using "=" a new String is created.Am i right?
In c2, the answer should be "true', but, its "false".lllrly for c6.
Request clarification
-------------------------My Assumptions Ends---------------------
class A1
{
public static void main(String arf[])
{
String s1 = "A";
String s2 = "A";
String s3 = "C";
String s4 = s3;
String s5 = "D";
String s6 = new String("D");
String s7 = new String("E");
String s8 = new String("E");
String s9 = new String("F");
String s10 = "F";
String s11 = new String("H");
String s12 = s11;
/*c1*/ System.out.println("s1 == s2 ==> " + s1 == s2);//true
/*c2*/ System.out.println("s3 == s4 ==> " + s3 == s4);//true
/*c3*/ System.out.println("s5 == s6 ==> " + s5 == s6);//false
/*c4*/ System.out.println("s7 == s8 ==> " + s7 == s8);//false
/*c5*/ System.out.println("s9 == s10 ==> " + s9 == 10);//false
/*c6*/ System.out.println("s11 == s12 ==> " + s11 == s12);//true
}//EoMain
}//EoA1
Thanks & Rgds,
ANaveenS
------------------
ANaveenS


ANaveenS
Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
Dear All,
I found the answer, the problem is with operator precedence
ie., "+" has got more precedence than "==" , so 'message string + string1[used for comparison] gets into one string which is then compared to the string2, so evaluates to false. If we introduce a parenthesis, then the problem is solved.

_CODE__________
class BBBBB1{

public static void main(String arf[]){

String s1 = "A";
String s2 = "A";
String s3 = "C";
String s4 = s3;
String s5 = "D";
String s6 = new String("D");
String s7 = new String("E");
String s8 = new String("E");
String s9 = new String("F");
String s10 = "F";
String s11 = new String("H");
String s12 = s11;
String s13,s14;
s13 = new String("H");
s14 = s13;
System.out.println("s1 == s2 ==> " + (s1 == s2)); //true
System.out.println("s3 == s4 ==> " + (s3 == s4)); //true
System.out.println("s5 == s6 ==> " + (s5 == s6)); //false
System.out.println("s7 == s8 ==> " + (s7 == s8)); //false
System.out.println("s9 == s10 ==> " + (s9 == s10));//false
System.out.println("s11 == s12 ==> " + (s11 == s12));//true
System.out.println("s11 == s12 ==> " + (s13 == s14));//true
}
}

Thanks
ANaveenS
Martha Yeh
Greenhorn

Joined: Jan 31, 2001
Posts: 18
hi there..
i tried to run ur code..
***************************
class A0324
{
public static void main(String args[])
{
String s1 = "A";
String s2 = "A";
String s3 = "C";
String s4 = s3;
String s5 = "D";
String s6 = new String("D");
String s7 = new String("E");
String s8 = new String("E");
String s9 = new String("F");
String s10 = "F";
String s11 = new String("H");
String s12 = s11;
/*c1*/ System.out.println("s1 == s2 ==> " + (s1 == s2));//true
/*c2*/ System.out.println("s3 == s4 ==> " + (s3 == s4));//true
/*c3*/ System.out.println("s5 == s6 ==> " + (s5 == s6));//false
/*c4*/ System.out.println("s7 == s8 ==> " + (s7 == s8));//false
/*c5*/ System.out.println("s9 == s10 ==> " +( s9 == s10));//false
/*c6*/ System.out.println("s11 == s12 ==> " + (s11 ==s12));//true
}//EoMain
}//EoA1
************************************
output:
s1==s2==>true
s3==s4==>true
s5==s6==>false
s7==s8==>false
s9==s10==>false
s11==s12==>true
*******************************************
String is immutable.. always remember that.
also, ur code got some error..
* main(String args[ ]) --> not arf[ ]
*System.out.println("s1==s2==>" + (s1==s2));
--> not just "s1==s2==>"+s1==s2
its look like: "s1==s2==>+s1"==s2 // always gets false
hope this help. good weekend.(try it~)
^__^*
martha
Martha Yeh
Greenhorn

Joined: Jan 31, 2001
Posts: 18
oops.. sorry about the mistake..
the arf[ ] work fine too
forgive me.. sleepy time here.. (12:41am)
good u got the point.. good weekend again.
martha
Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
Martha
Thanks for your valuable suggestions!
Actually, & Initially I did'nt look into the code properly & after a small trty , i got it. {Check my Expln}
Regarding your say "It should be args[] & not arf[]" : The variable name can be anytyhing , only the type should match[String []]
I wish you a excitiing weekend! Have Fun & Enjoy!
Regards,
ANaveenS
------------------
ANaveenS
 
 
subject: Strings