You initialize an int
variable defined within a method to ha开发者_运维百科ve 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. 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
.
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.
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?
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?
精彩评论