Java - Data Types long type

What is long type?

long data type is a 64-bit signed Java primitive data type.

long type is used to store whole numbers which may exceed the range of the int data type.

The range of long type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-2^63 to 2^63 - 1).

All whole numbers in the range of long are called integer literals of long type.

Since there are overlap between int type literals and long type literals, an integer literal of type long always ends with L (or lowercase l).

25L is an integer literal of long type whereas 25 is an integer literal of int type.

The following are examples of using a integer literal of long type:

long num1 = 0L; 
long num2 = 400L; 
long num3 = -35561233L; 
long num4 = 234234389898L; 
long num5 = -105L;  
long num6 = -1L;  

Demo

public class Main {
  public static void main(String[] args) {
    long num1 = 0L; 
    long num2 = 400L; 
    long num3 = -35561233L; 
    long num4 = 234234389898L; 
    long num5 = -105L;  
    long num6 = -1L;  
    System.out.println(num1);/* w w  w .  java2 s. c  o  m*/
    System.out.println(num2);
    System.out.println(num3);
    System.out.println(num4);
    System.out.println(num5);
    System.out.println(num6);
  }
}

Result

Integer literals of long type can also be expressed in octal, hexadecimal, and binary formats. For example,

long num1 = 25L;      // Decimal format 
long num2 = 0123L;     // Octal format 
long num3 = 0X191AAA31L;    // Hexadecimal format 
long num4 = 0b11001L; // Binary format 

Demo

public class Main {
  public static void main(String[] args) {
    long num1 = 25L;      // Decimal format 
    long num2 = 0123L;     // Octal format 
    long num3 = 0X191AAA31L;    // Hexadecimal format 
    long num4 = 0b11001L; // Binary format 

    System.out.println(num1);/*from w ww. j av a2s . co  m*/
    System.out.println(num2);
    System.out.println(num3);
    System.out.println(num4);
  }
}

Result

Long type class

Java has a class Long.

Long type defines two constants to represent maximum and minimum values of long data type, Long.MAX_VALUE and Long.MIN_VALUE.

long max = Long.MAX_VALUE; 
long min = Long.MIN_VALUE; 

Demo

public class Main {
  public static void main(String[] args) {
    long max = Long.MAX_VALUE; 
    long min = Long.MIN_VALUE; 
    System.out.println(max);//from  ww w  .j  a va  2s . c om
    System.out.println(min);
  }
}

Result

Exercise