Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
    Bookmark Topic Watch Topic
  • New Topic

StringBuffer

 
PSV Kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
Plz look in to this code and clear my doubt...
------------------------------------------------------------------
final StringBuffer str_buf = new StringBuffer("abc");
str_buf.append("xyz");
here it is appending the string buffer eventhough it is final. But when i want to concat a string which is final with another string then it says 'cannot assign a value to final variable.
My question is how can stringbuffer append even though it is declared as final?
thanks in advance..
 
MSanjeevMehra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi,
u r right, object declared as fianl can not be referenced to other or new object. ie. when u concatinate two strings, it creats new object. but when u append StringBuffer, it changes object's state.
easy eg. to understand it would be
class ABC
{
String str;
ABC(String str)
{
this.str=str;
}
}
class XYZ
{
final ABC obj1 = new ABC("Hello");
ABC obj2 = new ABC("Hi");

obj1 = obj2; // will not work, because obj1 is declared as final and value of ref., obj1, cannot be changed.
obj1.str = "Hi"; // will work, because u r not changing ref. but state (value of obj1's property).
}
 
PSV Kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank u Sanjeev....ur explanation and example cleared my doubts..thank u once again
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
You have created a duplicate conversation here originally in the Servlet forum, I moved it to the JiG(beginner) forum.
Please don't post the same question to multiple forums. It creates duplicate converstaions and wastes the time of the poeple trying to help you.
I'm going to ask for this thread to be closed, anyone who wants to help should see the other thread.
thanks,
Dave.
 
    Bookmark Topic Watch Topic
  • New Topic