Java Tutorial : Understanding Java Floating Literals: Decimal, Hexadecimal & Scientific Notation Explained


Test Your Knowledge

Take this quiz to test your understanding of Floating Literals in Java.


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.

Output:

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.

Conclusion

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!

Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad