Java long type

In this chapter you will learn:

  1. What is Java long type
  2. How to mark an integer as long type
  3. Find out the max/min value and its size for long type
  4. How to create Long object
  5. How to compare two long values
  6. How to get the sign of the long value
  7. How to get the number of zero bits preceding and following
  8. How to reverse and rotate a long value

long type

long is a signed 64-bit type and is used when an int type is not large enough. The range of long type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

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
    /*  j  ava  2 s.  c o  m*/
    } 
}

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

long type literal

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.

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;
/*  ja  va 2s . co  m*/
    System.out.println("l is " + l);
  }
}

The output generated by this program is shown here:

The Long class wraps a value of long in an object. Long class provides several methods for converting a long to a String and a String to a long.

max/min value and its size for long type

Long class defines three constants to designate long type's max value, min value and number of bits.

  • static long MAX_VALUE stores maximum value a long can have, 2^63-1.
  • static long MIN_VALUE stores minimum value a long can have, -2^63.
  • static int SIZE stores the number of bits used to represent a long value.

The following code displays the max/min value of a long type

public class Main {
  public static void main(String[] args) {
    System.out.println(Long.MAX_VALUE);/*from j  a v a 2s.  c  o  m*/
    System.out.println(Long.MIN_VALUE);
    System.out.println(Long.SIZE);
  }
}

The output:

Create Long type object

Long class defines two constructors we can use to create Long type object.

  • Long(long value) creates a Long object that represents the specified long argument.
  • Long(String s) creates a Long object that represents the long value indicated by the String parameter.

The following code creates two Long type objects, one is from long type literial and the other is from a string value.

public class Main {
  public static void main(String[] args) {
    Long long1 = new Long(12345L);
    Long long2 = new Long("12346");
    /* ja v a 2 s .c o m*/
    System.out.println(long1);
    System.out.println(long2);
  }
}

The output:

Compare two long values

We can use the following two methods to compare two long type value.

  • int compareTo(Long anotherLong) compares two Long objects numerically.
  • boolean equals(Object obj) compares this object to the specified object.

compareTo(Long anotherLong) returns

ValueMeaning
0if this Long is equal to the argument Long;
less than 0if this Long is numerically less than the argument Long;
greater than 0if this Long is numerically greater than the argument Long.

The following code uses the compareTo(Long anotherLong) method to do the comparison.

public class Main {
  public static void main(String[] args) {
    Long long1 = new Long(12345L);
    Long long2 = new Long("12346");
    /*from  ja  va  2 s  .com*/
    System.out.println(long1.compareTo(long2));
  }
}

The output:

If you just want to check the equality,

public class Main {
  public static void main(String[] args) {
    Long long1 = new Long(12345L);
    Long long2 = new Long("12346");
    //from j  av a  2 s.c  om
    System.out.println(long1.equals(long2));
  }
}

The output:

Get the sign of the long value

static int signum(long i) returns the signum function of the long value.

The returning value of signum(long i) are listed below:

ValueMeaning
-1if the specified value is negative;
0if the specified value is zero;
1if the specified value is positive.

The following code checks the sign for a negative value.

public class Main {
  public static void main(String[] args) {
    System.out.println(Long.signum(-10));
  }
}

The output:

Get the number of zero bits preceding and following

  • static int numberOfLeadingZeros(long i) returns the number of zero bits preceding the highest-order one-bit in binary.
  • static int numberOfTrailingZeros(long i) returns the number of zero bits following the lowest-order one-bit in binary.
public class Main {
  public static void main(String[] args) {
    System.out.println(Long.numberOfLeadingZeros(123123));
  }
}

The output:

Reverse and rotate a long value

  • static long reverse(long i) reverses the order of the bits.
  • static long reverseBytes(long i) reverses the order of the bytes.
  • static long rotateLeft(long i, int distance) rotates the binary long value left by distance.
  • static long rotateRight(long i, int distance) rotates the binary long value right by distance.
public class Main {
  public static void main(String[] args) {
    System.out.println(Long.reverse(123123));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to Convert long value to byte, double, float, int, long, short
  2. How to decode a string to create long value
  3. How to convert and parse long value from string
  4. How to convert or parse string to long with radix
  5. How to convert long value to binary, hex and octal format strings
  6. How to convert long value to string
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class