Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char initial value in Java

Tags:

java

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.

like image 668
Pan Avatar asked May 02 '11 16:05

Pan


People also ask

How do you initialize a char in a string in Java?

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 .

What is a char array initialized to in Java?

For type char, the default value is the null character, that is, '\u0000' .

What is '\ u0000 in Java?

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


1 Answers

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.

like image 144
Jon Skeet Avatar answered Oct 14 '22 14:10

Jon Skeet