• Post Reply Bookmark Topic Watch Topic
  • New Topic

Widening and Boxing Confusion  RSS feed

 
Samar Bir
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Henry,

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

Thanks,
Samar
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!