Introduction
The Long
class in Java is a wrapper for the primitive type long
. It provides methods for converting and manipulating long values, handling arithmetic operations, and comparing longs.
Table of Contents
- What is
Long
? - Creating
Long
Instances - Common Methods
- Examples of
Long
- Conclusion
1. What is Long?
Long
is a final class that wraps a long
value in an object. It offers methods for converting between long
and String
, performing arithmetic operations, and comparing longs.
2. Creating Long Instances
You can create Long
instances in several ways:
- Using the
Long
constructor:new Long(long value)
ornew Long(String value)
- Using the
Long.valueOf(long value)
orLong.valueOf(String value)
methods
3. Common Methods
longValue()
: Returns the value of thisLong
as a primitivelong
.toString()
: Returns aString
representation of thelong
value.parseLong(String s)
: Parses aString
to a primitivelong
.compareTo(Long anotherLong)
: Compares twoLong
objects numerically.compare(long x, long y)
: Compares twolong
values.MAX_VALUE
: A constant holding the maximum value along
can have (2^63-1).MIN_VALUE
: A constant holding the minimum value along
can have (-2^63).
4. Examples of Long
Example 1: Creating Long
Instances
This example demonstrates how to create Long
instances using constructors and valueOf
methods.
public class LongInstanceExample { public static void main(String[] args) { Long l1 = new Long(10L); Long l2 = Long.valueOf(20L); Long l3 = Long.valueOf("30"); System.out.println("l1: " + l1); System.out.println("l2: " + l2); System.out.println("l3: " + l3); } }
Output:
l1: 10 l2: 20 l3: 30
Example 2: Parsing a String to long
This example shows how to parse a String
to a primitive long
using parseLong
.
public class ParseLongExample { public static void main(String[] args) { long l1 = Long.parseLong("50"); long l2 = Long.parseLong("100"); System.out.println("l1: " + l1); System.out.println("l2: " + l2); } }
Output:
l1: 50 l2: 100
Example 3: Comparing Long Values
In this example, we demonstrate how to compare two Long
values using compareTo
and compare
.
public class CompareLongExample { public static void main(String[] args) { Long l1 = Long.valueOf(10L); Long l2 = Long.valueOf(20L); int comparison = Long.compare(l1, l2); if (comparison < 0) { System.out.println("l1 is less than l2"); } else if (comparison > 0) { System.out.println("l1 is greater than l2"); } else { System.out.println("l1 is equal to l2"); } } }
Output:
l1 is less than l2
Example 4: Using MAX_VALUE
and MIN_VALUE
This example demonstrates the use of MAX_VALUE
and MIN_VALUE
constants.
public class LongMinMaxExample { public static void main(String[] args) { System.out.println("Maximum Long Value: " + Long.MAX_VALUE); System.out.println("Minimum Long Value: " + Long.MIN_VALUE); } }
Output:
Maximum Long Value: 9223372036854775807 Minimum Long Value: -9223372036854775808
5. Conclusion
The Long
class in Java is a useful wrapper for the primitive long
type. It provides methods for converting, manipulating, and handling long values.