Java Long Class Tutorial and Example

HQ ¬Ľ Java Tutorial ¬Ľ java.lang ¬Ľ Long
Java Long Class Tutorial and Example 2018-02-16T07:26:15+00:00

The Long class is simply a wrapper class for the primitive type long. It wraps the long primitive value to an object. An object of type Long contains a single field whose type is long. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Long class which is very useful.In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.

Long Class Syntax

public final class Long
extends Number
implements Comparable<Long>

Long Class Compatibility Version

Long Class is available since JDK 1.0

Long Class Basic Usage

The Long¬†class as part of the java.lang package is one of the classes of the java api not only being one of the oldest class but also one that is being widely used. Let’s tackle the basics of using the Long¬†class. To begin with, let’s tackle¬†first how to instantiate a Long¬†object

Since we are dealing with long, the suffix L or l is required. This is a common mistake for most of the newbie java programmer.

Basically the declaration of a Long¬†object is similar on how¬†we assign a primitive data type. Similarly to convert Long¬†primitive the long¬†object has only two possible values, which is either true or false. This is a straightforward method to instantiate a Byte object type. Let’s take another way to do this.

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There two constructor available to Long class which we can use to instantiate a Character object:

Constructor Description
Long(long value) Constructs a newly allocated Long object that represents the specified long argument.
Long(String s) Constructs a newly allocated Long object that represents the long value indicated by the String parameter.

Long Class Test of Equality

Normally when we do equality test for long primitive data type we do like this:

This will no longer work in dealing with Long object type. To test equality we need to make use of equals which is a method inherited from Object class.

Long Class Method Usage Examples

Modifier and Type Method and Description

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close