• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want the difference between String and StringBuffer

 
sharon daze
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic