Notes about numeric literals in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 21 August 2019   |   Print Email
In this article, I will help you understand about numberic literals in Java. Consider the following code:
int size = 100; float weight = 52.5f; double amount = 1000000;
Here, the numbers 100, 52.5f, 1000000 are called numeric literals.
1. Integer literal is implicit integer value
By default, the Java compiler treats all integer literals as of type int. Consider the declaration of a variable of type byte:
byte number = 200;
You will get a compile error: possible loss of precision
Why? It’s because the compiler sees the literal 200 is an integer value, so it attempts to convert that value to a byte variable. However, 200 doesn’t fit in the range of a byte is from -127 to 128 so that the compiler complains.
But it’s fine if you declare a byte variable like this:
byte number = 100;
Because 100 fits within the range of a byte. Remember there is an implicit conversion from int to byte in this case.
When you declare a long variable like this:
long x = 2000000;
Then there is an implicit conversion from int to long. Since an int value always fits into a long variable, the compiler doesn’t complain. You can add the suffix l or L to explicitly denote a long literal:
long x = 2000000L;
2. Decimal literal is implicit double value
By default, the Java compiler treats all decimal literals as of type double. Consider the following declaration of a variable of type float:
float a = 36.5;
The compiler also complains: possible loss of precision
Why? The compiler sees the literal 36.5 as a double value, and conversion from a double value to a float variable would result in possible loss of precision. In this case, you the suffix f or F to force the conversion:
float a = 36.5f;
The compiler will be happy.
3. Representing numeric literals in other bases
Besides the default base is decimal, Java allows us to write numeric literals in binary, octal and hexadecimal bases using the following prefixes:
- 0b: indicates a binary literal (available since Java 7).
- 0: indicates an octal literal.
- 0x: indicates a hexadecimal literal.
Let’s see an example:
int decimal = 100; int binary = 0b1100100; int octal = 0144; int hexadecimal = 0x64;
4. Underscores can be used in numeric literals
From Java 7, you can use underscores in numeric literals to increase readability. For example:
int million = 1_000_000; long billion = 1_000_000_000; float pi = 3.141_592_653f;
More examples and rules:
http://www.codejava.net/java-core/the-java-language/java-7-underscores-in-numerical-literals
5. Conversion from one numeric type to another:
Consider the following example:
int x = 1000; long y = x;
This is okay because the size of an integer variable fits a long one. But if you do vice-versa:
long y = 1000; int x = y;
The compiler will issue an error possible loss of precision because the size of a long variable cannot fit in an integer one. In this case, you can use an explicit conversion like this:
long y = 1000; int x = (int) y;
This is called casting.
However, be careful because you can loss precision since the value on the right side may be larger than then the one on the left side. Only use this conversion if you are sure that the value on the right side would be fit into the primitive type on the left side.
So the rule is that you have to use an explicit cast when assign the bigger primitive type to the smaller one. Otherwise the compiler does the implicit cast for you.
Related Topics:
- Notes about Arrays in Java
- Notes about execution control statements in Java
- Notes about operators in Java
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand abstraction in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Comments