Literal Formats for Integers - Example

This section provides a tutorial example on how to use integer literals in 4 formats: decimal, hex, octal and binary.

Now let's see a tutorial program example on integer literals in decimal, hex, octal and binary formats:

/* IntegerLiteral.java
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
 */

class IntegerLiteral {
   public static void main(String[] a) {
      java.io.PrintStream o = System.out;
      o.println("Decimal Integer Literals:");
      o.println("   int value:              911  = "+911);
      o.println("   int high:        2147483647  = "+2147483647);
      o.println("   long value:      2147483648L = "+2147483648L);
      o.println("   Using _:      2_147_483_648L = "+2_147_483_648L);

      o.println();
      o.println("Hex Integer Literals:");
      o.println("   Hex format:           0x38F  = "+0x38F);
      o.println("   Leading 0:       0x0000038F  = "+0x0000038F);
      o.println("   int max value:   0x7FFFFFFF  = "+0x7FFFFFFF);
      o.println("   long value:      0x7FFFFFFFL = "+0x7FFFFFFFL);
      o.println("   int min value:   0x80000000  = "+0x80000000);
      o.println("   long value:      0x80000000L = "+0x80000000L);
      o.println("   int -1 value:    0xFFFFFFFF  = "+0xFFFFFFFF);
      o.println("   long value:      0xFFFFFFFFL = "+0xFFFFFFFFL);
      o.println("   Using _:        0xDead_Deed  = "+0xDead_Deed);
      o.println("   long value:     0xDead_DeedL = "+0xDead_DeedL);

      o.println();
      o.println("Octal Integer Literals:");
      o.println("   Octal format:         01617  = "+01617);
      o.println("   int max value: 017777777777  = "+017777777777);
      o.println("   long value:    017777777777L = "+017777777777L);
      o.println("   int min value: 020000000000  = "+020000000000);
      o.println("   long value:    020000000000L = "+020000000000L);
      o.println("   Using _:     0200_0000_0000  = "+0200_0000_0000);
      o.println("   long octal:  0200_0000_0000L = "+0200_0000_0000L);

      o.println();
      o.println("Binary Integer Literals:");
      o.println("   Octal format:  0b1110001111  = "+0b1110001111);
      o.println("   int min value:"
         +" 0b1000_0000_0000_0000_0000_0000_0000_0000  = "
         +0b1000_0000_0000_0000_0000_0000_0000_0000);
   }
}

Compile this example program in a command window and run it:

C:\>\progra~1\java\jdk1.8.0\bin\javac IntegerLiteral.java

C:\>\progra~1\java\jdk1.8.0\bin\java IntegerLiteral

Decimal Integer Literals:
   int value:              911  = 911
   int high:        2147483647  = 2147483647
   long value:      2147483648L = 2147483648
   Using _:      2_147_483_648L = 2147483648

Hex Integer Literals:
   Hex format:           0x38F  = 911
   Leading 0:       0x0000038F  = 911
   int max value:   0x7FFFFFFF  = 2147483647
   long value:      0x7FFFFFFFL = 2147483647
   int min value:   0x80000000  = -2147483648
   long value:      0x80000000L = 2147483648
   int -1 value:    0xFFFFFFFF  = -1
   long value:      0xFFFFFFFFL = 4294967295
   Using _:        0xDead_Deed  = -559030547
   long value:     0xDead_DeedL = 3735936749

Octal Integer Literals:
   Octal format:         01617  = 911
   int max value: 017777777777  = 2147483647
   long value:    017777777777L = 2147483647
   int min value: 020000000000  = -2147483648
   long value:    020000000000L = 2147483648
   Using _:     0200_0000_0000  = -2147483648
   long octal:  0200_0000_0000L = 2147483648

Binary Integer Literals:
   Octal format:  0b1110001111  = 911
   int min value: 0b1000_0000_0000_0000_0000_0000_0000_0000
      = -2147483648

Hope you know how to use integer literals properly now.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

Primitive Data Types and Literals

 Data Types Supported in Java

 Integer Data Types

 Floating-Point Data Types

 Logical (Boolean) Data Type

 Literals of Primitive Types

 Literal Formats for Integers

Literal Formats for Integers - Example

 Literal Formats for Floating-Point

 Literal Formats for Floating-Point - Example

 Literal Formats for Characters

 Literal Formats for Character - Example

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version