java.lang.Long Class

The Long class wraps the value of primitive data type long into Long object. An object of type Long contains a single field whose type is long. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

Long Class Constructors

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.

java.lang.Long Class Example
/* Save with file name LongExample.java */

public class LongExample
{
	public static void main(String args[])
	{
		//LONG DECLARATION
		Long b;
		//MEMORY ALLOCATION FOR LONG
		b = new Long(101);
		//LONG CLASS OUTPUT
		System.out.println();
		System.out.println("Long Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS byte PRIMITIVE DATA TYPE
		System.out.println("byte Value is : "+ b.byteValue());
		//RETURNS short PRIMITIVE DATA TYPE
		System.out.println("short Value is : "+ b.shortValue());
		//RETURNS double PRIMITIVE DATA TYPE
		System.out.println("double Value is : "+ b.doubleValue());
		//RETURNS float PRIMITIVE DATA TYPE
		System.out.println("float Value is : "+ b.floatValue());
		//RETURNS int PRIMITIVE DATA TYPE
		System.out.println("int Value is : "+ b.intValue());
		//RETURNS long PRIMITIVE DATA TYPE
		System.out.println("long Value is : "+ b.longValue());
	}
}
                        
java.lang.Long Class Example 2
/* Save with file name LongExample2.java */

public class LongExample2
{
	public static void main(String args[])
	{
		//LONG DECLARATION
		Long b, b2;
		//MEMORY ALLOCATION FOR LONG
		b = new Long(101);
		b2 = new Long("102");
		//LONG CLASS OUTPUT
		System.out.println();
		System.out.println("Long Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.longValue());
		System.out.println("b2 Value is : "+ b2.longValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Long Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Long Class Example 3
/* Save with file name LongExample3.java */

public class LongExample3
{
	public static void main(String args[])
	{
		//LONG CLASS STATIC METHODS USAGE
		System.out.println("Long Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Long OBJECT WITH THE SPECIFIED PRIMITIVE long DATA TYPE
		System.out.println("valueOf : "+ Long.valueOf(101));
		//RETURN Long OBJECT WITH THE SPECIFIED
		//PRIMITIVE long DATA TYPE AS String
		System.out.println("valueOf : "+ Long.valueOf("103"));
		//RETURN PRIMITIVE long DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE long DATA TYPE AS String
		System.out.println("parseLong : "+ Long.parseLong("105"));
	}
}