Java Literals

Introduction

A constant value in Java is created by using a literal.

For example, here are some literals:

100               //int literal
98.6              //floating-point value
'X'               //char literal
"This is a test"  //String literal

A literal can be used in anywhere a value of its type is allowed.

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 .jav a2  s . c  om*/

        //binary
        int bin1 = 0b1100;
        short bin2 = 0B010101;
        short bin3 = (short) 0b1001100110011001;

        System.out.println(bin1);
        System.out.println(bin2);
        System.out.println(bin3);


    }
}



PreviousNext

Related