1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Convert Integer to numeric primitive data types example This example shows how an Integer object can be converted into below given numeric data types - Convert Integer to byte - Convert Integer to short - Convert Integer to int - Convert Integer to float - Convert Integer to double */ public class IntegerToNumericPrimitiveTypesExample { public static void main(String[] args) { Integer intObj = new Integer("10"); //use byteValue method of Integer class to convert it into byte type. byte b = intObj.byteValue(); System.out.println(b); //use shortValue method of Integer class to convert it into short type. short s = intObj.shortValue(); System.out.println(s); //use intValue method of Integer class to convert it into int type. int i = intObj.intValue(); System.out.println(i); //use floatValue method of Integer class to convert it into float type. float f = intObj.floatValue(); System.out.println(f); //use doubleValue method of Integer class to convert it into double type. double d = intObj.doubleValue(); System.out.println(d); } } /* Output of the program would be : 10 10 10 10.0 10.0 */ |
more data type
hexadecimal
octal
etc.
0
down vote
favorite
String boxVal = “FB”;
Integer val = Integer.parseInt(boxVal, 16);
System.out.println(val); //prints out 251
byte sboxValue = (byte) val;
System.out.println(“sboxValue = ” + Integer.toHexString(sboxValue));
For Hexdecimal you can use this
Integer.parseInt(“INteger value”, 16);
Integer val = Integer.parseInt(“Integer value”, 16);
byte sboxValue = (byte) val;
//for integer to byte
Integer i=null;
int j=i;
system.out.println(“j”);
wat’s the output for this?
any one, do you know?
Jeevan you will get Null pointer Exception with following code
Jeevan you will get Null pointer Exception
I have 2 questions. Under which circumstance would someone use Integer instead of int. And also, when would someone need to use the object to primitive type conversion and vice versa. Thanks in advance