Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

java.lang.Long

• Long is a built-in class in Java available in java.lang package.
• It is a Wrapper class which is dedicated to long primitive data type.
• Java.lang.Long class has immutable property.

            
class A{
	public static void main(String arg[]){
		Long l1 = new Long (10);
		Long l2 = new Long (20);
		
		Long l3 = l1+l2;
		
		System.out.println("l1 == l3 :- "+(l1 == l3)); // false
		System.out.println("l2 == l3 :- "+(l2 == l3)); // false

	}
}
    l1 == l3 :- false
    l2 == l3 :- false
            

In the above example we can see that we have two different objects of Long class l1 and l2 and in the next line we are adding l1 and l2 and assigning their result in Long l3, and finally we are comparing l1 and l3, l2 and l3 and their corresponding results are false and false respectively. It proves thatno change happened in l1 and l2 which are the existing objects of java.lang.Long class.

• java.lang.Long class: -

public final class java.lang.Long extends java.lang.Number implements java.lang.Comparable<java.lang.Long> {
public static final long MIN_VALUE;
public static final long MAX_VALUE;
public static final java.lang.Class<java.lang.Long> TYPE;
public static final int SIZE;
public static final int BYTES;
public static java.lang.String toString(long, int);
public static java.lang.String toUnsignedString(long, int);
public static java.lang.String toHexString(long);
public static java.lang.String toOctalString(long);
public static java.lang.String toBinaryString(long);
static java.lang.String toUnsignedString0(long, int);
static int formatUnsignedLong(long, int, char[], int, int);
public static java.lang.String toString(long);
public static java.lang.String toUnsignedString(long);
static void getChars(long, int, char[]);
static int stringSize(long);
public static long parseLong(java.lang.String, int) throws java.lang.NumberFormatException;
public static long parseLong(java.lang.String) throws java.lang.NumberFormatException;
public static long parseUnsignedLong(java.lang.String, int) throws java.lang.NumberFormatException;
public static long parseUnsignedLong(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Long valueOf(java.lang.String, int) throws java.lang.NumberFormatException;
public static java.lang.Long valueOf(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Long valueOf(long);
public static java.lang.Long decode(java.lang.String) throws java.lang.NumberFormatException;
public java.lang.Long(long);
public java.lang.Long(java.lang.String) throws java.lang.NumberFormatException;
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public java.lang.String toString();
public int hashCode();
public static int hashCode(long);
public boolean equals(java.lang.Object);
public static java.lang.Long getLong(java.lang.String);
public static java.lang.Long getLong(java.lang.String, long);
public static java.lang.Long getLong(java.lang.String, java.lang.Long);
public int compareTo(java.lang.Long);
public static int compare(long, long);
public static int compareUnsigned(long, long);
public static long divideUnsigned(long, long);
public static long remainderUnsigned(long, long);
public static long highestOneBit(long);
public static long lowestOneBit(long);
public static int numberOfLeadingZeros(long);
public static int numberOfTrailingZeros(long);
public static int bitCount(long);
public static long rotateLeft(long, int);
public static long rotateRight(long, int);
public static long reverse(long);
public static int signum(long);
public static long reverseBytes(long);
public static long sum(long, long);
public static long max(long, long);
public static long min(long, long);
public int compareTo(java.lang.Object);
static {};
}

As we can see that inthe above API of java.lang.Long class, we have different utility methods or variables to perform different operationsalong with long type of values.

Here we will discuss about some important members of java.lang.Long class: -

1. public static final long MIN_VALUE;

Access Modifier: - public
Member Type: - static final variable
Data Type: - long
variable Name: - MIN_VALUE
Functionality: - This member stores the minimum value (2^63-1) into the long type of variable and, for creating an object,the same minimum value can be passed as an argument into the java.lang.Long class constructor.

When we try to store a value which is less than the minimum value (-9223372036854775808L) then we get compile-time error that shows as Integer number is too large. We can alsoget compile-time error messagewhile creating an objectof java.lang.Long class if we pass into a constructor an argument of value lesser than the lower limit .

            
class JTC{
	public static void main(String arg[]){
		System.out.println("Long.MIN_VALUE :- "+Long.MIN_VALUE);
		long l1 = -9223372036854775808L;
		System.out.println("l1 :- "+l1);
		// long i2 = -9223372036854775809L; --> integer number too large
		Long l3 = new Long(-9223372036854775808L);
		System.out.println("l3 :- "+l3);
		// Long l4 = new Long(-9223372036854775809); --> integer number too large
	}
}
    Long.MIN_VALUE :- -9223372036854775808
    l1 :- -9223372036854775808
    l3 :- -9223372036854775808
            

In the above example we can see that in JTC class first we have declared a longtype of variable which is long l1and assigned it a value of -9223372036854775808L.Since -9223372036854775808L is in the range of long primitive data type, so the statement is valid. In the next line we have one more variable long l2 = -9223372036854775809L and as we have discussed that -9223372036854775809L is not in the range of long, so here we get compile-time error which shows asInteger Number too large.
Then after we are creating an object of java.lang.Long class (Long l3 = new Long (-9223372036854775808L)) and as we can see that we are passing an argument of value-9223372036854775808L into thejava.lang.Long class constructor; since this value is in the range of Long, so it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Long class along with -9223372036854775809Las argument; because -9223372036854775809L is not in the range of Long, so here weget a compile time error.

2. public static final int MAX_VALUE;

Access Modifier: - public
Member Type: - static final variable
Data Type: - long
Variable Name: - MAX_VALUE
Functionality: - It stores the maximum value (9223372036854775807L) in the long type of variable and this same maximum value can be passed as an argument into the java.lang.Long class constructor,for creating an object.
When we try to store a value which is greater than the maximum value (9223372036854775807L) then we getcompile-time error that shows up asInteger number is too large and the same error occurs when we use this value for creating an object of java.lang.Long.

            
class JTC{
	public static void main(String arg[]){
		System.out.println("Long.MAX_VALUE :- "+Long.MAX_VALUE);
		long l1 = 9223372036854775807L; 
		System.out.println("l1 :- "+l1);
		// long l2 = 9223372036854775808L;  //--> Integer number is too large
		Long l3 = new Long(9223372036854775807L);
		System.out.println("l3 :- "+l3);
		// Long l4 = new Long(9223372036854775808L); //--> Integer number is too large
	}
}
    Long.MAX_VALUE :- 9223372036854775807
    l1 :- 9223372036854775807
    l3 :- 9223372036854775807
            

In the above example we can see that in JTC class we have declared an int type of variable which is long l1 and have assigned ita value of 9223372036854775807L.Since 9223372036854775807L is in the range of Long primitive data type, so it is valid.
In the next line we have one more variable long l2 = 9223372036854775808L and as we discussed that 9223372036854775808L is not in the range of Long, so here we get an error at compile time.
Then after we are creating an object of java.lang.Long class (Long l3 = new Long (“9223372036854775807L”)) and we can see that we are passinginto thejava.lang.Long class constructoran argument ofvalue 9223372036854775807L.Since the value is in the range of Long, so it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Long class along with 9223372036854775808Las argument; because 9223372036854775808L is not in the range of long,so here we will get compile time error which will show up as Integer number is too large.

3. public static long parseLong(java.lang.String) :-

Access Modifier: - public
Member Type: - static Method
Return Type: - long
Method Name: - parseLong
Parameter: - java.lang.String
Functionality: -As we know that we cannot type-cast a String type of value into the long type; but sometimes, as per our business logic, we convert the String literals into long type using parseLong() method.When we use parseLong() then we need to remember two conditions- Firstly, the argument of parseLong(String) must contain numeric value, and secondly the numeric value must be in the range of long. If these two conditions are not followed thenwe will get an exception that shows up as:java.lang.NumberFormatException.

            
class JTC{
	public static void main(String arg[]){
		long l1 = Long.parseLong("20");
		System.out.println("l1 :- "+l1);
		
		// Long.parseLong("9223372036854775808L"); -->java.lang.NumberFormatException

		// Long.parseLong("abc"); -->java.lang.NumberFormatException

	}
}
    l1 :- 20
            

In the above example we can see that in the first invocation of parseLong() method we are passing “20” as argument and because the argument value is in number format and 20 is in the range of Long,so this statement is valid.
However in the next two invocations of parseLong() method we are passing arguments of value “9223372036854775808L” and “abc” respectively. Since “9223372036854775808L” is not in the range of Long and “abc” is in string format and not in number format, so at the time of program executionwe will get an exception that shows up asjava.lang.NumberFormatException, for both invocations.

4. public int hashCode(): -

Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - hashCode()
Parameter: - No parameter
Functionality: - hashCode() method is overridden from java.lang.Object class in java.lang.Longclass.While creating the current working object of java.lang.Long class, the method returns the same value that was passed as an argument into the Integer class constructor.

            
class JTC{
	public static void main(String arg[]){
		
		Long l1 = new Long("32767");
		System.out.println("l1 hashcode :- "+l1.hashCode());
		
	}
}
    l1 hashcode :- 32767
            

In the above example we are creating an object of java.lang.Long class and passing “32767” as an argument (Long l1 = new Long (“32767”)) and in the next line we are invoking hashCode() along with l1. As we discussed earlier, hashCode() method is overridden in java.lang.Long class and that is the reason that here we are getting 32761 as the output.

5. public boolean equals(Object obj): -

Access Modifier: - public
Member Type: - Instance Method
Return Type: - boolean
Method Name: - equals()
Parameter:- Object
Functionality: - equals() methodis overridden in java.lang.Long class, and here it compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object.

            
class JTC{
	public static void main(String arg[]){
		Long l1 = new Long(10L);
		Long l2 = new Long(20L);
		
		System.out.println("l1.equals(l2) :- "+(l1.equals(l2))); // false

		Long l3 = new Long("10");
		
		System.out.println("l1.equlas(l3) :- "+(l1.equals(l3))); // true

	}
} 
    l1.equals(l2) :- false
    l1.equlas(l3) :- true
            

In this example we are creating two different objects of Long classi.e., Long l1 = new Long (10) and Long l2 = new Long (20) . Since we are passing two different values to create objects of java.lang.Long class, so here we are getting false.
In the next line again, we are creating an object of Long class i.e., (Long l3 = new Long (10)) and then after we are comparing l1 and l3 using equals() method.Since both objectscontain the same value,hence we get true as the result.