Passing char into a method with an int parameter

前端 未结 8 2067
庸人自扰
庸人自扰 2020-12-11 17:38

The output from the following code is 123 because substring takes from beginIndex to EndIndex - 1. However, I am surprised how char

相关标签:
8条回答
  • 2020-12-11 18:06

    This is the concept of implicit and explicit casting. the char is of small data type then the integer so when pass character as a value then it is auto-convert the char value into int value.

    public static void age (int  num){
            System.out.println(num);
       }
               char character = '2';
                age(character); output is 50.
    

    The character form '0' to '9' print values 48 to 57

    0 讨论(0)
  • 2020-12-11 18:11

    Others already explainted why it works but note that it is bad practice to use char variables for indices, since they have different associated semantics and thus it is confusing to use a char as an index.

    Use chars only to store character data and probably better: try to avoid char altogether, since they are not even wide enough to store every character (see Unicode and code point discussion). Use int to store character code points instead.

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