Java Long Primitive Data Type With Program Example

Long is a 64-bit primitive data type that supports the widest range of integer type values. It ranges from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is used where the int type seems smaller to hold a value. Its default value is 0L.

Printing Long Value

Long is a signed type that can hold both positive and negative values. Here’s the code block to print long values on the output screen.

public class LongExamples {
    public static void main(String[] args) {
        long posNum = 930;
        long negNum = -830;
        System.out.println("positive number :: "+posNum);
        System.out.println("negative number :: "+negNum);

    }
}

Output:

positive number :: 930
negative number :: -830

Java long to String

In Java, the String class provides various methods to convert the desired value to its string representation. Considering long type, we can use the same methods that we used previously to convert int to string. Below are the ways to convert long to a string :

1. Using toString() method

toString() method returns a string representation of an object. In the case of primitive types, an equivalent string object is returned of the respective data type.

Java program for converting long to string using toString()

public class LongExamples {
public static void main(String[] args) {
long num = 290;
String str = Long.toString(num);
System.out.println("Using toString() : "+str);
}
}

Output:

Using toString() : 290

2. Using valueOf() method

An overloaded method for all the built-in types that convert the desired value to string type. It is a part of the string class and a static method.

Java program for converting long to string using valueOf()

public class LongExamples {
public static void main(String[] args) {
long num = 290;
String str = String.valueOf(num);
System.out.println("Using valueOf() : "+str);
}
}

Output:

Using valueOf() : 290

3. Using concatenation operator

The concatenation operator is represented with a + sign. It concatenates two or more strings together. Concatenating an empty string with any built-in type would provide a proper string literal.

Java program for converting long to string using concatenation operator

public class LongExamples {
public static void main(String[] args) {
long num = 290;
String str = ""+num;
System.out.println("Using string concatenation : "+str);
}
}

Output:

Using string concatenation : 290

4. Using StringBuilder or StringBuffer

StringBuilder and StringBuffer classes provide mutable strings. These classes offer a method to convert a long to string, that is append() method.

Java program for converting long to string using StringBuilder and StringBuffer classes

public class LongExamples {
public static void main(String[] args) {
long num = 290;
StringBuilder stringBuilder = new StringBuilder();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(num);
stringBuilder.append(num);
System.out.println("Using stringBuilder : " + stringBuilder);
System.out.println("Using stringBuffer : " + stringBuffer);
}
}

Output:

Using stringBuilder : 290
Using stringBuffer : 290

5. Using format() method

Another static method from String class named format() method. It accepts  two arguments, first, a formatted string as param and other one is list of objects .

Java program for converting long to string using format() method

public class LongExamples {
public static void main(String[] args) {
long num = 290;
String str = String.format("%d",num);
System.out.println("Using format method :: "+str);
}
}

Output:

Using format method :: 290

6. Using formatDecimal class

formatDecimal is used for formatting numbers as per a particular format. It uses patterns as a constructor parameter. According to that format, a number gets formatted. It provides format() that accepts the desired number and forms it as per the given pattern.

Java program for converting long to string using formatDecimal Class

public class LongExamples {
public static void main(String[] args) {
long num = 290;
String str = new DecimalFormat().format(num);
System.out.println("Using formatDecimal :: "+str);
}
}

Output:

Using formatDecimal :: 290

Java long to integer

An integer can hold lower range values than long. It is of 32-bit, on other hand, a long is of 64-bit. If a long type is holding a value within the integer type range, then it is possible to convert a long value to integer type without loosing information.

1. Using typecasting

A simple assignment to integer type with explicit typecasting would convert a long value to integer type.

Java program for converting long to integer using typecasting

public class LongExamples {
public static void main(String[] args) {
long num = 290;
int intNum =(int)num;
System.out.println("Using typecasting :: "+intNum);
}
}

Output:

Using typecasting :: 290

2. Using intValue()

intValue() method of long class returns long type object value to int type.

Java program for converting long to integer using typecasting

public class LongExamples {
public static void main(String[] args) {
long num = 290;
int intNum = new Long(num).intValue();
System.out.println("Using intValue() :: "+intNum);
}
}

Output:

Using intValue() :: 290

3. Using toIntExact()

A predefined method that accepts long value as argument and returns it as an int type. It is a static method.

Java program for converting long to integer using toIntExact()

public class LongExamples {
public static void main(String[] args) {
long num = 290;
int intNum = Math.toIntExact(num);
System.out.println("Using toIntExact() :: "+intNum);
}
}

Output:

Using toIntExact() :: 290

Java long to date

Date class has a parameterised constructor that accepts long type value ( represents time in milliseconds) and returns its corresponding Date format.

Java program for converting long to date using  date constructor

public class LongExamples {
public static void main(String[] args) {
long num = 1656609908;
Date date = new Date(num);
System.out.println("Converting to date :: "+date);
}
}

Output:

Converting to date  :: Tue Jan 20 09:40:09 IST 1970

 

 

Leave a Reply