Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of char zero (0) in Java? [duplicate]

Tags:

java

string

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?

like image 532
Carlos Gavidia-Calderon Avatar asked Mar 17 '12 20:03

Carlos Gavidia-Calderon


People also ask

What is the meaning of '\ 0?

'\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.

What are the types of the values 0 and 0 Java?

But the answer is 0 is an integer immediate and 0. is a floating point immediate.

What is the meaning of '\ 0 in C?

\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 ).

Do strings in Java have \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.

Can char be empty in Java?

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.


1 Answers

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.

like image 191
Java42 Avatar answered Sep 19 '22 12:09

Java42