Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java overloading - long and float

I was trying to understand java overloading rules. Everything seems fine except following,

public static void main(String[] args) {
    long aLong = 123L;        
    foo(aLong);
}

private static void foo(double aDouble) {
    System.out.println("Foo aDouble");
}

private static void foo(Long aWrapperLong) {
    System.out.println("Foo Wrapper Long");
}

private static void foo(int anInt) {
    System.out.println("Foo Int");
}

private static void foo(float aFloat) {
    System.out.println("Foo Float");
}

Why does the call resolve to foo(float aFloat). I understand the following from JLS,

This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable There may be more than one such method, in which case the most specific one is chosen.

I have intentionally used Wrapper Long here and not primitive long. Primitive long being 64 bit in size does not end up in foo(double aDouble) but 32 bit float foo(float aFloat).

The question Why does Java implicitly (without cast) convert a `long` to a `float`? further clarifies the answer to this question.

like image 990
GauravJ Avatar asked May 05 '17 04:05

GauravJ


3 Answers

This is because of the 'most-specific' rule in JLS #15, which in turn refers to JLS #4.10, which in turn refers to #4.10.1, which states:

The following rules define the direct supertype relation among the primitive types:

  • double >1 float

  • float >1 long

  • long >1 int

  • int >1 char

  • int >1 short

  • short >1 byte

where "S >1 T" means "T is a direct subtype of S", as per JLS #4.10 immediately above this section.

So in this case, in the absence of a direct match on long, and before looking at auto-boxing, the compiler chooses the nearest available supertype, which is float, by the rules above.

like image 52
user207421 Avatar answered Oct 25 '22 01:10

user207421


Quote from JLS:

The process of determining applicability begins by determining the potentially applicable methods (§15.12.2.1).

The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are:

  1. The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.

  1. The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase. ...

Edit: about choosing float instead of double:

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

The first phase of overload resolution will choose two of these four methods

private static void foo(double aDouble) 
private static void foo(float aFloat) 

because first phase doesn't permit boxing/unboxing (Long) and you cannot pass long to method with int parameter without explicit casting. Than the most specific method will be choosen. In this situation float method would be interpreted as most specific than double.

like image 37
DAle Avatar answered Oct 25 '22 03:10

DAle


Conversion has precedence rules. It will choose widening over boxing.

So, in this case compiler searches for method which can accept the parameter bigger(closest possible bigger) than long primitive data type as an argument, which is float.

If you remove methods foo(double aDouble) and foo(float aFloat) from your posted example, then compiler will perform boxing and choose foo(Long aWrapperLong)

like image 45
adi Avatar answered Oct 25 '22 01:10

adi