String to number 문자 -> 숫자
String strNum = "10";
int i_num = Integer.parseInt(strNum); //return int
int i_num2 = Integer.valueOf(strNum);//return integer, call parseInt
double d_num = Double.parseDouble(strNum); //return double
double d_num2 = Double.valueOf(strNum);//return Double , call parseDouble
float f_num = Float.parseFloat(strNum); //return float
float f_num2 = Float.valueOf(strNum);//return Float, call parseFloat
long l_num = Long.parseLong(strNum); //return long
long l_num2 = Long.valueOf(strNum);//return Long, call parseLong
short s_num = Short.parseShort(strNum); //return short
short s_num2 = Short.valueOf(strNum);//return Short, call parseShort
If you need object-> valueOf
If you need data or calculation ->parseInt
number to String 숫자 -> 문자
int i_num = 10;
float f_num = (float) 10.10;
double d_num = 10.10;
String s_num;
s_num = String.valueOf(i_num);
s_num = Integer.toString(i_num);
s_num = "" + i_num;
s_num = String.valueOf(f_num);
s_num = Float.toString(f_num);
s_num = String.valueOf(d_num);
s_num = Double.toString(d_num);
Speed
toString > valueOf > ""+i_num;
Difference between valueOf and toString
- String.valueOf(Object) will return "null",
- Object::toString() will throw a null pointer exception.
String str = null;
System.out.println(String.valueOf(str)); // it will print null
System.out.println(str.toString()); // it will throw NullPointerException
integer <-> decimal
double d_num = 10.101010;
float f_num = (float) 10.1010;
int i_num;
i_num = (int) d_num;
i_num = (int) f_num;
double d_num2 = (double) i_num;
float f_num2 = (float) i_num;