- Wrapper 클래스
Wrapper 클래스 : 8개의 기본자료형을 객채화 하기 위한 8개의 클래스 통칭
기본자료형 - 정수이므로 값만 가지고 있다. Wrapper 클래스
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
기본자료형과 참조자료형 사이의 형변환은 불가.(함수를 이용하여 처리)
단 기본자료형과 Wrapper 클래스 간의 형변환은 가능함.
기본자료형의 기본형(변수) <- Wrapper 클래스의 참조형(객체): auto UnBoxing
(값만 딱 가지고 있는 것)
Wrapper 클래스의 참조형(객체) <- 기본자료형의 기본형(변수): auto Boxing
(박스에 값을 집어넣고 부수적인 것들을 처리하는...?)
📌
package chap11;
public class WrapperEx1 {
public static void main(String[] args) {
//Integer : 정수형을 표현하는 객체
Integer i1 = new Integer(100); //deprecated 됨. 사용을 권장하지 않음.(쓸 수는 있다.)
Integer i2 = new Integer(100);
System.out.println("i1==i2 : "+(i1==i2)); //false : 같은 객체가 아니여서 거짓으로 나옴.
System.out.println("i1.equals(i2) : "+(i1.equals(i2))); //equals를 쓸 수 있으므로 Integer는 오브젝트의 하위 클래스.
i1 = 100; //참조형(객체) <- 기본형(변수) : Boxing
i2 = 100;
System.out.println("i1==i2 : "+(i1==i2)); //true
System.out.println("i1.equals(i2) : "+(i1.equals(i2))); //true
System.out.println("i1==100 : "+(i1==100)); //true : 자동으로 i1객체가 UnBoxing됨.
//equals 메서드 오버라이딩시 hachCode()도 오버라이딩 하도록 권장.
System.out.println("i1.hashCode():"+i1.hashCode()); //100 : 오버라이딩 됨.
System.out.println("i2.hashCode():"+i2.hashCode()); //100
System.out.println("System.identityHashCode(i1):"+System.identityHashCode(i1)); //실제 hachCode 값 : 객체의 참조값
System.out.println("System.identityHashCode(i2):"+System.identityHashCode(i2));
//int 형 정보
System.out.println("int 형의 최대값: "+Integer.MAX_VALUE);
System.out.println("int 형의 최소값: "+Integer.MIN_VALUE);
System.out.println("int 형의 bit수: "+Integer.SIZE);
//long 형 정보
System.out.println("long 형의 최대값: "+Long.MAX_VALUE);
System.out.println("long 형의 최소값: "+Long.MIN_VALUE);
System.out.println("long 형의 bit수: "+Long.SIZE);
//정수형 <= 문자열
int num = Integer.parseInt("123");
System.out.println(num+100);
num = Integer.parseInt("123",16); //16진수를 10진수 값으로 변경
System.out.println(num);
//10진수를 16진수로 출력하기
System.out.println(Integer.toHexString(num));
//10진수를 8진수로 출력하기
System.out.println(Integer.toOctalString(num));
//10진수를 2진수로 출력하기
System.out.println(Integer.toBinaryString(num));
}
}
- Character 클래스
: char 기본형의 Wrapper 클래스
📌
package chap11;
public class WrapperEx2 {
public static void main(String[] args) {
char[] date = {'A','a','가','9','-'};
for(char c : date) {
if(Character.isUpperCase(c))
System.out.println(c+" : 대문자");
else if(Character.isLowerCase(c))
System.out.println(c+" : 소문자");
else
System.out.println(c+" : 기타문자");
if(Character.isAlphabetic(c))
System.out.println(c+" : 문자");
else
System.out.println(c+" : 문자아님");
}
}
}
- switch 구문의 조건 값으로 사용 가능한 자료형
switch 구문의 조건 값으로 사용 가능한 자료형
byte, short, cahr, int, String
Byte, Short, Character, Integer, String
📌
package chap11;
public class WrapperEx3 {
public static void main(String[] args) {
Integer d = 0;
switch(d) {
default : System.out.println("switch 구문에서 사용되는 자료형");
}
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap12: 기본 API - Random (0) | 2023.04.16 |
|---|---|
| [Java] chap11: 기본 API(패키지 클래스) - 기본 API Exam 4-5 (format 메서드, 함수 구현) (0) | 2023.04.16 |
| [Java] chap11: 기본 API(패키지 클래스) - Math (0) | 2023.04.16 |
| [Java] chap11: 기본 API(패키지 클래스) - 기본 API Exam3.(delChara메서드 구현) (0) | 2023.04.16 |
| [Java] chap11: 기본 API(패키지 클래스) - StringBuffer (0) | 2023.04.16 |