Why the method of concat(String str) in String Class doesn't work?

B

Bruce Sam

Below is my simple java program:
/* **************************************** */
public class MyString {
public static void main(String[] args) {
String str1 = "BCDE";
System.out.println(str1);
String str2 = "A";
System.out.println(str2);
str2.concat(str1);
System.out.println(str2);
}
}
/* **************************************** */
I think the run result should be as:
BCDE
A
ABCDE
But in fact,it is:
BCDE
A
A
Why the method of concat() doesn't work?What I hope to do is only to
concatenate the str1 to the end of str2;
 
J

Joona I Palaste

Bruce Sam said:
Below is my simple java program:
/* **************************************** */
public class MyString {
public static void main(String[] args) {
String str1 = "BCDE";
System.out.println(str1);
String str2 = "A";
System.out.println(str2);
str2.concat(str1);
System.out.println(str2);
}
}
/* **************************************** */
I think the run result should be as:
BCDE
A
ABCDE
But in fact,it is:
BCDE
A
A
Why the method of concat() doesn't work?What I hope to do is only to
concatenate the str1 to the end of str2;

I haven't read the JavaDocs, but I think String.concat(String) does not
alter the String which it was called on, but instead returns a new
String.
Try this:

public class MyString {
public static void main(String[] args) {
String str1 = "BCDE";
System.out.println(str1);
String str2 = "A";
System.out.println(str2);
str2 = str2.concat(str1);
System.out.println(str2);
}
}
 
A

andreas kinell

Below is my simple java program:
/* **************************************** */
public class MyString {
public static void main(String[] args) {
String str1 = "BCDE";
System.out.println(str1);
String str2 = "A";
System.out.println(str2);
str2.concat(str1);
System.out.println(str2);
}
}
/* **************************************** */
I think the run result should be as:
BCDE
A
ABCDE
But in fact,it is:
BCDE
A
A
Why the method of concat() doesn't work?What I hope to do is only to
concatenate the str1 to the end of str2;

concat returns the new String.

andreas
 
P

Paul H. van Rossem

Below is my simple java program:
/* **************************************** */
public class MyString {
public static void main(String[] args) {
String str1 = "BCDE";
System.out.println(str1);
String str2 = "A";
System.out.println(str2);
str2.concat(str1);
System.out.println(str2);
}
}
/* **************************************** */
I think the run result should be as:
BCDE
A
ABCDE
But in fact,it is:
BCDE
A
A
Why the method of concat() doesn't work?What I hope to do is only to
concatenate the str1 to the end of str2;

It can't alter str2, as String is immutable. So it returns a new String.

Paul.
 
S

Sudsy

Bruce Sam wrote:
Why the method of concat() doesn't work?What I hope to do is only to
concatenate the str1 to the end of str2;

Others have cited how the method works. In order to achieve your stated
goal you could just do this:

str2 += str1;
 
J

John C. Bollinger

Sudsy said:
Bruce Sam wrote:



Others have cited how the method works. In order to achieve your stated
goal you could just do this:

str2 += str1;

Which is equivalent to str2 = str2.concat(str1) (and also to str2 = str2
+ str1, of course). It is important to understand that in either case
the String originally referred to by variable str2 is *not* modified;
instead, a new String is constructed and a reference to it is stored in
str2, replacing the previous reference. I know Sudsy understands this
well, but I have a colleague who at one point was confused by it,
assuming that the += construct caused the left-hand String to be
modified, and therefore that Strings were mutable.


John Bollinger
(e-mail address removed)
 
S

Sudsy

John C. Bollinger wrote:
assuming that the += construct caused the left-hand String to be
modified, and therefore that Strings were mutable.

Good point! People don't always recognize when new objects are
created. Perhaps I contributed to the confusion?
John is, as always, correct and the clarification should provide
guidance as to the proper use of methods returning references
and language constructs which perform essentially the same thing.
 
J

John C. Bollinger

Sudsy said:
John C. Bollinger wrote:


Good point! People don't always recognize when new objects are
created. Perhaps I contributed to the confusion?

Not in the particular case I was thinking of (which happened a year or
two ago). The person in question was approaching Java with good C++
knowledge but little Java-specific training or background. He was
genuinely ignorant of the fact that Java Strings are immutable, which
would have been enough for him to draw the correct conclusion.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top