简体   繁体   中英

Passing char into a method with an int parameter

The output from the following code is 123 because substring takes from beginIndex to EndIndex - 1. However, I am surprised how char here is understood as 3 (int) because substring take two ints. What is the concept behind this?

String x = "12345";
char a = 3;
x = x.substring(0, a);
System.out.println(x);

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 .

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.

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

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.

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

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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM