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 get the sign of the long value
  6. How to get the number of zero bits preceding and following
  7. 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
    /*from   ja  v  a 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;
/*j av  a  2  s  .  c  o 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 ja v  a  2  s .  co 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");
    /*j av a 2s.co  m*/
    System.out.println(long1);
    System.out.println(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 compare two long values
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing