aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String code" Watch "String code" New topic
Author

String code

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277


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

thanks in advance


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Leonardo Rafaeli
Ranch Hand

Joined: Dec 12, 2005
Posts: 58
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 )


SCJP 1.4, SCWCD 1.4, SCBCD 5, OCUP, SCEA 5 (I), Oracle SOA Implementation Specialist, IBM Websphere Lombardi 7.1 Fundamentals
Blog: [url]http://www.leozin.com.br/blog[/url]
Leonardo Rafaeli
Ranch Hand

Joined: Dec 12, 2005
Posts: 58
lol, sorry, gramatical error:
overloaded, not overloading
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
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.
raghava yelamkonda
Greenhorn

Joined: Nov 28, 2005
Posts: 10
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
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
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
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59


Hope this example will clear your doubts a bit more.


Raghusham Sankargal
 
 
subject: String code