• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 23877
142
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: 23877
142
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: 4428
41
  • 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.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic