• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String code

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Anyone please justfy the results given by the last two println
statements

thanks in advance
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well...

i thing the s4 references the same memory position than s2.

The s4 comes from s2 without any modification, by the way, s2 is already in lower case, cauz' this, they references the same memory position. So, in this case, s4 = s2 and s4 = s2.toLowerCase() is the same thing.

If u want test, go and add this lines in your code

String s5=new String("CASE MATTERS NOW");
String s6=s5.toUpperCase();
System.out.println(s6==s5);

the operator "==" returns true if the objects are identical, and equals returns true if the object are "equivalents" (remember: the operator equals can be overloading )
 
Leonardo Rafaeli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol, sorry, gramatical error:
overloaded, not overloading
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Niranjan,
1)In this statement "String s3=s1.toLowerCase();" a new string will be
created and s3 is refering to that string.
2)In this statement "String s4=s2.toLowerCase();" it wont create a new string, because already all the char's are in small case, and s4 is referening to s2.
3)In this statement "System.out.println(s2==s4);", s2 and s4 both are refering to the same string, it is true.
4)In this statement "System.out.println(s3==s4);", s3 is refering to a different string and s4 to another string, but the contents are same. So it is giving false. if u give s3.equals(s4) it will give true.
i think its clear, pls correct me if i am worng.
Regards,
Sri.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
Strings will store in string pool na
in case of String s3=s1.toLowerCase();
String s4=s2.toLowerCase();
s3 and s4 should reffer to same string "case matters"
then why it is giving s3==s4 false
plese explain me
thank you
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the given string is already in lower case, toLowerCase() method returns the reference of the same string object.
If the given string is not in lower case, then toLowerCase() method creates a 'new' string object with lowercase letters and it returns the reference of the new object.
String s1=new String("Case Matters");
String s2=new String("case matters");
String s3=s1.toLowerCase(); // here s1 is not in lower case so a new string
//object is created

String s4=s2.toLowerCase(); // here s2 is already in lowercase, so
// toLowerCase() method returns the reference of s2
// s2,s4 are aliases

System.out.println(s1==s3); // false , cos s1,s2 are two dif. object
System.out.println(s2==s4); // true, cos s2,s4 are aliases
System.out.println(s3==s4); // s3-new object, s4 nothing but s2 ,two
// objects

Hope this helps u.
Regards
Naresh
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hope this example will clear your doubts a bit more.
 
reply
    Bookmark Topic Watch Topic
  • New Topic