long can't have decimals. int = 32-bit integer long = 64-bit integer float = 32-bit floating point double = 64-bit floating point If you want to (visually) understand how floating point works, then you should read and experiment here . Answer from Nightcorex_ on reddit.com
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The signed long has a minimum value ... divideUnsigned etc to support arithmetic operations for unsigned long. float: The float data type is a single-precision 32-bit IEEE 754 floating point....
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
🌐
Sololearn
sololearn.com › en › Discuss › 2059751 › what-s-the-difference-between-float-double-and-long-variables
What's the difference between float , double and long variables? | Sololearn: Learn to code for FREE!
Long is similar to int but has 8 bytes of storage and cannot have decimal point values, whereas float is of 4 bytes and can store decimal point values but with lower precision.
🌐
Quora
quora.com › What-is-the-difference-between-double-long-float-and-int-variables-in-Java-Which-one-should-be-used-for-storing-large-numbers-more-than-two-digits-Why
What is the difference between double, long, float, and int variables in Java? Which one should be used for storing large numbers (more than two digits)? Why? - Quora
Answer: In Java, int and long are primitive integer datatypes. They store integers as two‘s complement numbers. Ints use 32 bits and have a range from -2^{31}=-2147483648 to 2^{31}-1=2147483647, longs use 64 bits and have a range from -2^{63}=-9223372036854775808 to 2^{63}-1=9223372036854775807.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjava › how does a long fit into a float?
r/learnjava on Reddit: How does a long fit into a float?
August 1, 2022 -

I was just trying to explain data type rankings and loss of precision to a class and realized that I was probably explaining it wrong. I typically only work with double and int's so this has never really occured to me.

The ranking from lowest to highest goes, byte < short < int < long < float < double. I typically just say it's because a byte can fit into a short(1 byte < 2 bytes) and an int can fit into a double(4 bytes < 8 bytes) but then it occured to me that a long is also 8 bytes yet it can fit into a float which is only 4 bytes.

I'm guessing it has something to do with the fact that floats are floating-point numbers and store 8 decimal places...So if the biggest long is something like 10 quintrillion(?) and that needs 8 bytes of memory to store it, how can a float can go to somethingx10^38 with decimal points still only need 4 bytes of memory to store it?

Top answer
1 of 5
14
The ranking from lowest to highest goes, byte < short < int < long < float < double. This is inaccurate. Floating point numbers and integers represent data differently. You can't really place them on the same scale. It's more correct to say: For integers, byte < short < int < long For floating point numbers, float < double The whole idea with a floating point number is that the decimal point "floats", meaning that, for very small numbers, it can store a lot of decimal places, but it can also store very large numbers, but with fewer decimal places. So you can store, say, 0.000001 and 100000000. What you can't do is store a very large number with a very small fractional component: for example, 10000000.000001. Here's an example showing that, when performing float point arithmetic, it's possible that A + B = A. The precise details of how floating point numbers are represented and how math is performed on them are quite interesting. But overall the thing to keep in mind is that: Floating point arithmetic always runs the risk of loss of precision, frequently in ways that you don't anticipate. Small losses of precision may not seem important, until your space probe crash lands or misses Mars entirely... (This is also true for integer arithmetic, of course, because any fixed amount of computer memory can only store a fixed amount of information. But failure modalities with integer arithmetic are somewhat easier to predict.)
2 of 5
4
In a nutshell: floats are weird. You can’t judge the precision of a float by number of bits of its representation - they are designed to be the most memory efficient at encoding non-integers but it comes at a price: they aren’t based on decimal numbers and will have most decimal numbers approximated and some (minority) precisely represented. So. Treat them as never precise.
🌐
Coderanch
coderanch.com › t › 260406 › certification › long-fit-float
How does a long fit in a float? (OCPJP forum at Coderanch)
If you run code like the following, you'll see lots of numbers that are not translated with 100% accuracy from long to float. What I find slightly amusing is that Java will give a "loss of precision" error when translating a float to a long, but will give no warning of "loss of precision" when translating from long to float.
🌐
Coderanch
coderanch.com › t › 235941 › certification › long-float-double
long Vs float/double? (OCPJP forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... double = 64 bits --Chris --Am I a Ranch Hand Yet [ January 09, 2002: Message edited by: Chris Graham ] ... Yes!!! Though it may sound a bit strange, a long can be implicitly converted to float or doube.
🌐
Medium
medium.com › @t.dasanthaedirisinghe › understanding-javas-int-float-and-boolean-data-types-1b7b6cb6609
Understanding Java’s int, float, and boolean Data Types | by T_DasanthaEdirisinghe | Medium
April 14, 2023 - Note that you should end the value ... fractional numbers. The float data type is sufficient for storing up to six to seven decimal digits, while the double data type can store up to 15 decimal digits....
Top answer
1 of 2
10

Converting long to float is a widening primitive conversion. Java allows it without error because...it's defined that way. From the link:

19 specific conversions on primitive types are called the widening primitive conversions:

  • byte to short, int, long, float, or double

  • short to int, long, float, or double

  • char to int, long, float, or double

  • int to long, float, or double

  • long to float or double

...

A widening primitive conversion from int to float, or from long to float, or from long 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).

