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?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
Last edited by hns1984; January 11th, 2012 at 06:54 PM.
Wrong, float literal is a double value, implicitly.
java exception
Last edited by hns1984; January 11th, 2012 at 06:54 PM.
ahh, My bad it gets abit confusing when handling all the difference number types of java.