Java short type conversion

In this chapter you will learn:

  1. How to convert Short to byte, double, float, int, long and short
  2. How to decode a string to short value
  3. How to convert string to a short value
  4. How to convert short value to string

Convert Short to byte, double, float, int, long and short

The following list have the methods which we can use to convert short value.

  • byte byteValue() returns the value of this Short as a byte.
  • double doubleValue() returns the value of this Short as a double.
  • float floatValue() returns the value of this Short as a float.
  • int intValue() returns the value of this Short as an int.
  • long longValue() returns the value of this Short as a long.
  • short shortValue() returns the value of this Short as a short.
public class Main {
  public static void main(String[] args) {
    Short shortObject = new Short("10");
    byte b = shortObject.byteValue();
    System.out.println("byte:"+b);
/*j  a va2 s.c  o m*/
    short s = shortObject.shortValue();
    System.out.println("short:"+s);

    int i = shortObject.intValue();
    System.out.println("int:"+i);

    float f = shortObject.floatValue();
    System.out.println("float"+f);

    double d = shortObject.doubleValue();
    System.out.println("double:"+d);

    long l = shortObject.longValue();
    System.out.println("long:"+l);
  }
}

The output:

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  va 2 s. c  o 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  j a  va2s . c om*/
    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:

Convert short value to string

To convert short value to string we can use the following methods.

  • String toString() returns a String object representing this Short's value.
  • static String toString(short s) returns a new String object representing the specified short.
public class Main {
  public static void main(String[] args) {
    Short shortValue = new Short("10");
    System.out.println(shortValue.toString());
    //from j a  va 2s.  c o  m
    System.out.println(Short.toString((short)10));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Java int type introduction
  2. What is octal integer and how to use it in Java
  3. What is hexadecimal integer and how to use it in Java
  4. Find out Max, min value of an integer type variable
  5. How to create Integer using its constructors
  6. How to compare two integer values
  7. How to reverse and rotate the integer in binary format
  8. How to bit oriented operation on int type value
  9. How to get the sign of an Integer
  10. How to get the leading and trailing zeros
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class