GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Dishi Jain
Ranch Hand

Joined: Jan 16, 2011
Posts: 46




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?
alex galvao
Greenhorn

Joined: Jul 19, 2006
Posts: 2
Don't use Object and primitive, use primitive and primitive
for example:
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Take a look on great 5 Golden Rules of widening, boxing & varargs


hope this helps.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

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
gaurav gupta sitm
Greenhorn

Joined: Feb 09, 2011
Posts: 6
widening followed by boxing not allowed

Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

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...


Java Experience
Dishi Jain
Ranch Hand

Joined: Jan 16, 2011
Posts: 46

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?


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jan 16, 2011
Posts: 46

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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Yes, but not in the same single assignment. You can't do:
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting