-2

Why does autoboxing happen in the method

public static int compareAges(Person p1, Person p2) {
    return ((Integer) p1.getAge()).compareTo(p2.getAge());
}

but we get a compiler error in the method

public static int compareAges(Person p1, Person p2) {
    return p1.getAge().compareTo(p2.getAge());
}

?


As per the Javadocs:

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

So, in other words, why is the compiler not able to perform autoboxing in the second method? Is it because in the second method, the binding is not explicit, whereas the binding is unambiguous in the first method.

displayName
  • 13,888
  • 8
  • 60
  • 75
  • 1
    The answer here https://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java is a good read. – Naman Aug 16 '17 at 04:09
  • 3
    @nullpointer This has nothing to do with binding. Java simply chose not to support this syntax. Found a duplicate from almost 10 years ago https://stackoverflow.com/questions/4242/why-doesnt-java-autoboxing-extend-to-method-invocations-of-methods-of-the-autob – Oleg Aug 16 '17 at 04:20
  • 1
    @Oleg Even I was diving deeper to come up with something like that. And ya I guess `Integer.compare(a,b)` should effectively be used for such occurrences. – Naman Aug 16 '17 at 04:25
  • 1
    @nullpointer, Oleg: I felt there would be a stronger reason than just choosing not to support it. Nevertheless, that is what comes up as the answer everywhere. – displayName Aug 16 '17 at 04:32
  • @nullpointer Probably just another thing James Gosling didn't find useful, so of course that means nobody else has any use for it (like operator overloading). – Oleg Aug 16 '17 at 04:34
  • ok, give me a sec – Oleg Aug 16 '17 at 04:50

2 Answers2

1

In JavaDoc about autoboxing https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.

  • Assigned to a variable of the corresponding wrapper class.

In the second method, autoboxing will not happen.

Viet
  • 3,349
  • 1
  • 16
  • 35
  • The doc does exactly say what you have quoted. I know autoboxing will not happen in the second method. The 'why' behind it is precisely my question. – displayName Aug 16 '17 at 04:33
  • Because currently, Java is just supporting autoboxing in two cases(assign & passed as a parameter). In your case is calling a method of Integer (the variable is int) and Java is not supporting now. – Viet Aug 16 '17 at 04:38
1

Java chose not to support Autoboxing when you call a method on a primitive probably because James Gosling would never do such a thing himself so this of course means that no other developer will ever need it.

If you don't get the reference check this answer.

Because James Gosling said so:

I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++.

James Gosling. Source: http://www.gotw.ca/publications/c_family_interview.htm

Oleg
  • 6,124
  • 2
  • 23
  • 40