(My emphasis.)

Why allow it without requiring an explicit cast? In general, only James Gosling and others there at the time can answer that sort of question. For the rest of us, the answer is: Because that's what the specification says.

However, I'll note that even though precision is lost, overall magnitude is not, which I'd wager is why it's allowed. float can hold very large values imprecisely. So for instance:

class Demo {
    public static void main(String[] args) {
        long l = Long.MAX_VALUE;
        float f = l;
        System.out.println(l);
        System.out.println(f);
    }
}

Run that and you get:

9223372036854775807
9.223372E18

9.223372e18 is 9223372000000000000. Compare:

9223372036854775807 - the long value
9223372000000000000 - the float value

This is because float can sacrifice precision for range of value, because it stores a base value that it raises to an exponent. From Wikipedia's article on the IEEE-754 binary32 format (which is what Java's float is):

...an IEEE 754 32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.402823 × 1038...

3.402823 × 1038 is:

340,282,300,000,000,000,000,000,000,000,000,000,000

...but the highest integer it can store such that you can add 1 and not lose precision is just 224-1 (16,777,215). It can store 16,777,216, but not 16,777,217. So 16,777,216 + 1 is still 16,777,216:

class Demo {
    public static void main(String[] args) {
        float f = 16_777_216f;
        System.out.println(String.format("%8.0f", f));     // 16777216
        System.out.println(String.format("%8.0f", f + 1)); // 16777216 (still)
    }
}

That's because it's now storing the base value divided by 2 with an exponent of 2, so it can't store odd numbers anymore. It gets worse the higher you get, being able to only store multiples of 4, then of 8, then of 16, etc.

Contrast with the maxiumum value of a long, which is 263-1, which is "only":

9,223,372,036,854,775,807

But unlike float, it can represent each and every one of those integers precisely.

2 of 2
-3

There is something like implicit typecasting. Javajava casts it automatically if you will not lose information. From float to long you could lose all behind the floating point so there will be no implicit cast because normally you do not want to lose this information.

🌐
CodingNomads
codingnomads.com › what-are-java-primitive-types
What are Java data types? Boolean, char, byte, short, int, long, float, double.
// this is to inform the JVM that ... number is a word for a number containing fractional components. Java defines a float as one of two floating-point types....
🌐
Reddit
reddit.com › r/learnprogramming › [c] how to know when to use a double vs float or a long vs int?
r/learnprogramming on Reddit: [C] How to know when to use a double vs float or a long vs int?
November 28, 2017 -

Sorry if this seems like a basic question, but I've been learning C over the past several weeks. In some of the tutorials I've been using, I've noticed some of the instructors just use int/float while others tend to default to double when declaring floating point variables.

Is there any sort of best practice in terms of deciding what size variable to use?

🌐
Quora
quora.com › Whats-the-difference-between-long-and-Long-int-and-Integer-and-float-and-Float-in-Java-Which-is-suitable-for-which-situation
What's the difference between long and Long, int and Integer and float and Float in Java? Which is suitable for which situation? - Quora
Answer (1 of 4): While everyone has answered it correctly that one is primitive data type and other is wrapper class for the primitive data type. Wherever you need java class reference such as in generics you will need wrapper class. If you need to use Java collections or map data structure, you ...
🌐
Quora
quora.com › What-is-the-difference-between-an-int-a-long-a-double-and-a-decimal-in-Java
What is the difference between an int, a long, a double and a decimal in Java? - Quora
Answer (1 of 5): There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. int * Int data type is a 32-bit signed two's complement integer. * Minimum value is - 2,147,483,648 (-2^31) * Maximum value is 2,147,483,647(incl...