• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit casting

 
Tere Luna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Hama Kamal
Ranch Hand
Posts: 144
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tere Luna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic