aspose file tools*
The moose likes Beginning Java and the fly likes String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Buffer" Watch "String Buffer" New topic
Author

String Buffer

PSV Kumar
Greenhorn

Joined: Sep 20, 2003
Posts: 10
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..
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
To answer this question you need to understand these 2 scenarios -
1. Primitive datatypes: final primitive types cannot be changed once initialized.
2. Object reference: If the final keyword is related to the reference, it means that the reference can point to only one object. You cannot point the final reference to some other Object else you will get a compile time error. However, you can change the value of the object [as you are doing for StringBuffer]. But this is slightly complicated in case of String objects. Since String objects are immutable you cannot change its[String Object's] value and if the reference to the String is final you can neither change the reference to point to some other String object. So the String final object behaves like primitive data types.
Hope this helps
PSV Kumar
Greenhorn

Joined: Sep 20, 2003
Posts: 10
Thanx Harpreet....i got it now
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Harpeet,
final StringBuffer str_buf = new StringBuffer("abc");
str_buf.append("xyz");
//OK
final String str= "abc";
str.concat("xyz");
//This also OK
BUT......
final StringBuffer str_buf = new StringBuffer("abc");
str_buf=str_buf.append("xyz");
// NOT OK Compile time error >>>>>>>>>>cannot assign a value to final variable sb
final String str= "abc";
str=str.concat("xyz");
// NOT OK Compile time error >>> cannot assign a value to final variable s
Can u Please Explain Why???for Both String and StringBuffer
Again in the Ok part of String is print str we get only abc xyz is misssing
But of StringBuffer part we are getting the intended result as abcxyz
pls explain.
Thanks


Thanks,
Mathews
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
You can change the value of the object, but you can not change the reference of the object.
It won't allow even if you are assigning the reference of the same object without any value change. This is applicable for premitive type also.
e.g.
final int i = 25;
i=i; // This won't compile
so reassigning is not premitted.
Cheers


Sanjeev Kaushik
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
final StringBuffer str_buf = new StringBuffer("abc");
str_buf.append("xyz");
//OK
final String str= "abc";
str.concat("xyz");
//This also OK
BUT......
final StringBuffer str_buf = new StringBuffer("abc");
str_buf=str_buf.append("xyz");
// NOT OK Compile time error >>>>>>>>>>cannot assign a value to final variable sb
final String str= "abc";
str=str.concat("xyz");
// NOT OK Compile time error >>> cannot assign a value to final variable s
Can u Please Explain Why???for Both String and StringBuffer
Again in the Ok part of String is print str we get only abc xyz is misssing
But of StringBuffer part we are getting the intended result as abcxyz
pls explain.
-------------------------------------------------------------------------
String is immutable, but StringBuffer is not. When you gave the command str.concat("xyz"). You did not assign this to any reference hence it still retained its earlier value which was "abc". Also note that it was not possible to assign this back to the str reference which you already declared as final meaning you can not change the object it pointed at; however in the case of String Buffer you only succeded in changing the object pointed at by the reference str_buf. The previous object(abc) which it was pointing at would be garbage collected if nothing else points on to it.
Anselm Paulinus
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Not a Servlet question, moving to the Java in General (beginner) forum.
Dave
 
 
subject: String Buffer