Passing char into a method with an int parameter

前端 未结 8 2070
庸人自扰
庸人自扰 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 17:45

    so substring's function declaration looks like substring(int startIndex, int endIndex). Now when you pass char it is automatically promoted to integer (endIndex) and hence treated as int.

    0 讨论(0)
  • 2020-12-11 17:50

    Technically, this is because char is a subtype of int.

    To determine whether substring(int,int) is applicable to argument (int,char), we first try 15.12.2.2 Phase 1: Identify Matching Arity Methods Applicable by Subtyping, we need to test whether char is a subtype of int. Per 4.10.1 Subtyping among Primitive Types, it is.

    Then, to assign the char argument to the int parameter, per 15.12.4.5 Create Frame, Synchronize, Transfer Control, we apply 5.3 Method Invocation Conversion, which converts char to int, per 5.1.2 Widening Primitive Conversion

    0 讨论(0)
  • 2020-12-11 17:54

    This goes all the way back to C, where char is in essence a narrow integer type and gets implicitly converted to int whenever necessary.

    In Java, this is technically known as a "widening primitive conversion", and is covered in section 5.1.2 of the JLS.

    0 讨论(0)
  • 2020-12-11 17:57

    This is known as implicit casting. The same occurs if you assign an int value to a double. Quick example

        double d = 1;
    

    1 is an int but it is implicitly cast to double (1.0).

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

    In char a = 3;, you could think of it as storing 0011, the binary value of 3. The char '3' is not actually being stored: If you tried treating is a char, you would not get 3. But if you did

    char a = '3';

    Now you're storing the char 3, an ascii value of 51, and if you tried using it as in int, you would get 51.

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

    Take a look at section 5.1.2, where it discusses widening conversions.

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