Java int type conversion
In this chapter you will learn:
- How to convert an integer value to byte, double, float, int, long and short
- How to decode a string and return an integer value
- How to convert string to integer
- How to convert integer to string
- 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.
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:
- What is Java long type
- How to mark an integer as long type
- Find out the max/min value and its size for long type
- How to create Long object
- How to compare two long values
- How to get the sign of the long value
- How to get the number of zero bits preceding and following
- How to reverse and rotate a long value