You initialize an int
variable defined within a method to have a value of 0
until you compute specific values for the int
. What can one initialize char
values to?char retChar = '';
this gives an error and if I initialise to -1
it says too many characters.
The default value of char type is \u0000 , and if we want to initialize a char value with the default value, just create it as an instance variable and let the Java compiler do the rest of the work. If you want to see and print the default value, just cast the value, and you will see it is 0 .
For type char, the default value is the null character, that is, '\u0000' .
\u0000 is the null character in Unicode. It's different from null in Java, but has a similar meaning. @testerjoe2 Your code means that if the path contains the null character, it's status is INVALID , otherwise it's CHECKED .
Typically for local variables I initialize them as late as I can. It's rare that I need a "dummy" value. However, if you do, you can use any value you like - it won't make any difference, if you're sure you're going to assign a value before reading it.
If you want the char
equivalent of 0, it's just Unicode 0, which can be written as
char c = '\0';
That's also the default value for an instance (or static) variable of type char
.
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