[java] char initial value in 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.

This question is related to java

The answer is


Either you initialize the variable to something

char retChar = 'x';

or you leave it automatically initialized, which is

char retChar = '\0';

an ascii 0, the same as

char retChar = (char) 0;

What can one initialize char values to?

Sounds undecided between automatic initialisation, which means, you have no influence, or explicit initialisation. But you cannot change the default.


As you will see in linked discussion there is no need for initializing char with special character as it's done for us and is represented by '\u0000' character code.

So if we want simply to check if specified char was initialized just write:

if(charVariable != '\u0000'){
 actionsOnInitializedCharacter();
}

Link to question: what's the default value of char?


i would just do:

char x = 0; //Which will give you an empty value of character

you can initialize it to ' ' instead. Also, the reason that you received an error -1 being too many characters is because it is treating '-' and 1 as separate.


Perhaps 0 or '\u0000' would do?