This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Vaibhav Aggarwal
Greenhorn

Joined: Sep 12, 2007
Posts: 8
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

Joined: Jun 10, 2005
Posts: 56
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


Thanks and Regards,<br />Dhaval Shah<br />SCJP 1.4<br />SCWCD 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vaibhav Aggarwal
Greenhorn

Joined: Sep 12, 2007
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting
 
Similar Threads
What is the retun Type?
Implicit Casting
Casting
Assigning long value to float?
Long to Float conversion