Win a copy of Svelte and Sapper in Action this week in the JavaScript 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Strings

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Naveen Arumugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Bookmark Topic Watch Topic
  • New Topic