래퍼(Wrapper) 클래스2

public class Wrapper1 {

	public static void main(String[] args) {
		Integer iObj = 10; //박싱
		//Integer iObj = new Integer(10);
		Double dObj = 3.14;
		//Double dObj = new Double(3.14);
		
		System.out.println(iObj);
		System.out.println(dObj); //원래는 주소값 참조 이 경우는 내용 출력
		System.out.println();

박싱 / 언박싱 예제

public class Wrapper2 {

	public static void main(String[] args) {
		int num = 10; // 기본적으로는 값만 넣는 int로 선언
		Integer iObj = 10; // 박싱

		Double dObj = 3.14; // 박싱

		System.out.println(iObj);
		System.out.println(dObj + 10); // 원래는 주소값 참조 이 경우는 내용 출력
		System.out.println();

		int num1 = iObj.intValue(); // 언박싱
		double num2 = dObj.doubleValue(); // 언박싱

		System.out.println(num1);
		System.out.println(num2);

	}

}
  • System.out.println(iObj); 에서 내용이 출력되는 이유 : Integr클래스에도 toString()메소드가 있어서, 오버라이딩된 기능을 사용하기 때문에.

Integer클래스 toString()메소드

 public String toString() {
        return toString(value);
    }

Number클래스

Number클래스는 모든 래퍼 클래스의 부모 클래스.

Number클래스의 추상 메소드 호출의 예

import java.lang.Number;
public class NumberClass {

	public static void main(String[] args) {
		Integer num1 = new Integer(29);
		// Integer num1 = 29;
		System.out.println(num1.intValue());
		System.out.println(num1.doubleValue());

		double d = 3.14;
		Double num2 = new Double(3.14);
		System.out.println(num2.intValue());
		System.out.println(num2.doubleValue());
	}

}
  • java.lang.Number

래퍼 클래스의 다양한 static메소드

Integer클래스의 다양한 static메소드

public class Wrapper_static {

	public static void main(String[] args) {
		//클래스 메소드를 통한 인스턴스 생성 방법 두 가지
		Integer n1 = Integer.valueOf(5); //숫자 기반 Integer 인스턴스 생성
		Integer n2 = Integer.valueOf("1024"); //문자열 기반 Integer 인스턴스 생성

		//대소 비교와 합을 계산하는 클래스 메소드
		System.out.println("큰 수: " + Integer.max(n1, n2));
		System.out.println("작은 수: " + Integer.min(n1, n2));
		System.out.println("합: " + Integer.sum(n1, n2));
		System.out.println();

		//정수에 대한 2진 8진 16진 표현 결과를 반환하는 클래스 메소드
		System.out.println("12의 2진 표현: " + Integer.toBinaryString(12));
		System.out.println("12의 8진 표현: " + Integer.toOctalString(12));
		System.out.println("12의 16진 표현: " + Integer.toHexString(12));
	}

}

[결과]
큰 수: 1024
작은 수: 5: 1029

122진 표현: 1100
128진 표현: 14
1216진 표현: c

BigInteger클래스

매우 큰 정수 표현을 위한 java.math.BigInteger클래스

import java.math.BigInteger;

public class BigIntegerClass {

	public static void main(String[] args) {
		System.out.println("최대 정수: " + Long.MAX_VALUE);
		System.out.println("최소 정수: " + Long.MIN_VALUE);
		System.out.println();
		
		BigInteger big1 = new BigInteger("100000000000000000000");
		BigInteger big2 = new BigInteger("-99999999999999999999");
		
		BigInteger r1 = big1.add(big2);
		System.out.println("덧셈 결과: " + r1 );
		
		BigInteger r2 = big1.multiply(big2);
		System.out.println("덧셈 결과: " + r2 );
		System.out.println();
		
		int num = r1.intValueExact();
		System.out.println("From BigInteger: " + num);

	}

}

[결과]
최대 정수: 9223372036854775807
최소 정수: -9223372036854775808

덧셈 결과: 1
덧셈 결과: -9999999999999999999900000000000000000000

From BigInteger: 1

BigDecimal클래스

오차없는 실수 표현을 위한 BigDecimal클래스.

import java.math.BigDecimal;

public class BigDecimalTeat {

	public static void main(String[] args) {
		BigDecimal d1 = new BigDecimal("1.6");
		BigDecimal d2 = new BigDecimal("0.1");

		System.out.println("덧셈 결과: " + d1.add(d2));
		System.out.println("곱셈 결과: " + d1.multiply(d2));
	}

}

Math클래스

자주 사용하는 Math클래스의 메소드에 대한 예제

public class MathClass {

	public static void main(String[] args) {
		System.out.println("원주율: " + Math.PI);
		System.out.println("2의 제곱근: " + Math.sqrt(2));

		System.out.println("2의 16승: " + Math.pow(2, 16));
	}

}

[결과]
원주율: 3.141592653589793
2의 제곱근: 1.4142135623730951
216: 65536.0

Random클래스

난수를 생성하는 클래스.

import java.util.Random;

public class RandomClass {

	public static void main(String[] args) {
		Random rd = new Random();

		for (int i = 0; i < 7; i++) {
			System.out.println(rd.nextInt(1000) + 1);
		}
	}

}
  • 1~1000사이의 난수를 7개 생성.
  • 실행마다 출력 값이 바뀐다.
import java.util.Random;

public class RandomClass {

	public static void main(String[] args) {
		Random rd = new Random(10); //10은 seed값

		for (int i = 0; i < 7; i++) {
			System.out.println(rd.nextInt(1000) + 1);
		}
	}

}
  • new Random(10) 의 10은 seed값.
  • seed값을 주면 매번 랜덤으로 숫자를 출력하지 않고 실행마다 같은 숫자가 출력된다.

StringTokenizer클래스

  • 사용자가 지정하는 구분자를 경계로 문자열을 나눠주는 클래스.

아래를 프로그래밍 하시오.

Scanner를 이용하여 한 라인을 읽고,
공백으로 분리된 어절이 몇 개인지 출력을 반복하는
프로그램을 작성하라. “exit”이 입력되면 종료한다.

package com.fxmx.simple;

import java.util.Scanner;
import java.util.StringTokenizer;

public class TokenizerTest {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String input;
		while (true) {
			System.out.println("문자를 입력하세요: ");
			input = sc.nextLine();
			StringTokenizer s1 = new StringTokenizer(input, " ");

			int count = 0;
			while (s1.hasMoreTokens()) {
				System.out.println(s1.nextToken() + ' ');
				count++;
			}
			System.out.println(count + "개의 문자입니다.");

			System.out.println("다시 하시겠습니까?");
			String YesOrNo = sc.nextLine();
			if (YesOrNo.equals("y") || YesOrNo.equals("Y"))
				continue;
			else if (YesOrNo.equals("n") || YesOrNo.equals("N"))
				System.out.println("exit");
			break;
		}

		System.exit(0);
	}
}

0개의 댓글