i keep getting confused in the type casting concept. there are so many
possibilities in which some are valid and the others are invalid, can
someone clear my doubts with the examples below.
What would happen in the following situations?
int x = 5.0/6.0
int x = 30.0/5.0
int x = 5/6
int x = 29/6
int x = 29.0/6
double x = 5.0/6.0
double x = 29/6
double x = 6/29
double x = 29.0/6
double x = 29/6.0
int x = (int) 29/6.0
int x = (int) 29.0 /6
int x = (int) 29/6.0
int x = (int) 29.0 /6
double x = (double) 29/6.0
double x = (double)6.0/29
double x = (double) 29/6
int x = (double) 29/6.0
int x = (double) 29.0 /6
int x = (double) 29/6.0
int x = (double) 29.0 /6
double x = (int) 29/6.0
double x = (int)6.0/29
double x = (int) 29/6
Do we need to use type casting when we change from smaller data type
to bigger data type or when we change from bigger data type to smaller
data type or both ways?
I looked for type casting in my reference packet but it just has the
definition for it and no examples.
> i keep getting confused in the type casting concept. there are so many
> possibilities in which some are valid and the others are invalid, can
[quoted text clipped - 7 lines]
> int x = 29/6
> int x = 29.0/6
...
Why don't you try it and find out?
> Do we need to use type casting when we change from smaller data type
> to bigger data type
The term you want here is "widening conversion".
The hard truth is in chapter 5 of the Java Language Specification (JLS).
<http://java.sun.com/docs/books/jls/third_edition/html/conversions.html>
Going from subtype to supertype is a widening conversion for reference types.
From the JLS:
> Widening reference conversions never require a special action at run time
> and therefore never throw an exception at run time. They consist simply in
> regarding a reference as having some other type in a manner that can be
> proved correct at compile time.
Going from "smaller" to "larger" numeric types is a widening conversion for
numeric primitive types.
From the JLS:
> The following 19 specific conversions on primitive types are called the
> widening primitive conversions:
[quoted text clipped - 5 lines]
> * long to float or double
> * float to double
...
> Despite the fact that loss of precision may occur,
> widening conversions among primitive types never result in a run-time exception (§11).
> or when we change from bigger data type to smaller data type
Depends on whether the "narrowing conversion" is between primitives or
reference types. Again, the JLS is the boss.
> The following 22 specific conversions on primitive types are called the narrowing primitive conversions:
>
[quoted text clipped - 4 lines]
> * float to byte, short, char, int, or long
> * double to byte, short, char, int, long, or float
...
> The following conversion combines both widening and narrowing primitive convesions:
>
> * byte to char
There is also the "identity conversion".
The actual use of the cast operator in a program is to coerce a certain
run-time type. It also forces a compile-time type on the entire expression.
Use it whenever the compile-time type is not what you want at run time, to
force them to match up.
In the case of widening conversions, you usually do not need to cast to the
wider type unless you want to coerce a compile-time type on the expression and
such coercion is not automatic. There are a number of "implicit conversions"
that occur.
> I looked for type casting in my reference packet but it just has the
> definition for it and no examples.
Look for "conversion", "widening conversion", "narrowing conversion",
"implicit conversion" and "explicit conversion" as co-search terms with "Java".
-- Lew
> int x = 5.0/6.0
> int x = 30.0/5.0
> int x = 5/6
> int x = 29/6
> int x = 29.0/6
For 3 out of 5, you get errors (guess which ones!).
> double x = 5.0/6.0
> double x = 29/6
> double x = 6/29
> double x = 29.0/6
> double x = 29/6.0
No errors here.
> int x = (int) 29/6.0
> int x = (int) 29.0 /6
> int x = (int) 29/6.0
> int x = (int) 29.0 /6
Righty-o, 4 out of 4 for errors
> double x = (double) 29/6.0
> double x = (double)6.0/29
> double x = (double) 29/6
Once again, you are errorless!
> int x = (double) 29/6.0
> int x = (double) 29.0 /6
> int x = (double) 29/6.0
> int x = (double) 29.0 /6
4 out of 4 errors.
> double x = (int) 29/6.0
> double x = (int)6.0/29
> double x = (int) 29/6
And error-less here as well.
Out of 24 statements, 11 produce errors: ints need explicit casts from
doubles (narrowing conversion requires casts, see JLS 3 §5.1.3). Another
caveat that you might not be aware of is that
(int) 6.0 / 29 = ((int) 6.0) / 29 NOT (int) (6.0/29) -- casting is a
left-associative operation.
Lew - 05 Mar 2007 03:02 GMT
>> int x = 5.0/6.0
>> int x = 30.0/5.0
>> int x = 5/6
>> int x = 29/6
>> int x = 29.0/6
Any of these would give an error due to the lack of semicolons.
>> double x = 5.0/6.0
>> double x = 29/6
>> double x = 6/29
>> double x = 29.0/6
>> double x = 29/6.0
> No errors here.
semicolons.
>> int x = (int) 29/6.0
>> int x = (int) 29.0 /6
>> int x = (int) 29/6.0
>> int x = (int) 29.0 /6
> Righty-o, 4 out of 4 for errors
However, if semicolons were used properly, only two of these four would fail
to compile.
int x = (int) 29/6.0; // bad
x = (int) 29.0 /6; // good
x = (int) 29/6.0; // bad
x = (int) 29.0 /6; // good
The reason is the interaction between operator precedence, binary promotion
and conversion.
-- Lew
Arne Vajhøj - 05 Mar 2007 03:52 GMT
> However, if semicolons were used properly, only two of these four would
> fail to compile.
[quoted text clipped - 6 lines]
> The reason is the interaction between operator precedence, binary
> promotion and conversion.
It is hardly surprising that the same line
the second time it occurs behaves identical to the
first. I think you intended to write something else.
Arne
Lew - 05 Mar 2007 04:03 GMT
>> However, if semicolons were used properly, only two of these four
>> would fail to compile.
[quoted text clipped - 6 lines]
>> The reason is the interaction between operator precedence, binary
>> promotion and conversion.
> It is hardly surprising that the same line
> the second time it occurs behaves identical to the
> first. I think you intended to write something else.
Taken straight from the OP:
> int x = (int) 29/6.0
> int x = (int) 29.0 /6
> int x = (int) 29/6.0
> int x = (int) 29.0 /6
-- Lew
Chris Smith - 06 Mar 2007 00:11 GMT
> > int x = (int) 29/6.0
> > int x = (int) 29.0 /6
> > int x = (int) 29/6.0
> > int x = (int) 29.0 /6
> Righty-o, 4 out of 4 for errors
Are you giving incorrect answers just for fun? If not, you might take
Lew's advice and try this first.

Signature
Chris Smith