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 ofttimes asked questions past times a beginner who has started learning Java programming linguistic communication in addition to 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 recall few things piece dealing amongst long in addition to String outset of all long is primitive type which is wrapped past times Long wrapper course of educational activity in addition to String is an Object inward Java backed past times grapheme array. Before Java five yous demand to convey an extra footstep to convert Long object into long primitive exactly afterwards introduction of autoboxing in addition to 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 in addition to nosotros volition besides analyze pros in addition to 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 mill method from java.lang.Long course of educational activity which accepts an String declaration in addition to 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 in addition to MIN value of Long etc. Though negative sign tin survive included to announce negative long value every bit outset character. Main wages of valueOf() is that its an static mill method in addition to tin cache ofttimes used long value, since long is an immutable object its prophylactic to reuse cached long values. valueOf() is besides an overloaded method which accepts radix every bit 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 in addition to returns same Long object which volition render truthful if compared amongst equality operator "==". yous tin besides expect code of valueOf() method on java.lang.Long course of educational activity to verify it.

String to long - Long.parseLong
Long.parseLong() is closed to other static method from java.lang.Long course of educational activity which converts String into long value. parseLong() besides throws java.lang.NumberFormatException if provided String value is non convertible into long in addition to besides provides improver overloaded method which accepts radix to convert octal in addition to hexadecimal long values inward String format. Most of the String to long conversion uses parseLong for conversion part, inward fact Long.valueOf() besides calls parseLong method to convert long to String. parseLong() besides accepts negative sign every bit outset grapheme inward string input. Though "l" in addition to "L" values are non permitted within String input in addition to throw NumberFormatException.

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

Long.decode() in addition to Long Constructor
Other 2 methods using a java.lang.Long constructor which accepts String in addition to Long.decode() besides operate inward similar fashion. They besides throw NumberFormatException if String is non convertible into long. Long.decode() is skilful inward damage it accepts "#" every bit indication of hexadecimal long values. It besides convey "0" for octal long numbers, "0x" in addition to "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 divulge = 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 4 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 cache ofttimes used long values
       long divulge = 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 assist 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 possess got seen 4 ways to modify String values to long e.g. Long.valueOf(), Long.parseLong() in addition to Long.decode() method along amongst classic constructor approach for converting String to long inward Java. I personally prefer Long.valueOf() nearly of the fourth dimension in addition to Long.parseLong() inward residuum of fourth dimension to convert String to long.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
How to exercise executable JAR inward Java

Belum ada Komentar untuk "How To Parse Or Convert String To Long Inwards Coffee - Iv Examples"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel