-3

Referring to Java overloading - long and float, which mentions rules in JLS #15

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.

Why is the following code prints float?

int q = 2;
a(q);

void b(long a) {
        System.out.println("long");
}

void a(float a) {
        System.out.println("float");
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Sajin Surendran
  • 248
  • 6
  • 17

1 Answers1

0

There's only 1 method named a which accept float parameter,

Java support relation (direct supertype relation) from float to int (using long in the middle):

float > long - > int

If you rename method b to a, you will execute method with long as you expected

void a(long a) {
        System.out.println("long");
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Why conversion didn't stop at long? and if there were static void c(double c) { System.out.println("double"); }, the result is not "double" but still "float" – Sajin Surendran Dec 23 '18 at 12:19