• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Casting

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Why can't the second one as well follow the same steps?
"widening to int, then boxing" , while //Line 4 gives compilation error!!!

Is it like while assigning non-constant variable Casting is required?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use Object and primitive, use primitive and primitive
for example:
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look on great 5 Golden Rules of widening, boxing & varargs


hope this helps.
 
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dishi Jain wrote:
Why can't the second one as well follow the same steps?
"widening to int, then boxing" , while //Line 4 gives compilation error!!!
Is it like while assigning non-constant variable Casting is required?



In your case, Line 1 would give a compilation error, 'cause of the incompatibility; and yes non-constant variable when narrowing need casting.

Regards,
Gaurav
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
widening followed by boxing not allowed

 
Gaurav Sagar
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gaurav gupta sitm wrote:widening followed by boxing not allowed


The thumb rule states that, "Autoboxing occurs strictly between the primitive datatype and its wrapper class only", not to be confused with widening and casting.
In the above code, line 1 would compile only if you explicitly cast the float to a double.

Regards,
Gaurav
 
author
Posts: 23928
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

Dishi Jain wrote:



Be careful of trying to figure out what combination of widening, narrowing, and auto-boxing are allowed, based on compile time constants. Compile time constants has special rules that allow certain combinations to be allowed that isn't allowed without compile time constants.

Henry
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dishi,

Its very easy to get confused with this concept

Widening and auto(un) boxing don't go together.

I have seen many developers overlooking that...
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly,


is working fine for me. No compile time errors.
Because as per the description in Khalid Mughal, widening and then Boxing is taking place, while it is not legal.
Secondly,


doesnt work. Why is it not following the same steps as in earlier one.
Also what is the difference between,widening and casting as mentioned above?


 
Saloon Keeper
Posts: 14497
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Henry said, there are some special rules for compile time constants which do not follow the general rules. I'm not exactly sure, but I believe that when you assign a constant to a wrapper variable, it will work as long as you are using integral types, smaller than Long.
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per page number 254 in K&B,
You CANNOT widen and then box. (An int can't become a Long.)

Why???
I can widen int->long
and then Box long->Long !!
 
Stephan van Hulst
Saloon Keeper
Posts: 14497
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but not in the same single assignment. You can't do:
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Dishi,

Actually my statement was not fully correct.
The correct statement should be:

Implicit Widening and Auto(un)boxing don't work together.

In the code:


we are having explicit cast and hence it is working.

Don't get confused and try to write some variations of widening and auto(un)boxing in a test class and see what works and what doesn't
 
Henry Wong
author
Posts: 23928
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

Dishi Jain wrote:


is working fine for me. No compile time errors.
Because as per the description in Khalid Mughal, widening and then Boxing is taking place, while it is not legal.



To repeat...

Stephan van Hulst wrote:Like Henry said, there are some special rules for compile time constants which do not follow the general rules.



Thanks Stephan.


Anyway... interestingly, there are no byte or short compile time constants. Byte and short compile time constants are just int compile time constants.

Literals are compile time constants. A "10" is a int compile time constant. A "(short) 10" is actually not a short compile time constant, but is actually an int compile time constant that has been truncated to a short value (which is still 10 here). And... assigning this int compile time constant to an Integer reference just does boxing (and nothing else).

Henry
 
Henry Wong
author
Posts: 23928
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

Dishi Jain wrote:


doesnt work. Why is it not following the same steps as in earlier one.
Also what is the difference between,widening and casting as mentioned above?



In this case, there are float and double compile time constants... but... the special rules for implicit narrowing and boxing doesn't apply to Float and Double -- no special rules apply here.

And as your book mentioned, this doesn't work.

Henry
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic