Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

java - Why stringbuilder stops adding elements after using the null character?

When i am using the null character 'u0000', stringbuilder will stop appending new elements.

For example:

StringBuilder _stringBuilder = new StringBuilder();
_stringBuilder.append('u0000');
_stringBuilder.append('a');
System.out.println("."+_stringBuilder+".");

returns

. 

I understand that the null value should not be printed (or printed like if it was a null String value) but in this case, why does stringbuilder is failing to add more elements ?

Note: I am using jdk 1.6.0_38 on ubuntu.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your stringBuilder still appends the elements, see:

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('u0000');
System.out.println(stringBuilder.length()); // prints 1
stringBuilder.append('a');
System.out.println(stringBuilder.length()); // prints 2

My guess is that your console stops printing after the u0000 termination character. A different console might handle that differently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...