• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

internal doubt abou string and string buffer

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
from
partha
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

}
}
regards
deekasha
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
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;
if(a==b){
System.out.println(" a and b are equal");}
else{
System.out.println("false");
}
// a now points to a different object
a=a.replace('h','r');
System.out.println("a is"+a);
System.out.println("b is"+b);

// a is not equal to b as a new object is created
if(a==b){
System.out.println("true");}
else{
System.out.println("false");
}

// case of StringBuffer
StringBuffer ab=new StringBuffer("hello");
StringBuffer bb=ab;
if(ab==bb){
System.out.println("true");}
else{
System.out.println("false");
}
// ab still points to the same object
ab=ab.append('c');
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
if(ab==bb){
System.out.println("true");}
else{
System.out.println("false");
}

}
}
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic