45,000 Jobs - Get an Interview Call,  Post Your Resume Here
SURESHKUMAR.NET FORUMS
Registered Member Login:
Not a member? Register today!



Welcome to the SURESHKUMAR.NET FORUMS.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.




String and StringBuffer

        

Reply
 
LinkBack Thread Tools Display Modes
Old 07-12-06, 08:48 AM   #1 (permalink)
Member
 
Join Date: Nov 2006
Posts: 32
Thanks: 7
Thanked 0 Times in 0 Posts
Rep Power: 4 sumahals21 is on a distinguished road
Question String and StringBuffer

Hi friends,

Please tell me the difference b/w String and StringBuffer with Example.

Thanks
Suma

sumahals21 is offline Offline   Reply With Quote
Old 07-12-06, 12:28 PM   #2 (permalink)
Senior Member
 
jasmine84's Avatar
 
Join Date: Oct 2006
Posts: 424
Thanks: 10
Thanked 31 Times in 27 Posts
Rep Power: 13 jasmine84 is a name known to all jasmine84 is a name known to all jasmine84 is a name known to all jasmine84 is a name known to all jasmine84 is a name known to all jasmine84 is a name known to all
Re: String and StringBuffer

hi suma...
String object is immutable i.e.,itz contents cannot be modified.while StringBuffer objects can be modified.

the methods which are used to manipulate the data are not directly available in String class.they are available in StringBuffer class.

for example if we need to reverse a string we can use
sb.reverse().we need not use the lengthy procedure to reverse a string.
there are many other methods which can be used for manipulations of strings in String buffer class.
jasmine84 is offline Offline   Reply With Quote
The Following User Says Thank You to jasmine84 For This Useful Post:
sumahals21 (07-12-06)
Old 07-12-06, 07:54 PM   #3 (permalink)
Senior Member
 
Join Date: Dec 2006
Posts: 695
Thanks: 3
Thanked 17 Times in 13 Posts
Rep Power: 11 abeethabsl is a jewel in the rough abeethabsl is a jewel in the rough abeethabsl is a jewel in the rough
Re: String and StringBuffer

Hi suma,

Both String and StringBuffer are final classes.
But String is immutable.Once an instance of it is created one cannot change its contents, though all operations are possible.
For eg:to concate String s1="ABC" and String s2="DEF" we need another string
String s3=s1+s2;which will have the value of "ABCDEF";One cannot append it to s1.
But StringBuffer is mutable and one can change the contents of it.The same scenario in this case can be s1.append("DEF");s1 becomes "ABCDEF".
abeethabsl is offline Offline   Reply With Quote
Old 19-01-07, 01:44 PM   #4 (permalink)
Junior Member
 
Join Date: Dec 2006
Posts: 20
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3 sureshkumar.t is on a distinguished road
Re: String and StringBuffer

String is immutable where as stringbuffer is mutable
ex: if u declare one string object of size 10 .Now u want append that string with another one but the string is overflowed at that time jvm throws an excpetion bu using stringbuffer u can increse the sting object value(u can chnage that value dynamically)
sureshkumar.t is offline Offline   Reply With Quote
Old 19-01-07, 02:02 PM   #5 (permalink)
Member
 
avadhoot.dharmadhikari's Avatar
 
Join Date: Aug 2006
Location: Parli-V
Age: 25
Posts: 77
Thanks: 24
Thanked 9 Times in 8 Posts
Rep Power: 6 avadhoot.dharmadhikari has a spectacular aura about avadhoot.dharmadhikari has a spectacular aura about avadhoot.dharmadhikari has a spectacular aura about
Re: String and StringBuffer

String are immutable means when you try any method with string so JVM creats new object in heap.
Ex: String s="Hello";
So s is reference variable and it refers "Hello" in the heap;
String s1=s.concat("Friend");
Three object are created in heap one is hello, secong is Friend and third is Hello Friend so Now S refers to Hello and s1 refers to Hello Friend menas Friends has no reference variable now means JVM creates new object on every instatnce Old string objetc is not modified it creates new object. but in
case of string buffer it modifies the string object.
avadhoot.dharmadhikari is offline Offline   Reply With Quote
Old 19-01-07, 04:03 PM   #6 (permalink)
Member
 
Join Date: Jan 2007
Age: 27
Posts: 48
Thanks: 1
Thanked 3 Times in 3 Posts
Rep Power: 3 uday321 is on a distinguished road
Re: String and StringBuffer

Hi,

String object is immutable that means

eg. String s1=new String("abc");

for every object that is created jvm will assign internally hashcode to every object to differentiate one object from other it will be very clear from following code:

we can use the hashCode() method available in java.lang package to know the hashcode generated by the jvm for each and every object.

System.out.println("s1 first hashcode--->"+s1.hashCode());
then we can write
s1="def";

System.out.println("s1 second hashcode---"+s1.hashCode());

Here two stmts will give DIFFERENT OUTPUTS bec'z internally instead of modifying the existing string object the jvm will creates the new String object

where as in case of StringBuffer the object is mutable that means

StringBuffer sb=new StringBuffer("abc");

System.out.println("sb first hashcode---"+sb.hashCode());
sb="def";
System.out.println("sb second hashcode--->"+sb.hashCode());

In this case both stmts will print the SAME OUTPUT bec'z internally no other object is created........

Last edited by uday321; 19-01-07 at 04:19 PM..
uday321 is offline Offline   Reply With Quote
Reply

Tags
string , stringbuffer


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Practice Question in JAVA sowmya571 JAVA Technologies 8 15-02-08 08:09 PM
Doubt in EJB deployment in IBM Websphere Application Server sentil77 JAVA Technologies 2 19-10-06 02:04 PM
Hi everyone sentil77 Introduce yourself here 5 10-10-06 01:03 PM


All times are GMT +6.5. The time now is 11:24 AM.

More Interview Questions Here...

Content Relevant URLs by vBSEO 3.3.0