How To Parse Or Convert String To Long Inward Coffee - Iv Examples

Advertisement

Masukkan script iklan 970x90px

How To Parse Or Convert String To Long Inward Coffee - Iv Examples

Minggu, 10 Mei 2020

How to convert a string to long inward Java is 1 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 1 information type to another. Converting String to long is similar to converting String to Integer inward Java, in-fact if you lot know how to convert String to Integer than you lot tin convert String to Long past times next same procedure. Though you lot demand to think few things spell dealing amongst long together with String get-go of all long is primitive type which is wrapped past times Long wrapper course of educational activity together with String is an Object inward Java backed past times graphic symbol array. Before Java five you lot demand to bring an extra pace to convert Long object into long primitive only later 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 iii dissimilar ways to convert String to long past times code instance together with nosotros volition too analyze pros together with cons of each approach.


How to convert String to long inward Java

Here are iii 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 manufacturing flora method from java.lang.Long course of educational activity 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 whatever 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 live on included to announce negative long value equally get-go character. Main payoff of valueOf() is that its an static manufacturing flora method together with tin cache oft used long value, since long is an immutable object its prophylactic to reuse cached long values. valueOf() is too an overloaded method which accepts radix equally minute declaration to convert hexadecimal or octal String to long. Here is an instance 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 amongst equality operator "==". you lot tin too expect code of valueOf() method on java.lang.Long course of educational activity to verify it.

String to long - Long.parseLong
Long.parseLong() is about other static method from java.lang.Long course of educational activity 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 equally get-go 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 resultant inward NumberFormatException

Long.decode() together with Long Constructor
Other 2 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 practiced inward price it accepts "#" equally indication of hexadecimal long values. It too bring "0" for octal long numbers, "0x" together with "0X" for hexadecimal long numbers. Here is an instance of using Long.decode() method to convert hexadecimal long String value into long primitive number:

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

see code instance 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 instance of all iv ways to convert String to long inward Java discussed inward this Java tutorial.

/**
 * Java computer programme to convert String to long inward Java. In this computer programme nosotros volition run into 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 cache oft used long values
       long publish = 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 bring attention of long to Long conversion
       System.out.println("String to long conversion instance using Long object: " + number);
     
       //Convert String to long amongst 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 instance inward hex format decode() method: " + number);
   
       //String to long inward octal format
       strLong ="0777";
       number = Long.decode(strLong);
       System.out.println("String to long instance inward octal format decode() method: " + number);
    }
}

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


That’s all on How to convert String to long inward Java. We direct keep seen iv ways to modify String values to long e.g. Long.valueOf(), Long.parseLong() together with Long.decode() method along amongst classic constructor approach for converting String to long inward Java. I personally prefer Long.valueOf() close 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 do executable JAR inward Java