Click to See Complete Forum and Search --> : Strings


Deepa Natarajan
August 14th, 1999, 10:48 PM
Q.1

public class Quest{
public static void main(String[] arg){
String s1 ="abc";
String s2 ="def";
String s3 =s1.concat(s2.toUpperCase());
system.out.println(s1+s2+s3);
}
}

Ans. abcdefabcDEF - I got this answer and it contradicts with the concept that String Objects are immutable.
How does it work?

Thanks,
Deepa

ramu
August 16th, 1999, 12:32 AM
Hai Deepa,

String class is immutable. But StringBuffer class is mutable. String concatenation is immplemanted through
StringBuffer class and its append method.
Hope you understand

August 17th, 1999, 03:36 AM
Deepa,

Strings are immutable. You can see that s1 or s2 remains unchanged.
The concat does not affect the string object on which it is called.
Also the toUppercase does not affect the string object.
These methods return a cha

August 19th, 1999, 07:48 AM
Java language supports + operator which is same concat method in string.The + operator or concat method uses StringBuffer and append internally to change the String.So Strings are immutable is still true. The + operator/concat method is provided as this was available in C++,I guess.