RobTheRipper
RobTheRipper

Reputation: 27

does casting variables change the value?

When casting a Double to Int, lets say

 double X = 2.5; 
 int Y = (int)X;

can i then use the Double X later, and it still contain the .5? or is the value after the decimal lost forever? can i add onto the Double X, say by .1 increments, and keep casting the value to int Y as a int, but have the int Y only take whole value? say it'll take the value 2, 10 times, and then 3 10 times, and so on.

and does it work the same with arrays? when casting double arrays to int, is the value changed in the array?

thank you.

Upvotes: 1

Views: 1088

Answers (3)

Castiblanco
Castiblanco

Reputation: 1198

It doesn't change the value, because you are doing it:

 int Y = (int)X;

So, you're making things in Y and use for this purpose the variable X, but you never change it. BTW with this can of stuff you can easily write a little program to see what happen.

Upvotes: 1

Taylor
Taylor

Reputation: 4087

First off, Double and double mean different things. Double is a wrapped object and trying to cast is as an Integer will fail.

double and int are primitives, and casting from double to int will not change the value in the double, so what you have should work.

Upvotes: 1

javadev
javadev

Reputation: 1669

Casting X to int in a double value X will not remove the floating points from the X.

Upvotes: 1

Related Questions