Java int type conversion

In this chapter you will learn:

  1. How to convert an integer value to byte, double, float, int, long and short
  2. How to decode a string and return an integer value
  3. How to convert string to integer
  4. How to convert integer to string
  5. How to convert integer to binary, hexadecimal and octal format

Convert an integer value to byte, double, float, int, long and short

Integer class defines the following methods to convert int type value to other primitive types.

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

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

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

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

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

The output:

Decode a string and return an integer value

static Integer decode(String nm) decodes a String into an Integer.

decode(String nm) accepts decimal, hexadecimal, and octal numbers given by the following grammar:

public class Main {
  public static void main(String[] args) {
//from j a v  a  2s.co  m
    System.out.println(Integer.decode("010"));

  }
}

The output:

Convert string to integer

Converting string to int type value can be done through the following methods.

  • static int parseInt(String s) parses the string argument as a signed decimal integer.
  • static int parseInt(String s, int radix) parses the string argument in the radix specified by the second argument.
  • static Integer valueOf(int i) returns a Integer instance representing the specified int value.
  • static Integer valueOf(String s) returns an Integer object holding the value of the specified String.
  • static Integer valueOf(String s, int radix) returns an Integer from String based on the radix.
public class Main {
  public static void main(String[] args) {
/*ja  v  a 2s  .c  o m*/
    System.out.println(Integer.parseInt("010"));

  }
}

The output:

You can also indicate the radix.

public class Main {
  public static void main(String[] args) {
//from  j av a2s  . co m
    System.out.println(Integer.parseInt("010",8));

  }
}

The output:

Convert integer to string

Converting int type value to string can be done through the following methods.

  • String toString() returns a String object representing this Integer's value.
  • static String toString(int i) returns a String object representing the specified integer.
  • static String toString(int i, int radix) returns a string representation of the first argument in the radix specified by the second argument.
Calling the static methods Integer.toString is usually the choice.
public class Main {
  public static void main(String[] args) {
/*from  j av  a  2 s .c o  m*/
    System.out.println(Integer.toString(10));

  }
}

The output:

To indicate the radix during converting integer to string:

public class Main {
  public static void main(String[] args) {
/*from   ja  v a  2 s. c  o m*/
    System.out.println(Integer.toString(10, 8));

  }
}

The output:

Convert integer to binary, hexadecimal and octal format

An integer value can have hexadecimal and octal format other than decimal format. Integer class provides methods to convert an int value into various format.

  • static String toBinaryString(int i) returns the binary string for i.
  • static String toHexString(int i) returns a hes string for i.
  • static String toOctalString(int i) returns a octal string for i.
public class Main {
  public static void main(String[] args) {
/*from ja v a 2s .  c  o  m*/
    System.out.println(Integer.toBinaryString(10));
    System.out.println(Integer.toHexString(10));
    System.out.println(Integer.toOctalString(10));
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Java long type
  2. How to mark an integer as long type
  3. Find out the max/min value and its size for long type
  4. How to create Long object
  5. How to compare two long values
  6. How to get the sign of the long value
  7. How to get the number of zero bits preceding and following
  8. How to reverse and rotate a long value
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