Friday, June 20, 2025
HomeJavaFind out how to convert lengthy to String in Java? Instance

Find out how to convert lengthy to String in Java? Instance


There are three most important methods to transform an extended worth to a String in Java e.g. through the use of Lengthy.toString(lengthy worth) methodology, through the use of String.valueOf(lengthy), and by concatenating with an empty String. You should use any of those strategies to transform an extended information sort right into a String object. It isn’t very completely different from how you change an int to String in Java. The identical methodology applies right here as effectively. String concatenation appears the best method of changing an extended variable to a String, however others are additionally handy.  

In case you look Java code for valueOf() methodology from java.lang.String class, you’ll notice that it really calls the toString() methodology of java.lang.Lengthy class for conversion, which implies there is just one methodology the place the logic of changing primitive lengthy to String is written.

Nonetheless, in case you are not changing lengthy within the loop all strategies will carry out the identical, but when it’s a must to convert a lot of lengthy values to String then straight utilizing Lengthy.toString() methodology could be fairly helpful. 

Find out how to convert lengthy to String in Java? 

Listed below are the 3 ways to transform an extended primitive to String in Java :

  • Utilizing Lengthy.toString() methodology 
  • Utilizing String.valueOf() methodology
  • Utilizing String concatenation with empty String

You should use any of those strategies to transform an extended worth to a String in Java, however String.valueOf() is the really helpful method in Java.

How to convert long to String in Java? Example

Java Program to transform alongside worth to String in Java

Right here is our pattern Java program for this information sort conversion. On this program, you’ll study all these three strategies of changing primitive lengthy to String in Java.

/**
 * Java Program to transform lengthy to String in Java
 * 
 * @writer java67
 */

public class LongToStringInJava {

    public static void most important(String args[]) {

        // let's first create an extended variable for conversion
        lengthy massive = 3344344323434343L;

        // changing lengthy to String utilizing String concatenation
        // you simply want so as to add quantity with empty String
        // as proven beneath
        String s1 = "" + massive;
        System.out.println("String worth created from lengthy by concatenation : "
                + s1);

        // lengthy to String conversion utilizing Lengthy.toString() methodology
        String s2 = Lengthy.toString(massive);
        System.out.println("String to lengthy utilizing Lengthy.toString() : " + s2);

        // lengthy worth to String utilizing String.valueOf()
        String s3 = String.valueOf(massive);
        System.out.println("String to lengthy utilizing String.valueOf() : " + s3);

    }

}

Output
String worth created from lengthy by concatenation : 3344344323434343
String to lengthy utilizing Lengthy.toString() : 3344344323434343
String to lengthy utilizing String.valueOf() : 3344344323434343

From the output, it is fairly clear that we had been capable of convert the large lengthy worth into String with none drawback. Each Lengthy.toString() and String.valueOf() methodology can also be capable of convert an extended worth with a constructive and unfavourable signal to corresponding String literal as proven beneath :

lengthy longWithPlusSign = +300L;
String str1 = Lengthy.toString(longWithPlusSign);
        
lengthy longWithMinusSign = -333L;
String str2 = String.valueOf(longWithMinusSign);
        
System.out.println("lengthy to String with plus signal : " + str1);
System.out.println("lengthy to String with minus signal : " + str2);

Output
lengthy to String with plus signal : 300
lengthy to String with minus signal : -333

Issues to recollect

1) String.valueOf(lengthy) methodology internally calls Lengthy.toString() methodology.
2) In case you go an indication together with a quantity e.g. constructive or unfavourable signal then transformed String can even comprise that signal however just for unfavourable numbers, not for positives.

That is all about how one can convert lengthy values to String in Java. It is higher to make use of Lengthy.toString() methodology as a result of all different strategies internally name this one solely. If you know the way to transform int to String or double to String, you possibly can comply with the identical steps to transform different information varieties to String in Java as effectively.

In case you like this information sort conversion tutorial in Java, you may additionally get pleasure from following tutorials about changing String to different varieties e.g. Date, Enum, and so on

  • Find out how to convert ByteBuffer to String in Java? (program)
  • Find out how to convert String to Double in Java? (instance)
  • Find out how to convert float to String in Java? (instance)
  • Find out how to convert byte array to String in Java? (program)
  • Find out how to convert Double to Lengthy in Java? (program)
  • Find out how to convert String to Date in a thread-safe method? (instance)
  • Find out how to convert String to int in Java? (instance)
  • Find out how to convert Decimal to Binary in Java? (instance)
  • Find out how to convert Enum to String in Java? (instance)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments