I want to know that when we typecast the int to double does the actual result where int is stored is get changed or increased? because int is of 4 bytes(lets assume) and when we it get typecast to double which is of 8 bytes(assumption) then does the size also increased now to store the value of double? And please go easy on me if it is a stupid question?
Java compiler will automatically convert lower data type(int) to higher data type(double). Since higher data type has wider range and greater memory size than lower data type. It is called as implicit typecasting.
Short answer is "no" - the range of values an int can represent and that a double can represent are implementation defined - but a double certainly cannot support every integral value in the range it can represent.
Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.
Double wrapper class valueOf() method converts int value to double type. It returns the Double-object initialized with the provided integer value. Here, d = variable to store double value after conversion to double data type.
Casting doesn't affect the variable and the corresponding memory. It's just and indication for the compiler how to interpret the bits the reside at the given location.
In Marcus'es example:
int i = 12;
double d = (double)i;
d
is a new variable in a completely new location. The original value, i
, is not affected. However, if you start playing with pointers then you have to be careful:
int i = 12;
double *p = &i;
Now, if double
is 8 bytes wide then note that by using p
you/compiler assume that it points to a memory location that has 8 bytes of memory allocated for the variable that it's pointing to. This is however not true, because in fact it's pointing to i
, which is only 4 bytes wide (assume that int
is 4 bytes wide).
EDIT
This is a (relatively) recent edit motivated by OPs request for clarification in the comments. The OP wants to know:
what happens to the memory address of sum when it get typecast does now typecasted value of sum stored somewhere else in the memory
and here's the code:
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
There's actually quite a lot going in the above three lines of code. However, the important thing here is that the variable sum
is not being modified at all. It's only being used as an argument in addition. For the sake of adding two variables you only need the corresponding values. Before adding, the compiler will most likely copy the two variables into registers. Because you're casting to a double, the compiler will very likely store the value of sum
in a 64 bit wide register (assume that double is 64 bit wide) and that's it. However, bear in mind that this is implementation specific and will vary from one compiler to another. It's not something that's specified by the C standard.
At this point, if you want to understand more you're best of compiling to Assembly and trying to understand that. Hope this helps!
That is the same question as "in a = b + c * d;
where is the intermediate result of c * d
stored?".
Simple answer: in a temporary variable manged by the compiler. This will most likely be a register. The language standard does not define this - why should it actually?
The typecast is actually similar to -b
(b negated). This would also have to be stored somewhere prior to being used.
Note that the original value is not modified and the receiving variable has to be of the proper type anyway (that is one reason why you cannot typecast the receiving variable).
The int to double/float conversion is done by the compiler implicitly, so there is no need for an explicit cast - and it should not be used in such cases, as it will hide problem arising by later changes to the types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With