简体   繁体   中英

Casting to Integer in Java when using type double

I have this simple code and I need to understand why the right side of the equation has been casted to Integer before getting the result in type double . The answer here is 1.0 so I don't understand why it's not 1.5 .

int x = 2;
double y = 1 + 1/x;
System.out.println(y);

simply update your code as following since you are dealing with doubles.

int x = 2;
double y = 1 + 1.0/x;
System.out.println(y);

Notice: double y = 1 + 1/x;

Modify: double y = 1 + 1.0/x;

You'll get 1.5

here x is an int and 1 is also an int so calculation is done in int and so you are getting the answer as 1.0. To get as 1.5 you have to typecast 1 to double as 1.0. Then you will get as 1.5.

I think what you meant was: why the value of 'x' on the right-hand side has not been ' promoted ' to a double?

First, change the second line to :

double y = 1 + (double) 1 / x;
//or
double y = 1 + 1.0 / x;



Details:

Simply for most computers to perform arithmetic operations operands must be of identical data types.

The cast operator (double) performs explicit conversion of its operand to double, then the variable ' x ' will be implicitly converted to a double which is called promotion .

also in the second line 1.0 is already a floating-point constant so you can guess that this is the same as before, that is a promotion to the variable 'x' will occur.

What you have done in your code is called Integer Division meaning that the fractional part of the division will be lost (truncated). Note that the fractional part is lost before it is assigned to the variable y.

You may have some intuition now on why we didn't write the code like that:

double y = 1.0 +  1 / x;

clearly, (1 / x) is still integer division ...hope that helps!

Like user markspace noted in a comment, the integer division happens because / has a higher order of precedence than = , which means that the division takes place first in the statement.

double y = 1 + (1/2); // Both 1 and 2 are integers at the time of operation.

// The above equation is the same as

double y = 1 + 0; // 1/2 equals 0 because integer division just keeps the integer value
                  // and gets rid of the value's decimal part.

Similar to what the other answers have shown, you can do something like one of the following:

double y = 1 + 1.0/x;
// Or
double y = 1 + 1/(double)x;

Just for simplicity, when I use integer literals when assigning double values, I just make all the integers have the .0 part. For example, instead of 1 , I would always put 1.0 . That way, you will never accidentally get unexpected results because of unwanted integer division.

Check out this page in the Java doc for the order of precedence for operators in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM