33

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Pan
  • 6,455
  • 5
  • 27
  • 27
  • 1
    "You initialize an int variable defined within a method to have a value of 0 until you compute specific values for the int" ... speak for yourself. I use `Integer` if I want to do that, not `int`. – skaffman May 02 '11 at 16:53

6 Answers6

52

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Your example is not correct. c = '\0' notation means octal 0 but not a Unicode 0. You should use '\u0000' for Unicoide 0. – Anatolii Shuba Apr 15 '21 at 15:52
  • @AnatoliiShuba: No, it's still Unicode 0. `'\0'` and `'\u0000'` are the same value. See https://gist.github.com/jskeet/191afed452e1f12dbb00493e29f69b68 – Jon Skeet Apr 15 '21 at 16:06
  • I agree that values are equal. But I dont agree then you refer the code char c = '\0'; as "Unicode 0". It is not correct. Please check Java Language Specification document (any version). The literal '\0' is presented using Octal Escape sequence but not using Unicode escape sequance as you have mentioned – Anatolii Shuba Apr 16 '21 at 03:40
  • The correct sentence is - ...If you want the char equivalent of 0 then it could be assigned as Unicode literal '\u0000' or octal literal '\0' – Anatolii Shuba Apr 16 '21 at 03:47
  • It's an octal escape sequence *for the Unicode value 0*. There's no such thing as a "Unicode literal". Yes, `\0` is an octal escape sequence rather than a Unicode escape sequence, but nothing in my answer is incorrect. The Unicode value 0 absolutely *can* be written as `\0`... in the same way that the Unicode value 65 can be written as any of 'A', '\u0041` or `\101`. I'm not going to change the answer, because it's not incorrect. – Jon Skeet Apr 16 '21 at 06:09
  • You are mixing up char value (bits sequence) and char value's representation (Character Literal). And your "Unicode value 0" doesnt exist at all. Only "Zero value" exists. That "Zero value" character could be represented via Escape Sequence character literal. Escape Sequence character literal could be either UnicodeEscape as '\u000' or OctalEscape as '\0'. Your main asnwer sentence is fine, except the Unicode word. I think this word have to be removed. Otherwise your code should be changed to "char c = '\u0000'" – Anatolii Shuba Apr 18 '21 at 05:02
  • 1
    @AnatoliiShuba: To be even more precise, it is the UTF-16 code unit with a value of 0. It is the same *value* that can be written as `\u0000`, in the same way that 16 is the same value as 0x10. And no, I'm not mixing up different representations. However, I doubt that I'm going to persuade you that I'm right, and you're certainly not going to persuade me that you're right (or that it would have been worth nitpicking to this extent in the first place even if you *had* been right). I suggest we let the comment thread die at this point. – Jon Skeet Apr 18 '21 at 05:22
15

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.

user unknown
  • 35,537
  • 11
  • 75
  • 121
4

i would just do:

char x = 0; //Which will give you an empty value of character
Vin.X
  • 4,759
  • 3
  • 28
  • 35
2

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.

John Kane
  • 4,383
  • 1
  • 24
  • 42
0

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?

Community
  • 1
  • 1
kolboc
  • 805
  • 1
  • 8
  • 22
0

Perhaps 0 or '\u0000' would do?

Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60