Clarification on String concatenation in java -
this question has answer here:
- why java's concat() method not anything? 6 answers
i beginner in java, please go easy on comments.
string test = "test"; test.concat("test"); system.out.println("concatenated string: " +test); system.out.println("concatenated test: " +test.concat(test)); why first print statement not print testtest, while second 1 does? might basic question, not find answer online. can please explain?
because you're not assigning concatenated string value (as concat() returns resulting string), lost.
if want result, need do:
test = test.concat("test"); the second println() works, because resulting concatenated string returned, captured , used system.out.println(), lost outside of println() not assigned (so if check test after, still contains "test").
here's doc: http://docs.oracle.com/javase/7/docs/api/java/lang/string.html#concat%28java.lang.string%29
Comments
Post a Comment