Writing Readable Numeric Literals - Java Language Basics

Java examples for Language Basics:Primitive Types

Introduction

Use underscores in place of commas or decimals in larger numbers to make them more readable.

underscores cannot be placed at

  • the beginning or end of a number,
  • adjacent to a decimal point or floating-point literal,
  • prior to an F or L suffix, or
  • in positions where a string of digits is expected.

Demo Code

public class Main {

    public static void main(String[] args) {
        int million = 1_000_000;
        int billion = 1_000_000_000;
        float ten_pct = 1_0f;
        double exp = 1_234_56.78_9e2;
        System.out.println(million);
        System.out.println(billion);
        System.out.println(ten_pct);
        System.out.println(exp);//from w w w .j  a v  a2s.  co  m
    }
}

Result


Related Tutorials