SoFunction
Updated on 2025-03-08

Example of code comparing whether Long types are equal in Java

In Java, you can use the "==" and "equals()" methods to compare whether the Long types are equal.

1. Use "==" for comparison

In Java, "==" is used to compare whether two basic data types or two reference data types point to the same object. For encapsulated classes of Long type, since their values ​​are cached when they are between -128 and 127, Long type objects with the same values ​​point to the same object, and using "=="Compare will return "true". However, the Long-type object created by a value outside this range is not the same object. Even if the value is the same, using "==" will return "false".

Long a = 128L;
Long b = 128L;
(a == b); // Outputfalse
Long a = 127L;
Long b = 127L;
(a == b); // Outputtrue

2. Use "equals()" to compare

Another way to compare the equality of Long types is to use the "equals()" method. The "equals()" method compares whether the values ​​of two Long objects are equal, rather than whether they point to the same object. Therefore, no matter what range the value of the Long object is in, as long as the values ​​are equal, using the "equals()" method will return "true".

Long a = 128L;
Long b = 128L;
((b)); // Outputtrue
Long a = 127L;
Long b = 127L;
((b)); // Outputtrue

3. Comparison between basic types and encapsulation classes

Another point that needs to be noted is that when using "==" to compare between basic types and encapsulation classes, automatic unboxing operations will be performed. In other words, the encapsulation class will be converted into basic types for numerical comparisons. Therefore, regardless of whether the value is between -128 and 127 or not, the "==" comparison between the basic type and the encapsulation class will always return the correct result.

long a = 128L;
Long b = 128L;
(a == b); // Outputtrue
long a = 127L;
Long b = 127L;
(a == b); // Outputtrue

Appendix: Determine the maximum and minimum values ​​of long

In addition to length, we can also use the MAX_VALUE and MIN_VALUE constants of the Long class to get the maximum and minimum values ​​of the long type.

long maxValue = Long.MAX_VALUE;
long minValue = Long.MIN_VALUE;
("The maximum value of long is " + maxValue);
("The minimum value of long is " + minValue);

In the above code, we use Long.MAX_VALUE to get the maximum value of the long type, and use Long.MIN_VALUE to get the minimum value of the long type, and print out the result.

The output result is:

The maximum value of long is 9223372036854775807
The minimum value of long is -9223372036854775808

This indicates that the value range of the long type is between -9223372036854775808 and 9223372036854775807.

Summarize

This is the end of this article about comparing whether the Long types are equal in Java. For more related content about comparing whether the Long types are equal in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!