Convert String to long value

In this chapter you will learn:

  1. How to decode a string to create long value
  2. How to convert and parse long value from string
  3. How to convert or parse string to long with radix

Decode a string to create long value

static Long decode(String nm) decodes a String into a Long. decode(String nm) accepts decimal, hexadecimal, and octal numbers given by the following grammar:

The following code decodes a long value in hexadecimal format.

public class Main {
  public static void main(String[] args) {
    Long longObject = Long.decode("0Xff");
    System.out.println(longObject);// j a  v a  2s .  co m

  }
}

The output:

Convert and parse long value from string

The following methods are useful when we have a string value and want to convert it to long type.

  • static long parseLong(String s) parses the string argument as a signed decimal long.
  • static long parseLong(String s, int radix) parses the string argument as a signed long in the radix.
  • static Long valueOf(long l) returns a Long instance representing the specified long value.
  • static Long valueOf(String s) returns a Long object holding the value of the specified String.
  • static Long valueOf(String s, int radix) returns a Long holding the value from the String with the radix.

The following code converts a string value to a long type value:

public class Main {
  public static void main(String[] args) {
    System.out.println(Long.parseLong("12345"));
/*from  java  2  s . c o m*/
  }
}

The output:

Convert or parse string to long with radix

To convert or parse string with radix,

public class Main {
  public static void main(String[] args) {
    System.out.println(Long.parseLong("10",8));
/* ja v a2  s  .co  m*/
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert long value to string
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing