Possible Duplicate:
Java string replace and the NUL (NULL, ASCII 0) character?
I'm doing some String algorithms in Java, and i noticed that wherever i include a char with the 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's the reason/cause of this behaviour?
'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.
But the answer is 0 is an integer immediate and 0. is a floating point immediate.
\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case. The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0 ).
\0 is the same as \0n where n is also 0, and is used as a null character. Kind of equivalent to Epsilon in RE, which is used to represent a string that is null. \0 isn't printed because it's a control character, but that doesn't mean it's disregarded by the compiler.
To represent empty char in Java, you can use different methods such as empty single quotes, null character (\0), Unicode value (\u0000), MIN_VALUE Constant of the Character class.
For some additional insight, add the following to you 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) is not displaying everything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With