|
-
August 14th, 1999, 10:48 PM
#1
Strings
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
-
August 16th, 1999, 12:32 AM
#2
Re: Strings
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
#3
Re: Strings
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
#4
Re: Strings
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|