I am bit confused about how Java handle conversion.
I have char array consist of 0 (not ‘0’)
char[] bar = {0, 'a', 'b'};
System.out.println(String.valueOf(bar));
When this happens println method does not output anything. But when I treat zero as character:
char[] bar = {'0', 'a', 'b'};
System.out.println(String.valueOf(bar));
Then it output “0ab” as expected.
My understanding was if you declare array of primitive type with empty value like:
char[] foo = new char[10];
those empty cells have default value of 0 in Java, so I thought it was ok to have 0 in the char array but seems like not. Could anyone explain why print method is not even outputting ‘a’ and ‘b’ ?
As you have discovered, the character value of the integer
0and the character literal'0'are different. To convert a number to the character representation of a decimal digit, you need to do something like this:You can also do this as:
where
radixhas a value betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX. A radix of10gives you a decimal digit. SeeCharacter.forDigit(int,int).The character value zero is the Unicode codepoint NULL (0x0000). This is a non-displayable “control character”. If you try to display it, you are liable to see either nothing at all (as you are getting) or a special glyph that means “I cannot display this character”.