Java long type
In this chapter you will learn:
- What is Java long type
- How to mark an integer as long type
- Find out the max/min value and its size for long type
- How to create Long object
- How to get the sign of the long value
- How to get the number of zero bits preceding and following
- 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:
Value | Meaning |
---|---|
-1 | if the specified value is negative; |
0 | if the specified value is zero; |
1 | if 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: