Java long type

Description

Java long type is used when an int type is not large enough.

Size and value

long is a signed 64-bit type and . The range of long type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Literals

To specify a long literal, you need to tell the compiler that the literal value is of type long by appending an upper- or lowercase L to the literal. For example, 0x7ffffffffffffffL or 123123123123L.

Example

The following code creates a long type literal and assigns the value to a long type variable.


public class Main {
  public static void main(String args[]) {
    long l = 0x7ffffffffffffffL;
//w  w  w.  ja  v  a 2s  . c  o m
    System.out.println("l is " + l);
  }
}

The output generated by this program is shown here:

Example 2

Here is a program that use long type to store the result.


public class Main { 
    public static void main(String args[]) { 
        long result= (long)Integer.MAX_VALUE * (long)10; 
        System.out.println(result);//21474836470
    //from w  w  w .j  a va 2s. c o m
    } 
}

The result could not have been held in an int variable.

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures