How To Parse Or Convert String To Long Inwards Coffee - Four Examples

How to convert a string to long inward Java is i of those oft asked questions past times a beginner who has started learning Java programming linguistic communication together with non aware of how to convert from i information type to another. Converting String to long is similar to converting String to Integer inward Java, in-fact if y'all know how to convert String to Integer than y'all tin dismiss convert String to Long past times next same procedure. Though y'all demand to retrieve few things spell dealing alongside long together with String commencement of all long is primitive type which is wrapped past times Long wrapper shape together with String is an Object inward Java backed past times graphic symbol array. Before Java five y'all demand to convey an extra stride to convert Long object into long primitive only afterwards introduction of autoboxing together with unboxing inward Java 5, Java linguistic communication volition perform that conversion for you. This article is inward continuation of our previous conversion tutorial similar how to convert String to Double inward Java or how to convert String to Enum inward Java. In this Java tutorial nosotros volition acquire three dissimilar ways to convert String to long past times code trial together with nosotros volition too analyze pros together with cons of each approach.


How to convert String to long inward Java

Here are three dissimilar ways to convert an String to long primitive value inward Java:
  1. Using Long.valueOf() method
  2. Using Long.parseLong() method
  3. Using java.lang.Long constructor
  4. Using Long decode() method


String to long - Long.valueOf
static mill method from java.lang.Long shape which accepts an String declaration together with returns an equivalent long primitive value. Long.valueOf() volition throw NumberFormatException if String is non convertible into long due to whatsoever argue similar String contains non numeric value, numeric value of String is beyond MAX together with MIN value of Long etc. Though negative sign tin dismiss move included to announce negative long value every bit commencement character. Main payoff of valueOf() is that its an static mill method together with tin dismiss cache oft used long value, since long is an immutable object its rubber to reuse cached long values. valueOf() is too an overloaded method which accepts radix every bit 2d declaration to convert hexadecimal or octal String to long. Here is an trial of converting String to long value using valueOf method inward Java:

long decimalNumber = Long.valueOf("55555");
long octalNumber = Long.valueOf("55555", 8);
long hexNumber = Long.valueOf("5555", 16);

As per caching JDK's Long.valueOf() method caches values upto -128 to 127 together with returns same Long object which volition render truthful if compared alongside equality operator "==". y'all tin dismiss too await code of valueOf() method on java.lang.Long shape to verify it.

String to long - Long.parseLong
Long.parseLong() is roughly other static method from java.lang.Long shape which converts String into long value. parseLong() too throws java.lang.NumberFormatException if provided String value is non convertible into long together with too provides add-on overloaded method which accepts radix to convert octal together with hexadecimal long values inward String format. Most of the String to long conversion uses parseLong for conversion part, inward fact Long.valueOf() too calls parseLong method to convert long to String. parseLong() too accepts negative sign every bit commencement graphic symbol inward string input. Though "l" together with "L" values are non permitted within String input together with throw NumberFormatException.

long negativeLong = Long.parseLong("-1024"); //represent negative long value
long incorrectLong = Long.parseLong("-1024L"); // "L" is non permitted trial inward NumberFormatException

Long.decode() together with Long Constructor
Other ii methods using a java.lang.Long constructor which accepts String together with Long.decode() too operate inward similar fashion. They too throw NumberFormatException if String is non convertible into long. Long.decode() is expert inward price it accepts "#" every bit indication of hexadecimal long values. It too convey "0" for octal long numbers, "0x" together with "0X" for hexadecimal long numbers. Here is an trial of using Long.decode() method to convert hexadecimal long String value into long primitive number:

long expose = Long.decode("#FFFF");  //65535 inward decimal

see code trial department for to a greater extent than examples of decode() method of Long class.

Code Example - String to long conversion inward Java
Here is consummate code trial of all iv ways to convert String to long inward Java discussed inward this Java tutorial.

/**
 * Java programme to convert String to long inward Java. In this programme nosotros volition come across 4 examples
 * to convert String to long primitive inward Java.
 * @author Javin Paul
 */

public class StringToLong {

    public static void main(String args[]) {
       String strLong = "2323232";
     
       //Convert String to long using Long.valueOf(),better because
       //it tin dismiss cache oft used long values
       long expose = Long.valueOf(strLong);
       System.out.println("String to long conversion using valueOf :" + number);
     
       //Convert String to long inward Java using java.lang.Long object
       strLong ="4444444444444444444";
       Long numberObject = new Long(strLong);
       number = numberObject; //auto boxing volition convey attention of long to Long conversion
       System.out.println("String to long conversion trial using Long object: " + number);
     
       //Convert String to long alongside Long.parseLong method inward Java
       strLong="999999999999999999";
       Long parsedLong = Long.parseLong(strLong) ;
       number = parsedLong; //auto-boxing volition convert Long object to primitive long
       System.out.println("String to long conversion using parseLong() method: " + number);
     
       //Convert String to long using Long.decode() method inward Java
       strLong ="-1023454";
       number = Long.decode(strLong);
       System.out.println("String to long conversion using decode() method: " + number);
     
       //String to long inward hexadecimal format
       strLong ="0xFFFF";
       number = Long.decode(strLong);
       System.out.println("String to long trial inward hex format decode() method: " + number);
   
       //String to long inward octal format
       strLong ="0777";
       number = Long.decode(strLong);
       System.out.println("String to long trial inward octal format decode() method: " + number);
    }
}

Output:
String to long conversion using valueOf :2323232
String to long conversion trial using Long object: 4444444444444444444
String to long conversion using parseLong() method: 999999999999999999
String to long conversion using decode() method: -1023454
String to long trial inward hex format decode() method: 65535
String to long trial inward octal format decode() method: 511


That’s all on How to convert String to long inward Java. We own got seen iv ways to modify String values to long e.g. Long.valueOf(), Long.parseLong() together with Long.decode() method along alongside classic constructor approach for converting String to long inward Java. I personally prefer Long.valueOf() nigh of the fourth dimension together with Long.parseLong() inward residuum of fourth dimension to convert String to long.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures together with Algorithms: Deep Dive Using Java
How to practise executable JAR inward Java

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Parse Or Convert String To Long Inwards Coffee - Four Examples"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel