wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Casting, Narrowing and Widening Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting, Narrowing and Widening" Watch "Casting, Narrowing and Widening" New topic
Author

Casting, Narrowing and Widening

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
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


Thanks, leo
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
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

Joined: Apr 22, 2003
Posts: 1
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Casting, Narrowing and Widening
 
Similar Threads
Primitive data type
Is This a Widening or an Upcast?
Ternary operator curious problem
Question about primative return types
Instances of Superclasses