Java Tutorial : Understanding Java Floating Literals: Decimal, Hexadecimal & Scientific Notation Explained
StudyECart Technologies Pvt Ltd
Take this quiz to test your understanding of Floating Literals in Java.
Take the Quiz
Introduction
In Java, floating-point literals represent decimal numbers with a fractional part. These literals can be expressed in different formats, such as Decimal, Hexadecimal, and Standard literals. Let's dive into these formats with examples.
Decimal Floating Literals
Decimal floating literals are written as numbers with a decimal point. Here's an example:
float decimalLiteral = 123.45f; double anotherDecimalLiteral = 6789.1234;
In the above example, 123.45f is a float literal, and 6789.1234 is a double literal.
Hexadecimal Floating Literals
Hexadecimal floating literals are a lesser-known way to represent floating-point numbers in Java. They start with 0x or 0X followed by a hexadecimal number and a p or P to indicate the exponent.
double hexLiteral = 0x1.0p3;
Here, 0x1.0p3 represents the decimal number 8.0. The p3 indicates multiplying the value by 23.
Standard Floating Literals
Standard Floating Literals Are The Most Common And Straightforward Way To Write Floating-point Numbers In Java. You Simply Write The Number With Or Without a Decimal Point
double standardLiteral1 = 10.0; double standardLiteral2 = 5.0e2; // 5.0e2 is equivalent to 500.0
In the example, 10.0 is a regular floating-point literal, while 5.0e2 is a scientific notation where e2 means "times 10 to the power of 2."
Java Program: Floating Literals (Decimal, Hexadecimal, Standard)
This program demonstrates the use of different types of floating literals in Java: decimal, hexadecimal, and standard scientific notation.
Code Example
public class FloatingLiteralsExample { public static void main(String[] args) { // Decimal floating literal float decimalFloat = 123.45f; double decimalDouble = 6789.1234; // Hexadecimal floating literal double hexDouble = 0x1.0p3; // Equivalent to 8.0 in decimal // Standard floating literal with scientific notation double standardDouble = 5.0e2; // Equivalent to 500.0 // Printing the values System.out.println("Decimal float value: " + decimalFloat); System.out.println("Decimal double value: " + decimalDouble); System.out.println("Hexadecimal double value: " + hexDouble); System.out.println("Standard double value (scientific notation): " + standardDouble); } } (code-box)
Explanation
- Decimal floating literal: 123.45f is a float, and 6789.1234 is a double.
- Hexadecimal floating literal: 0x1.0p3 represents 8.0 in decimal. The p3 means multiplying the hexadecimal value by 2^3.
- Standard floating literal: 5.0e2 is written in scientific notation and represents 500.0.
Decimal float value: 123.45 Decimal double value: 6789.1234 Hexadecimal double value: 8.0 Standard double value (scientific notation): 500.0
Key Points to Remember
- Floating-point literals can be float or double. By default, they are double.
- Use a suffix f or F for float literals.
- Hexadecimal literals use 0x or 0X with p for the exponent.
- Scientific notation uses e or E to denote the exponent.
Understanding floating-point literals in Java is essential for precise numerical computations. Practice using these different literals to get comfortable with their syntax and usage.
Call to Action
Experiment with different floating-point literals in your Java programs. Try converting between decimal and hexadecimal literals, and explore how scientific notation works in your code!
Previous Session Next Session
Relevant Links
- How to Install JDK from Download to Install: A Step-by-Step Guide
- Setting Up Your Java Development Environment: A Beginner’s Guide