aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects" Watch "String Objects" New topic
Author

String Objects

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
class StringEqual
{
public static void main(String args[])
{
String s1="JavaServerPages";
String s2="JavaServer"+"Pages"; //Line 1

String s3="Pages";
String s4="JavaServer"+s3; //Line 2

if (s1==s2)
System.out.println ("s1 and s2 are equal");
else
System.out.println ("s1 and s2 are not equal");

if (s1==s4)
System.out.println ("s1 and s4 are equal");
else
System.out.println ("s1 and s4 are not equal");
}
}

output
s1 and s2 are equal
s1 and s4 are not equal

What does JVM do in line 1 and line. Could any one explain me


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi raghu

String s1="JavaServerPages";
String s2="JavaServer"+"Pages"; //Line 1

This is equal because it is determined at a compile time itself

String s3="Pages";
String s4="JavaServer"+s3; //Line 2

But in this case it is not because S4 is resolved only at run time.
In Runtime whenever a operation is performed on Strings ,it returns new String Object it doesnot refer to old one.

but if u declare s3 as final it will be true

final String s3="Pages";
String s4="JavaServer"+s3;

This is equal because S3 is final and treated as constant.So S4 determined at a compile time itself
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Thanks vidya? any more suggestions
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi vidyasagar
can u point out how mnany objects were created
for me its

JavaServerPages
Pages
JavaServer

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
HI Paramesh

Totally 4 created...

At compile time

JavaServerPages
Pages
JavaServer

At Run time

JavaServerPages
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Objects