• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Vaibhav Aggarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am not able to understand the following code :-

class Demo {

public static void main(String[] args){
long m =20l;
float n =30f;
n=m;
System.out.println("The value of n is "+n);
}
}

Problem is : long is 64 bits and float is 32 bits, but when we assign a long to float it doesn't give a error, while when we want to do a narrowing casting we should have to do explicit cast.

Vaibhav
 
Dhaval Shah
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all the numeric primitive types, when you downcast a number (in this case from long to float) it looses its actual value. In this case you are assigning value contained in long ( 64 bit ) number to a float (32 bit )variable. So the last 32 bits are lost and thats why it allows you to downcast in case of numeric types without an explicit cast.

However that is not the case when you assign number to char variable.

i.e
int i = 10;
char c = (char) i; // will allow
char c = i; // will give compilation error
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float stores numbers in a completely different way than long. The type float can store all the numbers that are in the range of long, but with limited precision.

Converting a long to a float is a widening primitive conversion, so that no cast is necessary.

The Java Language Specification says, in section 5.1.2:
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (�4.2.4).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhaval shah:
For all the numeric primitive types, when you downcast a number (in this case from long to float) it looses its actual value. In this case you are assigning value contained in long ( 64 bit ) number to a float (32 bit )variable.

This is not really correct in this case; a float stores numbers in the single-precision IEEE 754 format, while a long stores numbers in two's complement format.

In the conversion from long to float, it is not so that simply the lower 32 bits are discarded. Converting a long to a float is more complicated than that. But you do loose precision.
So the last 32 bits are lost and thats why it allows you to downcast in case of numeric types without an explicit cast.

This is not correct at all. Narrowing primitive conversions (from a larger to a smaller number of bits) normally require a cast, precisely because bits are thrown away. Only widening primitive conversions do not require a cast, because the original value will fit in the result without throwing away bits.
[ September 19, 2007: Message edited by: Jesper Young ]
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A long can contain any value between -2^63 and 2^63-1 (-9223372036854775808 and 9223372036854775807).

A float can hold approx any value between 2^-126 and 2^127. As you can see, any value that can be represented as a long can be represented as a float, more or less. You just may loose significance.
 
Vaibhav Aggarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Dhaval,Jesper and Rob . I had no idea that, long to a float is a widening primitive conversion and that both of them store numbers in a defferent format.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic