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

How to convert a string to long inward Java is 1 of those oftentimes 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 yous know how to convert String to Integer than yous tin convert String to Long past times next same procedure. Though yous demand to holler back few things piece dealing amongst long together with String commencement of all long is primitive type which is wrapped past times Long wrapper course of written report together with String is an Object inward Java backed past times grapheme array. Before Java v yous demand to convey an extra stride to convert Long object into long primitive merely later on 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 larn three 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 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 course of written report 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 hold out included to announce negative long value equally commencement character. Main wages of valueOf() is that its an static mill method together with tin cache oftentimes 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 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 "==". yous tin too await code of valueOf() method on java.lang.Long course of written report to verify it.

String to long - Long.parseLong
Long.parseLong() is around other static method from java.lang.Long course of written report 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 commencement grapheme 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 final result 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 function inward similar fashion. They too throw NumberFormatException if String is non convertible into long. Long.decode() is practiced inward damage it accepts "#" equally indication of hexadecimal long values. It too convey "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 break = 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 plan to convert String to long inward Java. In this plan nosotros volition run 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 cache oftentimes used long values
       long break = 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 tending 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 guide keep seen iv ways to alter 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 residual 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

Komentar