What is the value of char zero (0) in Java?

Possible duplicate:
replace Java string and NUL character (NULL, ASCII 0)?

I execute some string algorithms in Java, and I noticed that wherever I include char with a value of 0 (zero), it marks the end of the string. Like this:

String aString = "I'm a String"; char[] aStringArray = aString.toCharArray(); aStringArray[1] = 0; System.out.println(new String(aStringArray)); //It outputs "I" 

What is the reason / reason for this behavior?

+7
source share
3 answers

The character '\0' is the null character. This is a control character, and it does not break the line, not how the lines work in Java (which is how they work in C, though.)

+10
source

For more information, add the following code:

  System.out.println(new String(aStringArray).length()); for (Byte b : new String(aStringArray).getBytes()) { System.out.print("["+b+"]"); } 

Your rendering system (console or output window) does not display everything.

+2
source

I am wondering if you posted your actual code. When using the String constructor (byte []), the actual length of the string depends on the contents of the array.

0
source

All Articles