I had a question about why the term character is ignored when sending a sequence of characters to STDOUT .
For example, in C when you do the print
printf("%s", "hello\0world");
The output reaches \0
, since it is the character that indicates that the sequence of characters ends there, that is, it only printshello
However, reproducing this same behavior in Java only ignores this character
public static void main(String... args) {
char[] hello = {'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd'};
System.out.println(hello);
System.out.println("hello\0world");
}
and the output is helloworld
, Why is this happening? Is it a compiler issue?
If the compiler is not involved, then how does it distinguish which is the true term character?
The \0, although counted as a character in the sequence by the compiler, is not used in java to terminate the character sequence. Java does not use "null-terminated" for Strings, it simply uses an array of chars to represent the String and provides a method called "length" to know the number of characters in said String (which also takes into account the \0) .