Convert string to short

In this chapter you will learn:

  1. How to decode a string to short value
  2. How to convert string to a short value

Decode a string to short value

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

+/- 0x HexDigits 
+/- 0X HexDigits 
+/- # HexDigits 
+/- 0 OctalDigits

Small demo to illustrates the decode method.

public class Main {
  public static void main(String[] args) {
    /* j a  v  a 2s .co m*/
    System.out.println("Decimal 10:"+Short.decode("10"));
    System.out.println("Octal 10:"+Short.decode("010"));
    System.out.println("Hex F:"+Short.decode("0XF"));
    
    
    System.out.println("Negative Hex F:"+Short.decode("-0XF"));
  }
}

The output:

Convert string to a short value

Short class has the following methods we can use to convert a string to a short type value.

  • static short parseShort(String s) parses the string argument as a signed decimal short.
  • static short parseShort(String s, int radix) parses the string argument as a signed short in the radix specified by the second argument.
  • static Short valueOf(short s) returns a Short instance representing the specified short value.
  • static Short valueOf(String s) returns a Short object holding the value given by the specified String.
  • static Short valueOf(String s, int radix) returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.

The following code demonstrates the methods listed above.

public class Main {
  public static void main(String[] args) {
//from  ja v a 2s.  co m
    System.out.println(Short.parseShort("10"));
    System.out.println(Short.parseShort("10",8));
    
    System.out.println(Short.valueOf("10"));
    System.out.println(Short.valueOf("10",8));
    

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to reverse the bytes in a short
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