Clarification on String concatenation in java -


this question has answer here:

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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -