Nant
Nant

Reputation: 569

Need explanation of casting in Java. Which way is the best?

I have the following code:

double d = 23d;
byte b = 52;

int result;

I am trying to store the result of d + b in result and I have to cast in order to do that, however I have found two ways to do that and I don't know what the difference is.

//First method
result = (int) d + b;

//Second Method
result = (int) (d+b);

What is the difference?

Thanks

Upvotes: 2

Views: 95

Answers (4)

Ali Azam
Ali Azam

Reputation: 2115

Thxs! below explanation of my understandings may help you.

1st Method Simplification:
b is implicitly converted to double and added to d. then produce double value as a result and allocates a memory to store this double value first which is larger. Finally, casting to this value makes the result as int.

double d1 = d + b;
result = (int) d1;  

2nd Method Simplification:
First b is implicitly converted to double and added to d then casting the resulting value into byte. As a result, it allocates a memory to store this byte value which is comparatively lower. Finally, this byte value is implicitly converted to int and produces the result as int.

byte b1 = (byte)(d + b);
result =  b1;

So we conclude that the second method is the best one because it allocates the lower memory spaces than the first method.

Upvotes: 0

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

First of methods casts only the first operand, which in this case is d. Second method casts the result of the sum of d and b.

Upvotes: 4

Dici
Dici

Reputation: 25950

For most values, both expressions will return the same. However, take a look at this code that will illustrate the difference:

double d = 1 / (1 - (0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1)); // equal to 9.007199254740992E15
byte b = 1; 

System.out.println((long) d + b); // prints 9007199254740993
System.out.println((long) (d + b)); // prints 9007199254740992

Now what happened ?

  • in the first expression, d is converted to an long first and then added to b
  • in the second expression, d + b is calculated as a double value and then converted to a long

Why does it matter ?

I did not pick d randomly. This is a number for which d + 1 is an integer but is impossible to represent as a double because it's a large number and double has limited precision. As a result, for this particular number d + 1 == d ! However, long has infinite precision for all integers that are small enough to fit in 64 bits. Therefore, converting d as a long before calculating d + 1 gives us the real sum without precision error.

PS: I used long instead of int because this number would not fit in an int and woud overflow. The two expressions would still return two different results but it would make my point less clear.

Upvotes: 3

user2345
user2345

Reputation: 3227

In the first case, d is casted to int, and then added to b

result = (int) d + b;
// is also equivalent to
result = ((int) d) + b;

This is possible as adding a byte and a double can be done without any cast. On the other hand, int result = (int) b + d would fail, as this results in a int being added to a double, thus resulting in a double, that cannot be put in an int.


In the second case, the adding is done first, and then the result is casted to an int.

Upvotes: 4

Related Questions