C. Java

java 자바 wrapper 클래스

sfeg 2017. 7. 2. 22:43



Wrapper 클래스 : 자바 기본 데이터형에 해당하는 클래스
 
1. Integer 클래스
 => (int,short,integer,long,double)Value() 메소드를 제공, Intert.parseInt()메소드 제공,
        Integer.(toBinaryString,toOctalString,toHexStrion ) 메소드 제공
 
    ex) class test{
               public static void main(String[] args){
                  Integer n1 = new Integer(10);         //정수값을 인자로 갖는 생성자로 Integer 객체 생성
                  Integer n2 = new Integer("10");       //문자열을 인자로 갖는 생성자로 Integer 객체 생성
 
                  int  a = n1.intValue();
                  int  b = n2.intValue();
                  
                  int  c = a+b;                   < /FONT >
                  System.out.println("두 정수의 합은 = " + c );< /FONT >
 
                  System.out.println("합을 2진수로 =" + Integer.toBinaryString(c) );
                  System.out.println("합을 8진수로 =" + Integer.toOctalString(c));
                  System.out.println("합을 16진수로 =" + Integer.toHexString(c));
               }
          }
 
 
2.오토박싱 오토언박싱
    JDK 5.0 에서 지원하는방식
    오토   박싱 : 값형식 변수인 기본 자료형이(int,...)  레퍼런스 형식 변수인 Wrapper 클래스형(Integer...) 으로 변환하는것을 말한다.
                      즉 스택상의 메모리 공간에 있던값을 힙상의 객체를 생서하여 복사하는것을 말한다.
    오토언박싱 : 오토박싱과는 반대로 힙영역을 스택영역으로 복사하는것을 말한다.
 
     1.4에서는 xxxValue()메소드를 이용해서 변환했지만 1.5에서는 바로 변환이 가능해졌다.
  
     ex)  int n=10;
            int s;
 
            Integer num;
            Integer ss  = new Integer(20);
 
            num = n;   // 오토박싱
            s      = ss; // 오토 언 박싱
   
 
3. Long클래스
   ex) class Test [
              punlic static void main(String[] args){
                     Long    num =  new Long(10);
                     String   strN = "10";
                     
                     // parseXXX : 문자열에 해당하는 값을 해당 기본 자료형으로 변경한다. 
                     long    ss = Long.parseLong(strN);

                    //문자열에 해당되는 값을 해당 Wrapper클래스형 객체로 변경한다.
                    Long num2 = Long.valueOf(strN); 
              }
         }

 
4. Character 클래스 

   Character.isDefined(문자 데이타)                        :문자 데이타가 유니코드이면 true 아니면 false
                   .isDigit(문자 데이타)                             :문자 데이타가 숫자면 true 아니면 false
                   .isLetter(문자 데이타)                           :문자 데이타가 문자면 true 아니면 false

                   .isLetterOrDigit(문자 데이타)                :문자 데이타가 문자나 숫자면 true 아니면 false

                   .isLowerCase(문자 데이타)                   :문자 데이타가 소문자이면 true 아니면 false

                   .isSpace(문자 데이타)                           :문자 데이타가 공백이면 true 아니면 false

                   .isUpperCase(문자 데이타)                   :문자 데이타가 대문자면 true 아니면 false

                   .toLowerCase(문자 데이타)                  :문자데이타를 소문자로 변형

                   .toUpperCase(문자 데이타)                  :문자데이타를 대문자로 변형