char initial value in Java

前端 未结 6 1041
轮回少年
轮回少年 2020-12-08 19:21

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 on

相关标签:
6条回答
  • 2020-12-08 20:04

    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?

    0 讨论(0)
  • 2020-12-08 20:09

    i would just do:

    char x = 0; //Which will give you an empty value of character
    
    0 讨论(0)
  • 2020-12-08 20:13

    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.

    0 讨论(0)
  • 2020-12-08 20:18

    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.

    0 讨论(0)
  • 2020-12-08 20:21

    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.

    0 讨论(0)
  • 2020-12-08 20:21

    Perhaps 0 or '\u0000' would do?

    0 讨论(0)
提交回复
热议问题