aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Want the difference between String and StringBuffer 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 "Want the difference between String and StringBuffer" Watch "Want the difference between String and StringBuffer" New topic
Author

Want the difference between String and StringBuffer

sharon daze
Greenhorn

Joined: Feb 08, 2009
Posts: 16
Hello all ,

String s = "abc";
String s2 = new String("abc");

if(s==s2) // this is true

then

String s = "abc";
String s2 = new StringBuffer("abc").toString();
if(s==s2) // this condition will be false

What is the difference between this two codes.




http://myjvm.wordpress.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

String s = "abc";
String s2 = new String("abc");

if(s==s2) // this is true



Really?!?!? This is true? .... I don't think this is true.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

String s = "abc";
String s2 = new StringBuffer("abc").toString();
if(s==s2) // this condition will be false


This is false because the toString() method of the StringBuffer class will return a new String.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Want the difference between String and StringBuffer