The default value of a char attribute is indeed '\u0000' (the null character) as stated in the Java Language Specification, section §4.12.5 Initial Values of Variables .
In my system, the line System.out.println('\u0000'); prints a little square, meaning that it's not a printable character - as expected.
The default value of a char attribute is indeed '\u0000' (the null character) as stated in the Java Language Specification, section §4.12.5 Initial Values of Variables .
In my system, the line System.out.println('\u0000'); prints a little square, meaning that it's not a printable character - as expected.
'\u0000' is the default value for a character. Its decimal equivalent is 0.
When you are declaring some char variable without initializing it, '\u0000' will be assigned to it by default.
see this code
public class Test {
char c;
public static void main(String args[]) throws Exception {
Test t = new Test();
char c1 = '\u0000';
System.out.println(t.c);
System.out.println(c1);
System.out.println(t.c == c1);
}
}
This code will print true for the last print.
What are the default values of the char array in Java? - Stack Overflow
Default value of char data type is '\u0000'.
What is the initial values of char arrays?
In Java, if a variable is declared but nothing is assigned to it, what is its value?
Videos
So, I'm learning about arrays using int name = new int[5] where it creates a fixed size array (in this case, it has 5 items of arrays).
As for int, it will fill it with 0 in each item as the initial values. For boolean, false. For double, 0.0. For String, null.
But then, I tried create the char array. But, when I printed it out, it shows weird values. This is what it prints out on my command line. What the hell is that?
It's the same as for any type: the default value for that type. (So the same as you'd get in a field which isn't specifically initialized.)
The default values are specified in JLS 4.12.5:
For type char, the default value is the null character, that is,
'\u0000'.
Having said that, it sounds like really you want a List<Character>, which can keep track of the actual size of the collection. If you need random access to the list (for example, you want to be able to populate element 25 even if you haven't populated element 2) then you could consider:
- A
Character[]usingnullas the "not set" value instead of'\u0000'(which is, after all, still a character...) - A
Map<Integer, Character> - Sticking with
char[]if you know you'll never, ever, ever want to consider an element with value'\u0000'as "set"
(It's hard to know which of these is the most appropriate without knowing more about what you're doing.)
You can also convert it to int and then compare.
for ex -
Suppose I declare my array as
char[] arr = new char[10];
arr[3]='1';
Then, the below code
for(char c:arr){
if((int)c!=0){
System.out.println(c);
}
}
Will output
1