int x;
x= (int) (7.0/2);
I need to know why it is essential to use (int) in the statement above?
What is it called?
How it would be without (int), is it correct?
Printable View
int x;
x= (int) (7.0/2);
I need to know why it is essential to use (int) in the statement above?
What is it called?
How it would be without (int), is it correct?
Its because your trying to use a float... 7.0 is a float value, But stating (int) is just like parsing it to an int from a float which it currently is.
7.0 = Decimal point number = Float
7 = no Decimal Point = int
you are saying 7.0 divided by 2.. although your saying that the answer will equal the int x.
So long story short.. you cannot use a float value as an int unless u do either (int) or parse the float.
Because the literal "7.0" is implicit double value, so the result of 7.0/2 will be a double number, theno you need to put a type cast (int) to convert the result into an integer which is assigned to the variable x which is an integer.
java exception
Wrong, float literal is a double value, implicitly.
java exception
ahh, My bad :D it gets abit confusing when handling all the difference number types of java.