시소당
1. Wrapper 클래스
Wrapper 클래스는 기본 데이터를 클래스로 만들 수 있게하는 클래스들 입니다. 기본데이터를 다른 데이터형으로 바꿀때 많이 사용합니다.
- byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue()
기본 데이터형을 Wrapper한 클래스에 xxxValue() 메소드를 이용해서 해당 데이터 형의 값을 얻을 수 있다.
또한 기본 데이터형은 작은 범위로는 캐스팅을 하고, 큰 범위로는 자동으로 데이터형을 변환 할 수 있습니다.
=>참조
- integer to String
int i = 14;
String str = Integer.toString(i);
or
String str = "" + i
- double to String
String str = Double.toString(i);
- long to String
String str = Long.toString(l);
- float to String
String str = Float.toString(f);
- String to integer
str = "14";
int i = Integer.valueOf(str).intValue();
or
int i = Integer.parseInt(str);
- String to double
double d = Double.valueOf(str).doubleValue();
- String to long
long l = Long.valueOf(str).longValue();
or
long l = Long.parseLong(str);
- String to float
float f = Float.valueOf(str).floatValue();
- decimal to binary (2진수)
int i = 14;
String str = Integer.toBinaryString(i);
- decimal to hexadecimal (16진수)
int i = 14;
String hexstr = Integer.toString(i, 16);
or
String hexstr = Integer.toHexString(i);
- hexadecimal (16진수 String) to integer
int i = Integer.valueOf("B8DA3", 16).intValue();
or
int i = Integer.parseInt("B8DA3", 16);
- ASCII code to String
int i = 14;
String aChar = new Character((char)i).toString();
- char to ASCII code (int)
char c = 'a';
int i = (int) c; // value 97 decimal
- String to ASCII code
String test = "abcd";
for ( int i = 0; i < test.length(); ++i ) {
char c = test.charAt( i );
int i = (int) c;
System.out.println(i);
}