# why it is essential to use (int) ?

• November 1st, 2011, 11:20 AM
websit
why it is essential to use (int) ?
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?
• November 1st, 2011, 11:27 AM
macko
Re: why it is essential to use (int) ?
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.
• November 1st, 2011, 11:30 AM
hns1984
Re: why it is essential to use (int) ?
Quote:

Originally Posted by websit
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?

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
• November 1st, 2011, 11:33 AM
hns1984
Re: why it is essential to use (int) ?
Quote:

Originally Posted by macko
7.0 = Decimal point number = Float

Wrong, float literal is a double value, implicitly.

java exception
• November 1st, 2011, 11:37 AM
macko
Re: why it is essential to use (int) ?
ahh, My bad :D it gets abit confusing when handling all the difference number types of java.