본문 바로가기

공부/자바 OOP

5월31일 - Wrapper 클래스

Wrapper 클래스

의미 : 기본 자료형을 참조형화 해놓은 클래스들

주로 java.lang 팩키지에 선언되어있음

기본형 : boolean, byte, int, long, float, double, char

기본형을 한번 더 포장해준 것 = Wrapper 클래스

참조형 : Boolean, Integer, Long, Float, Double, Character 

 

1) Boolean 클래스

boolean flag=true;
//flag. 에러
Boolean b1=new Boolean(true);
Boolean b2=new Boolean("false");
		
System.out.println(b1.toString());//"true"
System.out.println(b2); //false

2) Integer 클래스

int a=3;
Integer inte1=new Integer(5);
Integer inte2=new Integer("7");
		
System.out.println(inte1); //5
System.out.println(inte2.doubleValue()); //7.0
		
System.out.println(Integer.toBinaryString(9)); //2진수 "1001" 나오는 건 String 형임
System.out.println(Integer.toOctalString(9));  //8진수 "11"
System.out.println(Integer.toHexString(9));    //16진수 "9"
System.out.println(Integer.parseInt("4"));      //string->int 형으로 변환 4
		
System.out.println(Integer.max(6,8));  //8
System.out.println(Integer.min(6,8)); //6

3) Long 클래스

long I=2L;  //접미사 L은 생략가능
Long I1=new Long(4);
Long I2=new Long(6);
System.out.println(I1); //4
System.out.println(I2.longValue()); //6
		
System.out.println(Long.parseLong("8")); //8

4) Double 클래스

double d=1.2;
Double d1=new Double(3.4);
Double d2=new Double("5.6");
		
//문제) d1과 d2의 합을 구하시오
int hap=d1.intValue()+d2.intValue();
System.out.println(hap); //3+5=8 

5) Character 클래스

char c='R';
Character ch=new Character('T');
System.out.println(c); //R
System.out.println(ch); //T
		
System.out.println(Character.isAlphabetic(5));  //false
System.out.println(Character.isAlphabetic('가')); //true
System.out.println(Character.isAlphabetic('a')); //true
System.out.println(Character.isAlphabetic('!')); //false
		
//Deprecated. 절판
System.out.println(Character.isSpace(' '));  //밑줄쳐진건 절판된것 
System.out.println(Character.isWhitespace(' ')); //isSpace대신

연습문제

문제 1) 대소문자를 서로 바꿔서 출력하시오

String str="Gone With The  Wind";
		for(int idx=0;idx<str.length();idx++){
			char ch=str.charAt(idx);
			if(Character.isUpperCase(ch)){
				System.out.print(Character.toLowerCase(ch));
			}else if(Character.isLowerCase(ch)){
				System.out.print(Character.toUpperCase(ch));
			}else{
				System.out.print(ch);
			}//if end
		}//for end

String
StringBuffer
StringBuilder
StringTokenizer : 문자열 분리 -java.util 팩키지

문자열 가공하는 경우 속도 차이가 있음 

: String < StringBuffer < StringBuilder 

 

 

ObjectArray 객체배열

예시)

Integer[] inte={new Integer(3),
			 	 new Integer(5),
				 new Integer(7)};
		
for(int idx=0; idx<inte.length;idx++){
	System.out.println(inte[idx].toString());
}//for end
        
출력결과
3
5
7

 

Calendar 클래스 -util 패키지

날짜 관련클래스 종류
- Date
- Calendar
- GregorianCalendar - 우리가 평소 보는 달력, 기원전인지 후인지

현재 시스템의 날짜 

 

'공부 > 자바 OOP' 카테고리의 다른 글

6월4일 - 상속 + 객체지향(다향성, object) + exception  (0) 2019.06.04
6월3일 - 상속  (0) 2019.06.03
5월30일 - this() / static / final  (0) 2019.05.30
5월29일 - Class  (0) 2019.05.29
5월28일 - 메소드 + 정렬  (0) 2019.05.28