java - Why does trim() not remove the trailing white spaces in this example? -
i revising java here: http://java-success.blogspot.com.au/2012/06/core-java-coding-questions-frequently.html , came along question:
"q1. output of following code snippet?
string s = " hello "; s += " world "; s.trim( ); system.out.println(s);
a1. output be
" hello world "
with leading , trailing spaces. expect trimmed "hello world".
so, concepts question try test?
string objects immutable , there trick in s.trim( ) line. understanding object references , unreachable objects eligible garbage collection."
can explain why trailing white spaces not removed?
the method trim()
doesn't modify string
, immutable. returns trimmed string
, promptly ignored, leaving s
unchanged. replace
s.trim( );
with
s = s.trim( );
Comments
Post a Comment