![]() ![]() Program code 2: // Java Program to demonstrate the conversion of long into string using Long.toString(). Let’s create a simple Java program to convert a primitive long to string in java using Long.toString() method. The general signature of this method is as below: public static String toString(long l) It returns a string object representing the specified long numeric value. The toString() method of Long class is a static utility method. The long wrapper class in Java also provides a toString() method that converts long numeric value to String. (str + 200) // 500200 because + is string concatenation operator.Ĭonverting Long to String in Java using Long.toString(long) (num + 200) // 700 because + is binary plus operator. Call valueOf() method of String class for converting long to string. Program code 1: // Java Program to demonstrate the conversion of long into string using String.valueOf(). Let’s create a simple program to convert a primitive long to string value in Java. This method accepts a long numeric value that needs to be converted and returns the string representation of the long value. The general signature of valueOf() method is as below: public static String valueOf(long l) We can simply call by using its class name. Therefore, we do not need to create an object to calling this method. It is a static utility method of the String class. The method String.valueOf() converts long to string. ![]() ![]() To convert primitive long to string in Java, we use valueOf() method of the String class. For example, the static factory methodīoolean.valueOf(String) is almost always preferable to theĬonstructor Boolean(String).Let’s understand all the above ways one by one with example programs.Ĭonverting Long to String in Java using String.valueOf() You can often avoid creating unnecessary objects by using staticįactory methods (Item 1) in preference to constructors on immutableĬlasses that provide both. since most of them reuse instances whenever it is possible making them potentially more efficient in term of memory footprint than the corresponding parse methods or constructors.Įxcerpt from Effective Java Item 1 written by Joshua Bloch: Generally speaking, it is a good practice to use the static factory method valueOf(str) of a wrapper class like Integer, Boolean, Long. Please note that the previous code can still throw a NumberFormatException if the provided String doesn't match with a signed long. Thanks to auto-unboxing allowing to convert a wrapper class's instance into its corresponding primitive type, the code would then be: long val = Long.valueOf(str) To cache values within a particular range. Note that unlike theĬorresponding method in the Integer class, this method is not required Likely to yield significantly better space and time performance byĬaching frequently requested values. ![]() Used in preference to the constructor Long(long), as this method is New Long instance is not required, this method should generally be Returns a Long instance representing the specified long value. The best approach is Long.valueOf(str) as it relies on Long.valueOf(long) which uses an internal cache making it more efficient since it will reuse if needed the cached instances of Long going from -128 to 127 included. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |