ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] Wrapper Class
    카테고리 없음 2022. 9. 8. 10:57
    public class WrapperClass01 {
    	public static void main(String[] args) {
    		Integer i1  = new Integer(100);
    		Integer i2 = new Integer(100);
    		
    		// 참조 변수(레퍼런스 변수)들의 비교
    		if( i1 == i2 )System.out.println("i1과 i2가 값이 같습니다.");
    		else System.out.println("i1과 i2가 값이 다릅니다.");
    		System.out.println("i1==i2 ? "+(i1==i2));
    		
    		// 실제 인스턴스에 저장된 값들의 비교
    		if( i1.equals(i2)) System.out.println("i1과 i2가 값이 같습니다.(equals)");
    		else System.out.println("i1과 i2가 값이 다릅니다.(equals)");
    		System.out.println("i1.equals(i2)="+i1.equals(i2));
    		
    		System.out.println("i1.compareTo(i2)="+i1.compareTo(i2));
            // 실제 인스턴스에 저장된 값들의 비교 - 뺄셈연산으로 비교 앞에숫자가 크면 양수 
    		// 뒤에숫자가 크면 음수반환 같으면 0 반환
    		
    		//Object 부모 클래스의 메서드 오버라이딩
    		System.out.println("i1.toString()=" + i1.toString());
    		System.out.println("i2=" + i2);
    		// toString()의 리턴값은  String
            
    		//표현가능한 가장 큰값(스태틱 변수)
    		System.out.println("MAX_VALUE=" + Integer.MAX_VALUE);
    		//표현가능한 가장 작은 값(스태틱 변수)
    		System.out.println("MIN_VALUE=" + Integer.MIN_VALUE);
    			    
    		//비트 바이트로 표현한 저장 크기 
    		System.out.println("SIZE=" + Integer.SIZE + " bits");
    		System.out.println("BYTES=" + Integer.BYTES + " bytes");
    		//자료형 
    		System.out.println("TYPE=" + Integer.TYPE);
    		
    		int i = 10;
    		Integer inti = (Integer)i;  // Integer inti = Integer.valueOf(i);
    		
    		int i3 = inti + 10;   // 참조형과 기본형간의 연산 가능
    		System.out.println(i3);
    		
    		Integer inti2 = new Integer(20);
    		System.out.println("inti2 ="+inti2);
    		int i4 = (int)inti2;  // 참조형을 기본형으로 형변환도 가능(형변환 생략가능)
    		System.out.println("i4    ="+i4);
    		
    		Integer inti3 = inti2 + i3;
    		System.out.println("inti2 + i3 ="+inti3);
    	}
    }
    import java.math.BigInteger;
    
    public class WrapperClass02 {
    	public static void main(String[] args) 
    		// BigInteger k = 1;  // 에러
    		BigInteger k = BigInteger.ONE;
    		//System.out.println(k);
    		//k = k.add( BigInteger.ONE );
    		//System.out.println(k);
    		
    		BigInteger fact = BigInteger.ONE;
    		for(int i=1; i<=40; i++) {
    			//1부터 현재 i값까지 모두 곱셈을 하여 출력하는 반복문 실행
    			fact = BigInteger.ONE;
    			// 반복을 제어해주는 변수는 k(BigInteger)
    			// 일반 정수를 BigInteger 로 변환하는 스태틱 매서드 BigInteger.valueOf(n)
    			// k.compareTo( BigInteger.valueOf(i)  ) <=0  k가 i보다 작거나 같은 동안 반복
    			for( k = BigInteger.ONE ; 
    					k.compareTo( BigInteger.valueOf(i)  ) <=0 ;
    						k = k.add(BigInteger.ONE)  ) {
    				fact = fact.multiply(k);				
    			}
    			System.out.printf("%d! = %s\n" , i, fact.toString() );
    		}
    	}
    }

    댓글

Designed by Tistory.