简体   繁体   中英

Why Does Java Compiler Treat Long Data Type As Double Instead of Integer By Default?

I'm new to Java and I stumbled upon this while testing some code. Why does Java pass in x (of data type long) into the function that takes in double parameters instead of the one with integer parameters. I would appreciate it if somebody could kindly explain to me why (Even though it might be an easy question to most of you!) Thank you in advance!

public class Hello {
public static void main (String [] args) {
    long x=1;
    System.out.println("Before calling the method, x is "+x);
    increase(x);
    System.out.println("After calling the method, x is "+x);
    System.out.println();

    double y=1;
    System.out.println("Before calling the method, y is "+y);
    increase(y);
    System.out.println("After calling the method, y is "+y);


}

public static void increase(int p) {
    p+=1;
    System.out.println(" Inside the method is "+p);

}

public static void increase(double p) {
    p+=2;
    System.out.println(" Inside the method is "+p);

}   }

The conversions allowed when calling a method are defined by JLS Chapter 5. Implicit primitive conversions must be widening , that is, not result in a loss of magnitude (though in the case of long to double, may result in a loss of precision).

There are six kinds of conversion contexts in which poly expressions may be influenced by context or implicit conversions may occur. Each kind of context has different rules for poly expression typing and allows conversions in some of the categories above but not others. The contexts are:

...

Strict invocation contexts (§5.3, §15.9, §15.12), in which an argument is bound to a formal parameter of a constructor or method. Widening primitive, widening reference, and unchecked conversions may occur.

Loose invocation contexts (§5.3, §15.9, §15.12), in which, like strict invocation contexts, an argument is bound to a formal parameter. Method or constructor invocations may provide this context if no applicable declaration can be found using only strict invocation contexts. In addition to widening and unchecked conversions, this context allows boxing and unboxing conversions to occur.

JLS 11 Chapter 5

Casting from long to int is a narrowing primitive conversion, as it may result in a loss of magnitude information. So it is not called unless you first explicitly cast to (int) .

You have 2 overloaded methods for increase() with int and double as input parameters. Also you are passing input parameter as long type.

In Java the UpCasting happens in the below format.

byte -> short -> int -> long -> float -> double

So when you pass the long type value as input parameter, compiler first looks for exact match in the input parameter. If it not found then it will upcast to next value.

Hence long value can be accepted by the method having double value as input parameter.

Please go through the below url's.
Type Conversion In Java
Type Conversion - Oracle Documentation

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