简体   繁体   中英

Why does this method return an output?

class Demo {

    public void myMethod(int a, double b, char c) {
        System.out.println("Version 1.");
    }

    public void myMethod(int a, double b, double c) {
        System.out.println("Version 2.");
    }

    public void myMethod(int a, boolean b, double c) {
        System.out.println("Version 3.");
    }

    public void myMethod(int a, boolean b, boolean c) {
        System.out.println("Version 4.");
    }

    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.myMethod(1, 100, 'b');
    }

}

1 and 100 are both of type int and b is a type char so shouldn't the result be a compile error? i do not understand why it returns "version 1".

could someone please explain to me why?

thank you

The value 100 can be used as a double, it's the widening primitive conversions :

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

long to float or double

float to double

At runtime, your compiler converts the int 100 to type double because you left it no other choices (ie another overloaded method that accepts exactly int , int and char ).

In other words, instead of throwing a RuntimeException , your compiler converts the int that takes less size to the type that takes more size which is in your case a double . That is automatically performed without loosing information.

That is called Widening Primitive Conversion or auto conversion or implicit conversion.

The scenario you described occurs for type conversions in java. The official java doc has already provided the 13 categories of conversions that are possible in java. Your one also falls on one of them, the widening primitive one.

You can easily understand the logic if you can understand this simple code:

int a = 12.80;
System.out.print(a);

12.80 is not an integer data type and if 12.80 is to be converted to an integer then the compiler has to cut out .80 from there which will lead you to possible lossy conversion compilation error.

Again, we may want to run another code:

double b = 12;
System.out.print(b);

If you think that the above code may give you a compilation error, then you are completely wrong. Because, the output here is 12.0 . As no data loss occurs here, int can be easily converted to double .

So, you should now understand that not all the data types can be converted to another one. You may refer to the official java doc link provided above to know about all types of conversions.

Now, in your case, 100 is considered to compiler as 100.0 or something like that as there is no other overloaded method with integer data type.

Hope you understand now.

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