Convert int to primitive types
In this chapter you will learn:
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 j a va 2 s. c om*/
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:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types