• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Widening and Boxing Confusion

 
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone.

I am reading JLS to understand the method matching by the JVM. A compiler cant widen and box. Here is one example which is
giving compile time error.



I know we cannot widen from one wrapper class to the other. What is happening here? Can someone explain?

Thanks,
Samar
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samar Bir wrote:
I know we cannot widen from one wrapper class to the other. What is happening here? Can someone explain?



Can you elaborate the issue you are having? One one hand, you are saying that something is not allowed. On the other hand, you are saying that the compiler is not allowing it. Doesn't these two points match? Where is the issue?

Henry
 
Samar Bir
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

It is depicting a widen and box concept. My question is, this is integer by default. Will it widen to long and then Box it? I am confused why it is giving a compile time error?

Why it cannot be widened to double? I guess double has enough space to hold an int and then box to double?

I am just confused at these potential points.

Thanks,
Samar


 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samar Bir wrote:
It is depicting a widen and box concept. My question is, this is integer by default. Will it widen to long and then Box it? I am confused why it is giving a compile time error?

Why it cannot be widened to double? I guess double has enough space to hold an int and then box to double?



You already answered the question in your original post.

Samar Bir wrote:
I am reading JLS to understand the method matching by the JVM. A compiler cant widen and box. Here is one example which is
giving compile time error.



The Java Language Specification defines what is allowed in a method invocation conversion. And widening (implicitly) followed by boxing (via autoboxing) is not one of the allowable options.

So, the answer to why the compiler doesn't allow it. It is because it is defined that way by the JLS. As for why the JLS doesn't allow it -- well, that is probably going to be left to speculation, unless someone has some sort of knowledge (or know someone) who help create the JLS.

Henry
 
Samar Bir
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks very much for your elaborate answer. It cleared some of my confusions.

Thanks,
Samar
reply
    Bookmark Topic Watch Topic
  • New Topic