12 as a constant in java is an int.

The reason long l = 12 compiles is that it is automatically widened to a long.

EDIT: Regarding your comment, there is nothing wrong with automatic widening, use whatever makes your code clearer, but just be aware of what is going on when you do math on primitives. For example:

int i = 1213;
long l = 112321321L * i;

The long will have a very different value if you don't explicitly put that first number as a long because Java will treat it as integer math, and cause an overflow.

Answer from Yishai on Stack Overflow
🌐
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....
🌐
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.
🌐
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
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › how-to-convert-float-to-long-in-java.html
How to convert float to long or int data type in Java? Example
This is a more structured way as other ways are simply to cast a float to long or int to get rid of decimal points. You can also write the Java program by following these tips to convert a float value to an int by replacing the long method with their int counterpart.
🌐
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....
🌐
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 ...
🌐
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 › 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...
Top answer
1 of 4
15

This is because of the 'most-specific' rule in JLS #15, which in turn refers to JLS #4.10, which in turn refers to #4.10.1, which states:

The following rules define the direct supertype relation among the primitive types:

  • double >1 float

  • float >1 long

  • long >1 int

  • int >1 char

  • int >1 short

  • short >1 byte

where "S >1 T" means "T is a direct subtype of S", as per JLS #4.10 immediately above this section.

So in this case, in the absence of a direct match on long, and before looking at auto-boxing, the compiler chooses the nearest available supertype, which is float, by the rules above.

2 of 4
7

Quote from JLS:

The process of determining applicability begins by determining the potentially applicable methods (§15.12.2.1).

The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are:

  1. The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.

  1. The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase. ...

Edit: about choosing float instead of double:

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

The first phase of overload resolution will choose two of these four methods

private static void foo(double aDouble) 
private static void foo(float aFloat) 

because first phase doesn't permit boxing/unboxing (Long) and you cannot pass long to method with int parameter without explicit casting. Than the most specific method will be choosen. In this situation float method would be interpreted as most specific than double.