File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit casting" Watch "Implicit casting" New topic
Author

Implicit casting

Tere Luna
Greenhorn

Joined: Jul 19, 2011
Posts: 12
Hi All

I'm a little confused about implicit and explicit casting in Java. I'm preparing for OCPJP, but I have the below code, and I wonder why when you perform an arithmetic operation using literals like 3/2, that can be assigned to a short type attribute, but if you try to perform the same operation but using a variable like short1/4, you get a compiler error. Also the same in the line 5, where you want to assign a byte to a short, I would assume it would compile since byte is lower precision than short, and I would think it would perform an implicit casting

1. Short short1 = 3;
2. Byte byte1 = 4;
3. short1 = 3/2;
4. Short short2 = short1 / 4;
5. short1 = byte1;

Thanks for your help
Tere
Boris Mechkov
Ranch Hand

Joined: May 13, 2011
Posts: 72

Tere Luna wrote:Hi All

I'm a little confused about implicit and explicit casting in Java. I'm preparing for OCPJP, but I have the below code, and I wonder why when you perform an arithmetic operation using literals like 3/2, that can be assigned to a short type attribute, but if you try to perform the same operation but using a variable like short1/4, you get a compiler error. Also the same in the line 5, where you want to assign a byte to a short, I would assume it would compile since byte is lower precision than short, and I would think it would perform an implicit casting

1. Short short1 = 3;
2. Byte byte1 = 4;
3. short1 = 3/2;
4. Short short2 = short1 / 4;
5. short1 = byte1;

Thanks for your help
Tere


First of all, welcome to JavaRanch! Further down the road, please use the "code" tags when you are posting code, so it is easier to read. Now off to the questions...Let's consider this:



The reason you can assign a division of "3 devided by 2" is because it returns an integer of 1, which can fit in a short (integer division). By the way this code: compiles and assigns a zero to short2, so not sure why it did not compile for you.
The reason you cannot assign "byte1" to "short1" is because you cannot assign one wrapper class to another i.e. you cannot assign a Byte object to a Short reference. Why? Because the IS-A test fails i.e. Byte IS NOT a Short. You can assign an Integer to a Number reference like this:

Number n = new Integer();

But this is possible because Integer extends Number or Integer IS-A Number.
Hope this helps!

OCPJP 6
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Tere Luna wrote:Hi All

I'm a little confused about implicit and explicit casting in Java. I'm preparing for OCPJP, but I have the below code, and I wonder why when you perform an arithmetic operation using literals like 3/2, that can be assigned to a short type attribute, but if you try to perform the same operation but using a variable like short1/4, you get a compiler error. Also the same in the line 5, where you want to assign a byte to a short, I would assume it would compile since byte is lower precision than short, and I would think it would perform an implicit casting

Thanks for your help
Tere


find my comments above


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Tere Luna
Greenhorn

Joined: Jul 19, 2011
Posts: 12
Thank you Boris

Your explanation is very clear! Now I'm testing by using only primitive not wrapper, like in the code below



And I get the "Type mismatch: cannot convert from int to short" compiler error in line 2. I thought this could be implicit casting :S

Thanks for your help !
Tere
Boris Mechkov
Ranch Hand

Joined: May 13, 2011
Posts: 72

Tere Luna wrote:Thank you Boris

Your explanation is very clear! Now I'm testing by using only primitive not wrapper, like in the code below



And I get the "Type mismatch: cannot convert from int to short" compiler error in line 2. I thought this could be implicit casting :S

Thanks for your help !
Tere

Hi Tere,

You have to remember this: Any numerical operation results in an INT! Consider this:



If you want to make this work you have two choices:

1. Add an explicit cast - byte b3 = (byte) b1 +b2;
2. Use a compound assignment - b3 += b1+b2;

Compound assignments (+=, -=, *=, /=) add an IMPLICIT cast, so you are safe!.
Hope this helps!
Boris
 
jQuery in Action, 2nd edition
 
subject: Implicit casting
 
Similar Threads
cast of numeric literal int
conversion pblm
Short literal representation
b*=b1 vs b=b*b1
integers initialization - Interesting !!!!