Java: Number Literals

By Xah Lee. Date:

Numbers can be written in Java as decimals, hexidecimal, or octal.

To input in octal notation, prefix with 0.

To input in hexdecimal notation, prefix with 0x. Letters A to F can be either lower case or upper case.

A integer literal is of type “long” if it is suffixed with L or lower case l; otherwise it is of type “int”.

class Test {
    public static void main(String[] arg) {
        int x1 = 10; // 10 in decimal
        int x2 = 012; // 10 in octal
        int x3 = 0xA; // 10 in hexdecimal

        System.out.println( x1 );
        System.out.println( x2 );
        System.out.println( x3 );
    }
}

If a number is written with the decimal point, it is automatically of type “double”.

You can append f to a number to indicate it's a type “float”. Similarly, d for “double”.

Examples of float literals:
1e1f    2.f     .3f     0f      3.14f   6.022137e+23f

Examples of double literals:

1e1     2.      .3      0.0     3.14    1e-9d   1e137

Reference

http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html

Java

  1. Install
  2. javac java jar javadoc

Java Basics

  1. Print String
  2. Strings
  3. Power Function
  4. Java Types and Type Conversion
  5. Function
  6. Arrays

Class, Inheritance

  1. extends
  2. Constructor
  3. Constructor Return Type
  4. Extending a Class that has Explicit Constructors
  5. static
  6. this
  7. super
  8. Access Specifiers

Abstract class, Interface

  1. abstract
  2. Interface

Package

  1. Package

Misc

  1. Collection, Map
  2. Read/Write File
  3. Convert File Encoding
  4. Complex Numbers in Java
  5. Unicode
  6. Number Literals