- Java long type
- long type literal
- max/min value and its size for long type
- Create Long type object
- Get the sign of the long value
- Get the number of zero bits preceding and following
- Reverse and rotate a long value
- Next chapter.
- Long programs in java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Long Data Type In Java
- Representation of Long Data Type In Java
- How To Use Long Data Type In Java
- Error: Integer Number Too Large
Java long type
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); > >
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); > >
Get the sign 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)); >>
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)); >>
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)); >>
Next chapter.
What you will learn in the next chapter:
Long programs in java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Long Data Type In Java
Long Data Type In Java | The Long is one of eight primitive data types in Java, it is also called the fundamental data type. The long data type is the type of integer that stores only the numeric values but not the floating point numbers containing the decimal value.
The long can occupy the largest memory space up to 64 bits, that is it ranges from -9223372036854775808 to 9223372036854775807 i.e. -2 63 to 2 63 -1. So we can say that to store any large numeric value we can use a long data type. To find the minimum and maximum value of the long data type Java Long class provides the constant that stores the minimum and maximum values of the long, they are MIN_VALUE and MAX_VALUE respectively.
Long min Value: -9223372036854775808
Long max Value: 9223372036854775807
While working with data types we need to know the range of each data type in order to avoid errors, knowing all the minimum and maximum values of each data type is quite tedious hence to overcome that Java has made it easy by providing these constants. As said above the minimum value of long is -9223372036854775808 and the maximum value is 9223372036854775807 and the default value is 0.
Size :: 8 bytes Range ::-263 to 263-1
Or,
-9223372036854775808
to 9223372036854775807
Default value :: 0 Corresponding Wrapper class :: Long
Representation of Long Data Type In Java
To represent an int data type value as a long data type we must write one of the below code:-
long n3 = 9; // variable assignment Or, (long)9; // casting // it will convert 9 as int to long Or, 9L or 9l // using suffix L or l
While declaring the long values we should append ‘L’ or ‘l’ (capital or small L) at the end indicating it is a long value. Below is an example of the long data type in Java.
By default, any number declared in Java is treated as of int data type. if the given number belongs to the int data type range then we can assign it to a long data type without any suffix. Due to auto-promotion, the int value is internally converted as long by JVM.
int n1 = 123; // valid long n2 = 123; // valid
The number 8888888888888 is out of the int data type range but belongs to the long data type range. To represent it as a long data type we must add the suffix ‘l’ or ‘L’ to it.
int n1 = 8888888888888; // invalid long n2 = 8888888888888; // invalid long n3 = 8888888888888l; // valid long n4 = 8888888888888L; // valid
Note:- Lowercase l (small L) looks like 1 (one) and sometimes it creates confusion, so it is suggested to use the capital letter (L) instead of the small letter (l) for the long data type.
How To Use Long Data Type In Java
The default size of the long data type is 8 bytes and the default value is 0L. The below code demonstrates the use of long data types for both positive and negative values.
Positive long number: 8888812487635898
Negative long number: -3698745123654789
Sum: 5190067363981109
Subtraction: 12587557611290687
Multiplication: 5279030630842837086
Division: -2
Remainder: 1491322240326320
Error: Integer Number Too Large
By default, large integer numbers also consider as int type by the Compiler. But large integers are not in the range of int data type. Hence when we use a large integer number directly, the compiler will throw an error. The compiler throwing error is “ integer number is too large ”
System.out.println(987654321012345); // error: integer number too large
The compiler considers 987654321012345 as an integer number but this number exceeds the range of the int data type, we got an error.
To use a large integer number, we must suffix this number with L or l. Now, the compiler will treat the number as a long type. The number comes under the range of the long data type so we don’t get any errors.
System.out.println(987654321012345L); // Output:- 987654321012345
The long data type has also a range and it does not support a very long number like 100! If we want to store a very very long number which is out of range of the long data type then how can we store them in Java? In that case, we use the string-referenced data type. BigDecimal and BigInteger classes are also there to represent large values. Using string we can store those very very long numbers.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!