• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Overloading and Autoboxing question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I understand that widening gets more priority over autoboxing and var-args,
and that is not possible to do widening and then boxing, but boxing and then widening is possible.
But what's happened when you try to pass Integer and there is only long or Long?
First, at line 19 autoboxing has made, then at line 10 unboxing?

Thanks!

 
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Java Language Specification, Section 5.3, allows for unboxing followed by widening -- so, the method (marked via //3) will be called.

Henry
 
Maor Pau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
The Java Language Specification, Section 5.3, allows for unboxing followed by widening -- so, the method (marked via //3) will be called.

Henry


thanks!
can I ask what is the reason I cannot  do like:

but this one will work:
 
Henry Wong
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maor Pau wrote:
can I ask what is the reason I cannot  do like:



The Java Language Specification, Section 5.3, does not allow for the implicit cast from int to short. Of course, this makes sense, as you can lose precision with the cast.

Maor Pau wrote:
but this one will work:



Interestingly, the Java Language Specification, Section 5.2, which is for assignments (ie. different from the previous example), does allow for the implicit cast from int to short (provided that it is a compile time constant). Since it is a compile time constant, the compiler can check whether precision is lost (or not), and decide to allow it (or not).

Henry
 
Enthuware Software Support
Posts: 4356
39
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Maor Pau wrote:
can I ask what is the reason I cannot  do like:



The Java Language Specification, Section 5.3, does not allow for the implicit cast from int to short. Of course, this makes sense, as you can lose precision with the cast.



To put it in simpler terms, Java does not allow implicit narrowing for passing arguments. The reason given is, "The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the rules of overload resolution."
Widening is OK.

Henry Wong wrote:

Maor Pau wrote:
but this one will work:



Interestingly, the Java Language Specification, Section 5.2, which is for assignments (ie. different from the previous example), does allow for the implicit cast from int to short (provided that it is a compile time constant). Since it is a compile time constant, the compiler can check whether precision is lost (or not), and decide to allow it (or not).

Henry


i.e. Java allows implicit narrowing for assignments but only where the value (i.e. the right hand side of the assignment) is a compile time constant and the value fits in the variable.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!