2

I try to understand overloading rules which uses java compiler.

it is too hard for me.

public class Main {

    public static void var(Long x) {
        System.out.println("Long");
    }

    public static void var(int... x) {
        System.out.println("int... x");
    }


    public static void main(String... args) {
          var(5);
    }
}

output:

int...

In Internet I found next rules:

  1. Primitive Widening > Boxing > Varargs.
  2. Widening and Boxing (WB) not allowed.
  3. Boxing and Widening (BW) allowed.
  4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
  5. Widening between wrapper classes not allowed

but I cannot apply this rules to this code behaviour.

Can you help me?

my wrong version:

boxing int -> Integer and widening to Long

Additional question

public class Main {

    public static void var(Object x) {
        System.out.println("Object");
    }

    public static void var(int... x) {
        System.out.println("int... x");
    }


    public static void main(String... args) {
          var(5);
    }
}

output:

object
8
  • Well, the answer is in the question: Widening and Boxing (WB) not allowed. To call the method taking a Long, the int 5 would have to be widened to a long, then boxed to a Long. And that is not allowed. Commented Mar 23, 2014 at 16:32
  • boxing int -> Integer and widening to Long Commented Mar 23, 2014 at 16:33
  • Boxing and Widening (BW) allowed. Commented Mar 23, 2014 at 16:33
  • Widening between wrapper classes not allowed. Anyway, the rules are extremely complex, and the source of truth is the JSL. Commented Mar 23, 2014 at 16:35
  • for me corresponds part of jls super extra extremely complicated Commented Mar 23, 2014 at 16:37

1 Answer 1

2

boxing int to Integer and widening to Long

Although int to Integer boxing is fine, but Integer to Long is not a valid widening. Long is not supertype of Integer. So that's not valid. One option is widening followed by boxing. But such conversion is not allowed in Java. So the only option that is left is using varargs.

Sign up to request clarification or add additional context in comments.

7 Comments

I noticed one more strange behaviour. can you explain it ?(added in topic)
@gstackoverflow That is boxing followed by widening. Integer to Object is a valid widening.
boxing+widening - third, but varargs - in first step
@gstackoverflow Just remember one thing while method overloading -> varargs comes last. Compiler will always first try exact match, then widening, then boxing, then boxing followed by widening, and then varargs. The rules are there in JLS.
Are there exist vararg + widening and vararg+boxing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.