Java long type

In this chapter you will learn:

  1. What is Java long type
  2. Size and value for Java long type
  3. How to mark an integer as long type
  4. Example - long literal
  5. Example - 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;
//from   w w w.j  av  a  2  s  . 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  ww  .  j av  a 2  s.co  m*/
    } 
}

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

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is Java float type
  2. Value and size for Java float type
  3. How to create float literals
  4. Example - Java float literal
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Types
Java Primitive Data Types
Java boolean type
Java char type
Java char value escape
Java byte type
Java short type
Java int type
Java long type
Java float type
Java double type
Java String type
Java String Escape
Java String Concatenation