This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting, Narrowing and Widening

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone kindly tell me if I have this right? I am trying to understand what happens during an assignment statement that involves any of the primitive data types and when a cast is implicit or explicit.
byte---short---int---long---float---double
char
If I move from left to right on the above, am I widening my primitive types? I know that I have to explicity cast to assign a double to an int, but I'm trying to understand what I'm reading in the different java tutorials about this topic.
can anyone be of assistance on this?
thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, moving from left to right is widening because each successive type uses more bytes for storage than the previous one. Conversely, right to left is narrowing because the number of bytes gets smaller.
When an assignment is narrowing the type (I'm not sure if that's the correct terminology), some data my be lost. For example, a very large number stored in a long may be larger than the maximum for an int, so assigning a long to an int has the possibility of losing data. This is why type-casting is required when moving from the right to the left in your list. This tells the compiler that you are certain that the value is small enough for the new type.
On the other hand, widening operations don't have the potential to lose data, so an implicit cast can be performed.
HTH
Layne
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thanks, this did help.
What happens then when the primitive types are char, byte and short? Is everything promoted to type int first implicity?
leo
 
celayne heaton
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what you're trying to do. Many operations will such as the arithemetic operations will convert primitives shorter than an int to int (beware 'op=' eg += which retains the original datatype so
> byte b = 10;
> b+=20;
retains b as type byte
but:

> byte a = 10;
> byte b = a+10; // compiler error cos converts a to int then returns the result as an int. Therefore need to cast result to a byte to compile correctly)
However:
> void MyFunc(byte b){}
takes a byte without converting it to int.

Hence, depends what you're trying to do...
Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic