기본형 | 래퍼클래스 | 생성자 | 활용예 |
boolean | Boolean | Boolean(boolean value) Boolean(String s) |
Boolean b = new Boolean(true); Boolean b 2= new Boolean("true"); |
char | Character | Character(char value) | Character c = new Character('a'); |
byte | Byte | Byte(byte value) Byte(String s) |
Byte b = new Byte(10); Byte b2 = new Byte("10"); |
short | Short | Short(short value) Short(String s) |
Short s = new Short(10); Short s2 = new Short("10"); |
int | Integer | Integer(int value) Integer(String s) |
Integer i = new Integer(100); Integer i2 = new Integer("100"); |
long | Long | Long(long value) Long(String s) |
Long l = new Long(100); Long l2= new Long("100"); |
float | Float | Float(double value) Float(float value) Float(String s) |
Float f = new Float(1.0); Float f2 = new Float(1.0f); Float f3 = new Float("1.0f"); |
double | Double | Double(double value) Double(String s) |
Double d = new Double(1.0); Double d2 = new Double("1.0"); |
주의 : 생성자의 매개변수로 문자열을 제공할 때, 각 자료형에 알맞은 문자열을 사용해야한다.
예) new Integer("1.0"); -> NumberFormatException 발생
- 적용
boolean boo1=true;
Boolean boo2=new Boolean("false"); //Deprecated 절판
Boolean boo3=true;
System.out.println(boo1);
System.out.println(boo2);
System.out.println(boo3);
System.out.println(boo3.toString());//"true"
///////////////////////////////////////////////////////
int in1=3;
Integer in2=new Integer(5);
Integer in3=7;
System.out.println(in1);
System.out.println(in2);
System.out.println(in3);
System.out.println(in2.toString()); //"5"
System.out.println(in3.doubleValue()); //7.0
System.out.println(Integer.toBinaryString(15)); //"1111"
System.out.println(Integer.toOctalString(15)); //"17"
System.out.println(Integer.toHexString(15)); //"f"
System.out.println(Integer.sum(2, 4)); //6
System.out.println(Integer.max(2, 4)); //4
System.out.println(Integer.min(2, 4)); //2
//★★★
System.out.println(Integer.parseInt("123")); //"123" -> 123 변환
//NumberFormatException 발생
//System.out.println(Integer.parseInt("KOREA"));
////////////////////////////////////////////////////////////////
long lo1=3L;
Long lo2=new Long(5);
Long lo3=7L;
double dou1=2.4;
Double dou2=new Double("3.5");
Double dou3=6.7;
//문)실수형값들 중에서 정수의 합을 구하시오 (2+3+6)
int hap=(int)dou1+dou2.intValue()+dou3.intValue();
System.out.println(hap);//11
//////////////////////////////////////////////////
char ch1='R';
Character ch3=new Character('a');
Character ch2='m';
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.toLowerCase('R'));
System.out.println(Character.toUpperCase('a'));
'Backend > JAVA' 카테고리의 다른 글
[JAVA] 상속 (Inheritance) (0) | 2023.03.20 |
---|---|
[JAVA] quiz : 성적프로그램 OX 표시하기 (0) | 2023.03.20 |
[JAVA] final (0) | 2023.03.20 |
[JAVA] static (0) | 2023.03.16 |
[JAVA] this와 this() (0) | 2023.03.16 |