This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes internal doubt abou string and string buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "internal doubt abou string and string buffer" Watch "internal doubt abou string and string buffer" New topic

internal doubt abou string and string buffer

partha sarathy

Joined: Jun 19, 2000
Posts: 15
Dear friend,
String and stringBuffer have a diffrence.
-> they say if you declare a Object of String class then the contents of it can not be changed.But that is not the case in StringBuffer.My doubt is that I want a practical example to show that when i declare a string object the contents of it is unchanged and that if i declare a String buffer object its contents can be changed. PLease be brief in giving a solution.
awaiting a reply from any intellectual
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi parth,
this code probably will help u.

public class Str
public static void main(String j[])
String s = new String("hello");
StringBuffer sb = new StringBuffer("Hello");

String t;
StringBuffer t1;

t = s.concat(" parth"); // will make a new string object
t1 = sb.append(" parth"); // will append parth to buffer

System.out.println("string refereces"+ (s==t));[b]//prints false

System.out.println("buffer refereces"+sb==t1));//prints true

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
please check out this example that u wanted. Basically i am creating two string objects a and b first. the test a==b is true saying both are pointing to the same object. i am replacing a character in a. A new object is created as Strings are immutable. thus a==b test fails now as the two objects are not the same now.
this is not the same with StringBuffer. even though i have appended a character to ab this is reflected in bb also. and the objects pointed to by ab and bb still remain the same.
class demo
public static void main(String[] args)
// both a and b in the code below point to the same object
String a=new String("hello");
String b=a;
System.out.println(" a and b are equal");}
// a now points to a different object
System.out.println("a is"+a);
System.out.println("b is"+b);

// a is not equal to b as a new object is created

// case of StringBuffer
StringBuffer ab=new StringBuffer("hello");
StringBuffer bb=ab;
// ab still points to the same object
System.out.println(ab);// ab contains helloc
System.out.println(bb);// bb also contains helloc

// ab equal to bb as no new object is created

Consider Paul's rocket mass heater.
subject: internal doubt abou string and string buffer
Similar Threads
Doubt in this()
Cloning Doubt
== doubt
Rules round up doubt.
srting doubt on ==