aspose file tools*
The moose likes Java in General and the fly likes float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable. " Watch "float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable. " New topic
Author

float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable.

sree hareesh
Greenhorn

Joined: Nov 16, 2011
Posts: 11
float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable. how it is possible?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Why are you using floats at all?

The reason you can assign it is that it counts as a widening primitive conversion, which the language permits implicitly. Even though there is likely to be loss of precision. I think widening may refer to the range rather than the precision; every valid long value can be mapped to a float value, even though several longs can map to the same float.
If you try it the other way round, there are float values which are completely outwith the range of longs, so that counts as a narrowing conversion and you need an explicit cast. Anyway, the JLS link I gave you is quite clear.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

when using java, there is no specification as to how many bits (bytes) a type utilizes.

Two majors questions:

Why do you think this is important?

Where did you git for perception?

-steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Steve Fahlbusch wrote:when using java, there is no specification as to how many bits (bytes) a type utilizes.




JLS 4.2 Primitive Types and Values:
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units


JLS 4.2.3 Floating Point Types, Formats, and Values:
The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).



Although the physical size in a given JVM implementation may be larger than the sizes mentioned above, the various types must occupy at least as many bits or bytes as indicated. More to the point, though, the types must behave as though they were of those sizes, and that directly drives the OP's question.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

McDonalds used to have signs in front of many of their stores, telling how may people they has served. I remember when they use to say something like "350 million served". The number kept going up, and eventually it was changed to "2 billion served", or whatever.

They went from a 3-digit number (350) to a 1-digit number (2) - because there is another part that tells you the magnitude. Some accuracy has been lost, but you could change the 350 into a 1-digit number by saying ".3 billion". or if you'd prefer... "3 onehundredmillion".

That's more or less how floating point numbers work in just about any computer language. You have the "value" part, and the "magnitude" part. When you convert from on to the other, some accuracy is lost, but it is often possible.

Further, not every number can be stored. Just like on the McDonald's sign, they could store 350 million, then 351 million...missing 999,999 number in between. When they converted to billions, they could go from 2 billion to 3 billion, missing quite a few numbers in between.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable.