How to Convert a Long Primitive Data Type to a String Type in Java?

javathreadIn every Java program, you will encounter several primitive data types — byte, short, int, long, float, double, boolean or char—declared and manipulated. They drive the inner workings of most Java applications. String is not a data type but a class in java that represents character strings.

New to Java? Learn to code in Java with a course in udemy.com

int x = 5;  //  integer variable declaration

double d = 56.7; // double variable declaration

String s = “hello45678%”;  // String object declaration

In Java, the primitive data types can be cast to a String. This means variable x and d in the above example can be turned to a String object and assigned to a String variable. This can be achieved in more than one  ways.

Convert Java Long to String

One way to cast a long primitive type to String is by using String concatenation. The ‘+’ operator in java is overloaded as a String concatenator. Anything added to a String object using the ‘+’ operator becomes a String.

For example:

class longToString1

{

Public static void main(String[] arg)

{

long  num1 = 4587;          // declare a long variable

String longString = “ ” + num1;  // concatenate the long and an empty String

System.out.println(longString);

}

}

OUTPUT:

4587

In the above example, the output “4587” is a String type and is no longer a long.

In the concatenation step, you can include an actual string instead of an empty string.

So if we modify that step to the following code:

String longString = “hello” + num1;

This modification will give the output: hello4587. But since we only want the string form of the long variable, we can use just an empty String.

A second method to convert long to String is by using a static method of the class Long. The class Long is one of the wrapper classes in java that lets you wrap a primitive data type into an object. There is a matching wrapper class for every primitive type. Following are primitive data types and their corresponding wrapper classes:

char  –  Character

boolean – Boolean

byte  – Byte

int – Integer

double – Double

short – Short

long – Long

float  – Float

Wrapper classes are very useful when you want to add primitives to Collections. You may want to add long variables to an arraylist. An arraylist, however, will not accept long variables. In such cases, you can wrap the long variable with a wrapper— turn a long variable to a Long object. The arraylist would now accept the long variables.

For example:

Wrapping a long variable:

long  x = 34533355;

Long  xWrap = new Long(x);

Unwrapping a Long object:

long xUnwrap = xWrap.longValue();

In Java 5.0 and later versions, the wrapping and unwrapping is done automatically. A concept called autoboxing takes care of the hassle of manually wrapping and unwrapping primitive data types that are supposed to go to a collection’s class.

Learn important Java concepts through a course in Udemy

Wrapper classes have several static methods that will allow you to change a primitive variable to a String. A  Java long type can be converted to String using the toString(long x).

For example:

class longToString2

{

Public static void main(String[] arg)

{

long  num2 = 4587;          // declare a long variable

String longString2 =  Long.toString(num2); // convert the long variable num2 to a String

System.out.println(longString2);

}

}

OUTPUT:

4587

If you declare a variable of the type Long, you can convert it to a String object by using the simple toString().

The Long class along with other wrapper classes is included in the java.lang package. This means you will not have to import any class in order to use the methods of the wrapper classes.

The toString method of Long class also has an additional overloaded method: toString(long i, int radix).

It is defined as:  public static String toString(long i, int radix)

The method takes in two arguments — a long and an int — and returns a String type.

Applications of Java long to String by overriding toString()

In java, the toString() method is defined in the Object class, which means you do not have to extend any class in order to use the toString() in your class. The toString() method is very useful to provide concise and clear information about an object. You can fully implement this method by overriding it in your class and creating your own version that can display detailed information about any object in your class.

Consider the following example:

Let’s say you are building a bank-related application that stores customer information. The application will contain several long variables like bank account numbers and other numeric codes. You can build a toString version that displays all details of a customer as one String object.

import java.util.*;

class CusDetails {

String name;     // declare variables

long acc;

long SortCode;

public CusDetails(String s, long a, long b) // take in customer details through constructor

{

this.name = s;

this.acc = a;

this.SortCode = b;

}

 

@Override  // although not necessary, using the @override symbol is considered good  practice

public  String toString() // override the Object class’s toString method.

{

return "Name of the customer: " + this.name + "\nBank Account number:" + this.acc + "\nBank Code: " + this.SortCode + "\n\n";

}

}

class PrintDetails{

public static void main(String[] arg)

{

ArrayList<CusDetails>  cd = new ArrayList();

cd.add(new CusDetails("Ann", 34567778, 3466));

cd.add(new CusDetails("Mary", 23434536, 2353));

Iterator<CusDetails> itr = cd.iterator();

while(itr.hasNext())

{

System.out.println(itr.next());

}

}

OUTPUT:

Name of the customer: Ann

Bank Account number:  34567778

Bank Code: 3466

Name of the customer: Mary

Bank Account number: 23434536

Bank Code: 2353

Notice in the above example that we did not have to explicitly call the toString method because the System.out.print method implicitly calls it.

Learn how to use built-in methods and classes in Java API through a java programming course

How to Convert a String into a long in Java?

The String object on the other hand can be converted to primitive data types.

The Long class includes methods to convert String to long. Other wrapper classes such as Integer, Double, Float or Boolean can also convert a String to their corresponding types provided; the string object is actually a textual representation of the specific type.

Let’s look at the following example.

Class ParseString

{

Public static void main(String[] arg)

{

String s = “364678967”;  // declare a string variable

long num = Long.parseLong(s);  // call the parseLong method of class Long and pass the

//string as argument.

System.out.println(num);

}}

OUTPUT:

364678967

The above output is a long variable and can now be manipulated and used as a number in an expression.