Long Wrapper Class in Java

The Long class in Java is a wrapper class for the primitive data type long. It is part of the java.lang package and provides methods to work with long values, such as parsing, converting, and comparing them. The Long class also provides constants for representing the maximum and minimum values a long can have.

Key Features of Long Wrapper Class

  1. Immutability: Instances of the Long class are immutable.
  2. Constants: Provides constants like MAX_VALUE and MIN_VALUE.
  3. Static Methods: Methods for parsing strings to longs, comparing longs, and converting longs to strings.

Long Class Methods

Here are some commonly used methods in the Long class:

  1. byte byteValue()
  2. int compareTo(Long anotherLong)
  3. static int compare(long x, long y)
  4. static Long decode(String nm)
  5. double doubleValue()
  6. boolean equals(Object obj)
  7. float floatValue()
  8. static Long getLong(String nm)
  9. static Long getLong(String nm, long val)
  10. static Long getLong(String nm, Long val)
  11. int hashCode()
  12. int intValue()
  13. long longValue()
  14. static long parseLong(String s)
  15. static long parseLong(String s, int radix)
  16. short shortValue()
  17. static String toString(long i)
  18. String toString()
  19. static Long valueOf(String s)
  20. static Long valueOf(long l)

1. byte byteValue()

Returns the value of this Long as a byte.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        byte value = l.byteValue();
        System.out.println(value);  // Output: 57 (truncated value)
    }
}

Output:

57

2. int compareTo(Long anotherLong)

Compares two Long objects numerically.

public class LongExample {
    public static void main(String[] args) {
        Long l1 = 12345L;
        Long l2 = 54321L;
        System.out.println(l1.compareTo(l2));  // Output: -1
    }
}

Output:

-1

3. static int compare(long x, long y)

Compares two long values numerically.

public class LongExample {
    public static void main(String[] args) {
        System.out.println(Long.compare(12345L, 54321L));  // Output: -1
    }
}

Output:

-1

4. static Long decode(String nm)

Decodes a String into a Long.

public class LongExample {
    public static void main(String[] args) {
        Long l = Long.decode("12345");
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

5. double doubleValue()

Returns the value of this Long as a double.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        double value = l.doubleValue();
        System.out.println(value);  // Output: 12345.0
    }
}

Output:

12345.0

6. boolean equals(Object obj)

Compares this object to the specified object.

public class LongExample {
    public static void main(String[] args) {
        Long l1 = 12345L;
        Long l2 = 12345L;
        System.out.println(l1.equals(l2));  // Output: true
    }
}

Output:

true

7. float floatValue()

Returns the value of this Long as a float.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        float value = l.floatValue();
        System.out.println(value);  // Output: 12345.0
    }
}

Output:

12345.0

8. static Long getLong(String nm)

Returns the Long value of the system property with the specified name.

public class LongExample {
    public static void main(String[] args) {
        System.setProperty("myLong", "12345");
        Long l = Long.getLong("myLong");
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

9. static Long getLong(String nm, long val)

Returns the Long value of the system property with the specified name, or the specified default value if there is no property with that name.

public class LongExample {
    public static void main(String[] args) {
        Long l = Long.getLong("nonExistent", 12345L);
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

10. static Long getLong(String nm, Long val)

Returns the Long value of the system property with the specified name, or the specified default value if there is no property with that name.

public class LongExample {
    public static void main(String[] args) {
        Long l = Long.getLong("nonExistent", Long.valueOf(12345L));
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

11. int hashCode()

Returns a hash code for this Long object.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        System.out.println(l.hashCode());  // Output: a unique hash code
    }
}

Output:

12345

12. int intValue()

Returns the value of this Long as an int.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        int value = l.intValue();
        System.out.println(value);  // Output: 12345
    }
}

Output:

12345

13. long longValue()

Returns the value of this Long as a long.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        long value = l.longValue();
        System.out.println(value);  // Output: 12345
    }
}

Output:

12345

14. static long parseLong(String s)

Parses the string argument as a signed decimal long.

public class LongExample {
    public static void main(String[] args) {
        long l = Long.parseLong("12345");
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

15. static long parseLong(String s, int radix)

Parses the string argument as a signed long in the radix specified by the second argument.

public class LongExample {
    public static void main(String[] args) {
        long l = Long.parseLong("1101", 2);
        System.out.println(l);  // Output: 13
    }
}

Output:

13

16. short shortValue()

Returns the value of this Long as a short.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        short value = l.shortValue();
        System.out.println(value);  // Output: 12345
    }
}

Output:

12345

17. static String toString(long i)

Returns a String object representing the specified long.

public class LongExample {
    public static void main(String[] args) {
        String s = Long.toString(12345L);
        System.out.println(s);  // Output: "12345"
    }
}

Output:

12345

18. String toString()

Returns a String object representing this Long's value.

public class LongExample {
    public static void main(String[] args) {
        Long l = 12345L;
        System.out.println(l.toString());  // Output: "12345"
    }
}

Output:

12345

19. static Long valueOf(String s)

Returns a Long object holding the value given by the specified String.

public class LongExample {
    public static void main(String[] args) {
        Long l = Long.valueOf("12345");
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

20. static Long valueOf(long l)

Returns a Long instance representing the specified long value.

public class LongExample {
    public static void main(String[] args) {
        Long l = Long.valueOf(12345L);
        System.out.println(l);  // Output: 12345
    }
}

Output:

12345

Conclusion

The Long class in Java provides a wide range of methods for manipulating and converting long values. Understanding and utilizing these methods can greatly enhance your ability to handle long operations in your applications.

Comments