본문 바로가기
ICIA 수업일지

2021.06.24 수업일지

by 주성씨 2021. 6. 26.

- 예외처리 다시보기

1. 모든 예외처리는 ~ exception으로 끝난다.
2. exception의 상위 클래스는 throwable
3. throwable의 상위 클래스는 object(super class)
4. RuntimeExecption의 하위 클래스는 반드시 써야하는건 아니다. 필수가 아니다.
5. 이외의 익셉션 클래스는 필수이다.

- 예외 처리

예제)

package ex1;

import java.util.Scanner;

public class Ex1_0624 {
	
	public static void main(String[] args) {

		Scanner keyboard = new Scanner(System.in);
		int[] arr = new int[100];
		for (int i = 0; i < 3; i++) {
			try {
				System.out.print("피제수 입력: ");
				int num1 = keyboard.nextInt();
				System.out.print("제수 입력: ");
				int num2 = keyboard.nextInt();
				System.out.print("연산결과를 저장할 배열의 인덱스 입력: ");
				int idx = keyboard.nextInt();
				arr[idx] = num1 / num2;
				System.out.println("나눗셈 결과는 " + arr[idx]);
				System.out.println("저장된 위치의 인덱스는 " + idx);
			} catch (ArithmeticException e) // catch(Throwable e)
			{
				System.out.println("제수는 0이 될 수 없습니다.");
				i--;
				continue;
			} catch (ArrayIndexOutOfBoundsException e) // catch(ArithmeticException e) 에러발생
			{
				System.out.println("유효하지 않은 인덱스 값입니다.");
				i--;
				continue;
			}
		}
	} // main method end
} // class end

ㄴ 트라이와 캐치는 항상 붙어다닌다.
ㄴ 예외는 트라이 캐치 내 어디서나 발생할 수 있다.
ㄴ 트라이 하나에 여러 캐치가 붙을 수 있다. // 많아야 2개?
ㄴ 반복문을 빨리 진행하고 싶을때 컨티뉴를 사용한다.
ㄴ 캐치를 순차적으로 예외 비교를 하고나서 적합한 예외처리에 적용된다.

			  catch (Exception e) // catch(Throwable e) catch(Exception e) 도 가능하다.
			{
				System.out.println("제수는 0이 될 수 없습니다.");
				i--;
				continue; 
			} catch (ArrayIndexOutOfBoundsException e) // catch(ArithmeticException e) 에러발생
			{
				System.out.println("유효하지 않은 인덱스 값입니다.");
				i--;
				continue;
			}

ㄴ 이와 같이 1차 캐치에 Exception을 쓸 수 있지만 2차 캐치가 받을 기회가 없어져서 2차 캐치에 오류가 발생한다.
ㄴ 만약에 Exception을 사용하고 싶으면 자손 타입을 먼저 1차에 기입하고 마지막에 부모 타입 예외처리를 넣는다.

 

- 예외처리에서 무조건 실행되는 finally

- 그냥 무조건, 항상 실행되는 것이 아니라, finally와 연결되어 있는 try 블록으로 일단 진입을 하 면, 무조건 실행되는 영역이 바로 finally 블록이다.
- 중간에 return 문을 실행하더라도 finally 블록이 실행된 다음에 메소드를 빠져나간다.

예제)

package ex1;

public class Ex2_0624 {
	
	public static void main(String[] args) {
		boolean divOK = divider(4, 2);
		if (divOK)
			System.out.println("연산 성공");
		else
			System.out.println("연산 실패");
		divOK = divider(4, 0);
		if (divOK)
			System.out.println("연산 성공");
		else
			System.out.println("연산 실패");
	}

	public static boolean divider(int num1, int num2) {
		try {
			int result = num1 / num2;
			System.out.println("나눗셈 결과는 " + result);
			return true;
		} catch (ArithmeticException e) {
			System.out.println(e.getMessage());
			return false;
		} finally {
			System.out.println("finally 영역 실행"); // 무조건 실행
		}
	}
}

- 프로그래머가 직접 정의하는 예외 클래스의 정의와 throw

- 나이가 음수 값이거나 주민등록번호를 12자리만 입력했다거나 이름을 숫자로 입력했다거나와 같은 입력시 예외처리가 발생되는 것을 직접 명시해야한다. 이를 프로그램의 논리적 예외상황이라고 한다. 즉 프로그램의 성격에 따라 결정이 되는 예외상황이며 이러한 경우에는 직접 정의해야함을 말한다.

예제)

package ex1;

// 프로그래머 정의 예외 클래스 만들기
public class AgeInputException extends Exception {
// 필요에 의해서 필드, 생성자, 메서드 등을 추가로 정의 가능하다.
	public AgeInputException() {
		super("우효하지 않는 나이가 입력되었습니다.");
	}
} // class end

ㄴ 프로그래머가 필요에 의해서 예외 클래스를 만든다고 하면 무조건 상위 Execption을 상속해야한다.

예제)

package ex1;

import java.util.Scanner;

public class ProgrammerDefineException {
	public static void main(String[] args) {
		System.out.print("나이를 입력하세요: ");
		try {
			int age = readAge(); // 3.throws에 의해 이동된 예외처리 포인트
			System.out.println("당신은 " + age + "세입니다.");
		} catch (AgeInputException e) // 4.예외 처리 코드
		{
			System.out.println(e.getMessage()); // e.printStackTrace(); 변경해 볼것.
		}
	}

	// 1. AgeInputException는 던져버린다.
	public static int readAge() throws AgeInputException
	{
		Scanner keyboard = new Scanner(System.in);
		int age = keyboard.nextInt();
		if (age < 0) {
			AgeInputException excpt = new AgeInputException();
			throw excpt; // 2. 예외가 발생한 지점 명시.
		}
		return age;
	} // readAge 메소드
} // class end

ㄴ readAge라는 나이를 입력받는 메서드에서 예외가 발생하게끔 한다.
ㄴ readAge에서 예외가 발생하면 AgeInputException로 throws를 해버리겠다는 예고를 첫줄에 명시한다.
ㄴ throws랑 throw랑 무슨 차이?  throws 뒤에 예외가 1개 초과할 수 있기 때문에 복수형으로 쓴다.
ㄴ thorw 키워드는 메서드 본문안에서 예외를 throw하는 데 사용되는 반면 throws는 메서드 시그니처에서 메서드에 있는 문에서 발생할 수 있는 예외를 선언하는 데 사용됩니다.

			AgeInputException excpt = new AgeInputException(); // 인스턴스 생성
			throw excpt; // 2. 예외가 발생한 지점 명시.

ㄴ 위를 한줄로 바꾸면

			throw new AgeInputException();

ㄴ 이처럼 바꿀 수 있다.

			System.out.println(e.getMessage()); // e.printStackTrace(); 변경해 볼것.
			System.out.println("나이 입력 예외 발생");
			e.printStackTrace(); //변경해 볼것. 예외 발생경로를 추적해준다.

ㄴ 예외 발생 경로를 추적하고 싶다면 위와 같이 입력해본다. 결과는?

나이를 입력하세요: -1
유효하지 않는 나이가 입력되었습니다.
나이 입력 예외 발생
ex1.AgeInputException: 유효하지 않는 나이가 입력되었습니다.
	at ex1.ProgrammerDefineException.readAge(ProgrammerDefineException.java:27)
	at ex1.ProgrammerDefineException.main(ProgrammerDefineException.java:9)

ㄴ 예외가 발생한 지점이 어딘지 알 수 있다.

+ 위의 예제를 아래와 같이 변경할 수 있다.

package ex1;

import java.util.Scanner;

public class ProgrammerDefineException {
	public static void main(String[] args) throws AgeInputException {
		System.out.print("나이를 입력하세요: ");
			int age = readAge(); // 3.throws에 의해 이동된 예외처리 포인트
			System.out.println("당신은 " + age + "세입니다.");
	}
	// 1. AgeInputException는 던져버린다.
	public static int readAge() throws AgeInputException
	{
		Scanner keyboard = new Scanner(System.in);
		int age = keyboard.nextInt();
		if (age <= 0) {
			throw new AgeInputException();
		}
		return age;
	} // readAge 메소드
} // class end

ㄴ 메인 메서드에 throws AgeInputException를 입력해 예외처리를 하여 트라이 캐치를 안쓰고 JVM에게 맡길수도 있다.

나이를 입력하세요: -10
Exception in thread "main" ex1.AgeInputException: 유효하지 않는 나이가 입력되었습니다.
	at ex1.ProgrammerDefineException.readAge(ProgrammerDefineException.java:27)
	at ex1.ProgrammerDefineException.main(ProgrammerDefineException.java:9)

ㄴ 위와 같은 출력을 나타낸다. 

 

- 전화번호부 5단계 예외 처리 적용하기

예시)

메뉴를 선택하세요.
----------------
1.데이터 입력
2.데이터 검색
3.데이터 삭제
4.데이터 전체 출력
5.프로그램 종료
----------------
선택: 6
6에 해당하는 선택은 존재하지 않습니다.
메뉴 선택을 처음부터 다시 진행합니다.

메뉴를 선택하세요.
----------------
1.데이터 입력
2.데이터 검색
3.데이터 삭제
4.데이터 전체 출력
5.프로그램 종료
----------------
선택:1
데이터 입력을 시작합니다..
1.일반, 2.대학, 3.회사
선택>>4
4에 해당하는 선택은 존재하지 않습니다.
메뉴 선택을 처음부터 다시 진행합니다.

메뉴를 선택하세요.
----------------
1.데이터 입력
2.데이터 검색
3.데이터 삭제
4.데이터 전체 출력
5.프로그램 종료
----------------
선택:

나의 코딩)

class MenuInputException 예외 클래스)

package phoneBook;
// 프로그래머 정의 예외 클래스
public class MenuInputException extends Exception {
	public MenuInputException() {
		System.out.println("메뉴 선택을 처음부터 다시 진행합니다.");
	} //예외 처리 메서드
} // 클래스 끝

class PhoneBookManager)

	public void inputData() throws MenuInputException {
		System.out.println("1.일반, 2.대학, 3.회사");
		System.out.print("선택: ");
		int subMenu= readSubMenuNum();
		PhoneInfo info=null;
		switch(subMenu) {
		case INPUT_MENU.NORMAL:
			info=normalInput();
			break;
		case INPUT_MENU.UNIV:
			info=univInput();
			break;
		case INPUT_MENU.COMPANY:
			info=companyInput();
			break;
		}
		phoneList[cnt++]=info;
		System.out.println("입력 완료");
	}
	
	public static int readSubMenuNum() {
		Scanner key = new Scanner(System.in);
		int subMenu = key.nextInt();
		if(subMenu >= 4) {
			new MenuInputException();
		} else {
			if(subMenu<=0) {
				new MenuInputException();
			}
		}
		return subMenu;
	}

class PhoneBook)

	public static Scanner sc=new Scanner(System.in);

	public static void main(String[] args) throws MenuInputException {
		PhoneBookManager manager=new PhoneBookManager();
//		int menuNum;
		while(true) {
			showMenu();
//			menuNum=sc.nextInt();
			int menuNum = readMenuNum();
			switch(menuNum) {
			case INIT_MENU.INPUT:
				manager.inputData();
				break;
			case INIT_MENU.SEARCH:
				manager.searchData();
				break;
			case INIT_MENU.DELETE:
				manager.deleteData();
				break;
			case INIT_MENU.LIST:
				manager.showList();
				break;
			case INIT_MENU.EXIT:
				System.out.println("프로그램이 종료되었습니다.");
				return;  //System.exit(0);
			}
			
		}//while End
	}//main End
	
	public static int readMenuNum() {
		Scanner key = new Scanner(System.in);
		int menuNum = key.nextInt();
		if(menuNum >= 6) {
			new MenuInputException();
		} else {
			if(menuNum<=0) {
				new MenuInputException();
			}
		}
		return menuNum;
	}

+ 선생님 코딩 확인하기

 

- Object

예시)

package ex2;

public class Person {
	private String name;
	private int age;
	
}
package ex2;

public class Student extends Person {
	private int sNo;

}
package ex2;

public class Main {

	public static void main(String[] args) {
		Object obj = new Person(); //퍼슨 상위가 오브젝트라 성립됨
		obj = new Student(); // 스투던트의 상위는 퍼슨이고 퍼슨의 상위는 오브젝트라 성립
	}

}
package ex2;

public class Main {

	public static void main(String[] args) {
		Object obj1 = new Person(); //퍼슨 상위가 오브젝트라 성립됨
		System.out.println(obj1.toString());
		
		Object obj2 = new Student();
		System.out.println(obj2.toString());
	}

}

ㄴ 위와 같이 모든 클래스의 상위 클래스는 오브젝트 이므로 오브젝트의 퍼블릭 메서드를 사용할 수 있다.
ㄴ 모든 클래스는 모든 오브젝트의 클래스를 직간접적으로 사용할 수 있다.
ㄴ 아래를 참고한다.

예제 변경)

package ex2;

public class Person {
	private String name;
	private int age;

	@Override
	public String toString() {
		return "나는 Person 인스턴스 입니다.";
	}
	
}
package ex2;

public class Student extends Person {
	private int sNo;

	@Override
	public String toString() {
		return "나는 Student 인스턴스 입니다.";
	}

}
package ex2;

public class Main {

	public static void main(String[] args) {
		Object obj1 = new Person(); //퍼슨 상위가 오브젝트라 성립됨
		System.out.println(obj1.toString());
		
		Object obj2 = new Student();
		System.out.println(obj2.toString());
	}

}

- equals 메소드

- ==(비교) 연산자는 참조값 비교를 한다. 따라서 JAVA에서는 인스턴스 간의 내용 비교를 목적으로 Object 클래스에 equals 메소드를 정의해 놓았다.
- 그러나 Object클래스에 정의된 equals()는 참조변수 값(객체의 주소)을 비교하여 같으면 true, 다르면 false를 반환한다. - 따라서 새로 정의되는 클래스의 내용 비교가 가능하도록 이 메소드를 오버라이딩하는 것이 좋다.
- String 클래스처럼 equals 메소드가 내용비교를 하는 경우도 많다.

예제)

package ex3;

public class IntNumber {
	private int num;

	public IntNumber(int num)
	{
	this.num=num;
	}
}
package ex3;

public class Main {
	public static void main(String [] args) {
		IntNumber num1=new IntNumber(10);
//		IntNumber num2=new IntNumber(12);
		IntNumber num3=new IntNumber(10);
		if(num1.equals(num3)) //내용 비교
		System.out.println("num1과 num2는 동일한 정수");
		else
		System.out.println("num1과 num2는 다른 정수");
	}
}

ㄴ num1 과 num3는 각 각 10으로 같지만 여기서는  false로 다른 정수라고 나온다. 왜?
ㄴ 각 인스턴스의 변수값만을 비교하기 때문에 다른 정수라고 나온다.
ㄴ 이를 해결하기 위해서 이퀄 클래스를 오버라이드 해야한다.

예제 변경)

package ex3;

public class IntNumber {
	private int num;

	public IntNumber(int num)
	{
	this.num=num;
	}

	@Override // 두 인스턴스간의 내용 비교
	public boolean equals(Object obj) {
		if(this.num==((IntNumber)obj).num) {
			return true;
		}
		return false;
	}
	
}

ㄴ (IntNumber)obj -> 오브젝트를 인트넘버 타입으로 낮춰서(형변환 해서) 사용하여야 한다.
ㄴ 이것을 다르게 표현해도 된다.

@Override // 두 인스턴스간의 내용 비교
	public boolean equals(Object obj) {
		IntNumber cmp=(IntNumber)obj;
		if(this.num==cmp.num) {
			return true;
		}
		return false;
	}

문제1) Rectangle 클래스에 내용비교를 위한 equals 메소드를 삽입하고, 이를 테스트할 main 메소드도 완성하시오.

문제 풀이)

class EncapsulationEquals(main class)

package ex3;

public class EncapsulationEquals {
	public static void main(String[] args) {
		Rectangle rec1 = new Rectangle(1, 2, 8, 9);
		Rectangle rec2 = new Rectangle(2, 3, 5, 5);
		Rectangle rec3 = new Rectangle(1, 2, 8, 9);
		if (rec1.equals(rec3))
			System.out.println("rec1과 rec2의 내용 정보는 같다.");
		else
			System.out.println("rec1과 rec2의 내용 정보는 다르다.");
	}
}

class Point

package ex3;

public class Point {
	private int xPos, yPos;

	public Point(int x, int y) {
		this.xPos = x;
		this.xPos = y;
	}

	public void showPosition() {
		System.out.printf("[%d, %d]", xPos, yPos);
	}

	@Override
	public boolean equals(Object obj) {
		Point pos = (Point)obj;
		if(this.xPos==pos.xPos) //1개씩 일때
			if(this.yPos==pos.yPos)
			return true;
		return false;
	}
	
} //class end

class Rectangle

package ex3;

public class Rectangle {
	private Point upperLeft, lowerRight;

	public Rectangle(int x1, int y1, int x2, int y2) {
		upperLeft = new Point(x1, y1);
		lowerRight = new Point(x2, y2);
	}

	@Override
	public boolean equals(Object obj) {
		Rectangle rtg = (Rectangle) obj;
		if (upperLeft.equals(rtg.upperLeft)) // 2개 이상일때
			if (lowerRight.equals(rtg.lowerRight))
				return true;
		return false;
	}

	public void showPosition() {
		System.out.println("직사각형 위치정보...");
		System.out.print("좌 상단: ");
		upperLeft.showPosition();
		System.out.println("");
		System.out.print("우 하단: ");
		lowerRight.showPosition();
		System.out.println("\n");
	}

} // class end

ㄴ 비교할때는 이퀄스 오버라이딩을 생각하자.
ㄴ 두 인스턴스의 내용 비교할때는 이퀄스 오버라이딩.
ㄴ 인스턴스 간의 내용 비교 외에는 사용하면 안된다.


- clone 메서드

- object 클래스에는 인스턴스의 복사를 목적으로 clone 메서드가 정의되어 있다. 단, 이 메서드는 Cloneable 인터페이스를 구현하는 클래스의 인스턴스에서만 호출될 수 있다.
- Cloneable 인터페이스의 구현은 다음의 의미를 지닌다. "이 클래스의 인스턴스는 복사를 해도 됩니다." 인스턴스의 복사는 매우 민감한 작업이기 떄문입니다.
- clone 메서드는 protected 로 선언되어 있다. 따라서 외부호출이 가능하도록 public으로 재정의하자.

※ 인터페이스의 이름은 -albe로 끝나는 것이 보통이며 비어 있을 수도 있다.

※ 형식화된 출력 - printf()
- 기본 출력문은 println()은 변수의 값을 그대로 출력하므로, 값을 변환하지 않고는 다른 형식으로 출력할 수 없다. 반면에 printf()는 지시를 통해 변수의 값을 여러 가지 형식으로 변환하여 출력할 수 있다.

예시) System.out.printf("출력 서식",출력할 내용);

예제)

class Main_clone

package ex4;

public class Main_clone {

	public static void main(String[] args) {
		Point org = new Point(3, 5); // 인스턴스 선언
		Point cpy; // 인스턴스 복사
		
		try { // 예외처리 생성
			cpy = (Point)org.clone();
			org.showPosition();
			cpy.showPosition();
			if(org!=cpy) {
				System.out.println("서로 다른 인스턴스");
			}
		} catch (CloneNotSupportedException e) {
			System.out.println("복제 예외 처리");
			e.printStackTrace(); // 예외처리 위치 추적
		}
	} // main method end
} // class end

class Point

package ex4;

public class Point implements Cloneable{ //복제가 가능한 인스턴스임을 명명
	private int xPos, yPos;

	public Point(int x, int y) {
		this.xPos = x;
		this.yPos = y;
	}

	public void showPosition() {
		System.out.printf("[%d, %d]", xPos, yPos); //%d 정수형식으로 출력
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone(); //인스턴스는 함부로 복제할 수 없다.
	}

	@Override
	public boolean equals(Object obj) {
		Point pos = (Point)obj;
		if(this.xPos==pos.xPos) //1개씩 일때
			if(this.yPos==pos.yPos)
			return true;
		return false;
	}
	
} //class end

※ 얕은 복사 : 내부에서 내부로 복제하는 것. object 클래스의 clone 메서드는 인스턴스를 통쨰로 복사하지만, 인스턴스가 참조하고 있는 또 다른 인스턴스까지는 복사하지는 않는다. 단순히 참조 값만을 복사 할 뿐이다.

ㄴ 다시 한번 보기


- Object class _ hashCode method?

ㄴ 다음에 인쇄물 보기

- finalize 메소드

- 인스턴스가 완전히 소멸되기 직전 호출되는 메소드이다.

예제)

package ex4;

public class MyName {
	String objName;

	public MyName(String name) {
		objName = name;
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		System.out.println(objName + "이 소멸되었습니다.");
	}
}
package ex4;

// 1. 생성자 호출 -> 인스턴스 생성
// 2. finalize(소멸자)호출 -> 인스턴스 소멸
// 아무도 참조하지 않는 인스턴스는 GC가 한가할때 삭제한다.

public class ObjectFinalize {
	public static void main(String[] args) {
		MyName obj1 = new MyName("인스턴스1");
		MyName obj2 = new MyName("인스턴스2");
		obj1 = null;
		obj2 = null;
		System.out.println("프로그램을 종료합니다.");
		System.gc(); //Garbage Collection을 명령함.
		// 명령은 했지만 정리할 인스턴스에 표시만 해둔다.
		System.runFinalization();
		//GC에 의해서 소멸이 결정된 인스턴스를 즉시 소멸해라.
	}

}

- Wrapper클래스

- 기본형을 클래스로 정의한 것. 기본형 값도 객체로 다뤄져야 할 때가 있다.
- 이때 사용하는 것이 래퍼클래스이다. 아래 도표를 참조하자.

자바에서 제공하는 Wrapper 클래스

- Wrapper 클래스의 두가지 기능
1. Boxing -> 기본 자료형 데이터를 Wrapper 인스턴스로 포장하는것.
2. Unboxing -> Wrapper 인스턴스에 저장된 데이터를 꺼내는 것.

예제)

package ex4;

public class BoxingUnboxing {

	public static void main(String[] args) {
		Integer iValue = new Integer(10); //boxing
		System.out.println(iValue);
		iValue = new Integer(iValue.intValue() + 10); //intVlaue() ; 인트값을 꺼내라 //unboxing
		System.out.println(iValue);
	}

}

ㄴ 이처럼 수동으로 가능하지만 자동으로도 가능하다.

예제_자동)

package ex4;

public class BoxingUnboxing {

	public static void main(String[] args) {
		Integer iValue = 10; //autoboxing
		System.out.println(iValue);
		iValue = iValue + 10; //intVlaue() ; 인트값을 꺼내라 //autoUnboxing
		System.out.println(iValue);
	}

}

 

- Auto Boxing & Auto Unboxing
1. AB - > 기본 자료형 데이터가 자동으로 Wrapper 인스턴스로 포장하는 것.
2. AU - > Wrapper 인스턴스에 저장된 데이터가 자동으로 꺼내지는 것.
+ 기본 자료형 데이터가 와야 하는데, Wrapper 인스턴스가 있다면, AU된다.
+ 인스턴스가 와야 하는데, 기본 자료형 데이터가 있다면, AB된다.
+ 때문에 자바제공 Wrapper 클래스를 사용하는 것이 좋다.

예제)

package ex4;

public class AutoBoxingUnboxing {
	public static void main(String[] args) {
		Integer iValue = 10; //AutoBoxing
		Double dValue = 3.14; //AutoBoxing
		System.out.println(iValue);
		System.out.println(dValue);
		int num1 = iValue; //AutoUnBoxing
		double num2 = dValue; //AutoUnBoxing
		System.out.println(num1);
		System.out.println(num2);
	}
}

+ 우리는 왜 오토박싱 언박싱을 사용해야할까?

예제)

package ex4;

public class AutoBoxingUnboxing {
	public static void main(String[] args) {
		Integer i1 = new Integer(10);
		Integer i2 = new Integer(10);
		
		if(i1==i2) {
			System.out.println("동일 인스턴스");
		} else {
			System.out.println("비동일 인스턴스");
		}

	}
}

결과)

비동일 인스턴스

ㄴ 수동으로 박싱할 경우 각각의 참조값이 다르기 때문에 같지 않다는 결과를 나온다. 인스턴스를 하나만 만들면 되는데 비효율적으로 두개씩 쓰기 때문에 참조값을 다르게 갖는다.
ㄴ 그렇다면 오토박싱은 어떻게 할까.

 

예제)

package ex4;

public class AutoBoxingUnboxing {
	public static void main(String[] args) {
//		Integer i1 = Integer.valueOf(10);
//		Integer i2 = Integer.valueOf(10);
	
		Integer i1 = 10;
		Integer i2 = 10;
		
		if(i1==i2) {
			System.out.println("동일 인스턴스");
		} else {
			System.out.println("비동일 인스턴스");
		}

	}
}

 

- BigInteger 클래스; 아주 큰 정수

- long형으로도 표현 불가능한 정수를 표현하기 위해 java.math 패키지의 클래스이다.
- 큰 정수를 문자열로 표현한 이유는 숫자로 표현이 불가능하기 때문이다. 기본 자료형의 범위를 넘어 서는 크기의 정수는 숫자로 표현 불가능하다.

예제)

package ex4;

import java.math.*;

public class SoBigInteger {
	public static void main(String[] args) {
		System.out.println("최대 정수: " + Integer.MAX_VALUE);
		System.out.println("최소 정수: " + Integer.MIN_VALUE);		
		System.out.println("최대 정수: " + Long.MAX_VALUE);
		System.out.println("최소 정수: " + Long.MIN_VALUE);
		BigInteger bigValue1 = new BigInteger("100000000000000000000");
		BigInteger bigValue2 = new BigInteger("-99999999999999999999");
		BigInteger addResult = bigValue1.add(bigValue2);
		BigInteger mulResult = bigValue1.multiply(bigValue2);
		System.out.println("큰 수의 덧셈결과: " + addResult);
		System.out.println("큰 수의 곱셈결과: " + mulResult);
	}
}

 

- BigDecimal 클래스 ; 딱 떨어지는 실수

- 오차 없는 실수의 표현을 위한 클래스

package ex4;

import java.math.*;

public class NoErrorBigDecimal {
	public static void main(String[] args) {
		BigDecimal e1 = new BigDecimal("1.6"); // new BigDecimal(1.6); 오차발생
		BigDecimal e2 = new BigDecimal("0.1");
		System.out.println("두 실수의 덧셈결과: " + e1.add(e2));
		System.out.println("두 실수의 곱셈결과: " + e1.multiply(e2));
	}
}

- 문제1) 키보드로부터 두 개의 실수를 입력 받은 후, 두 실수의 차에 대한 절대값을 계산하여 출력 하시오. 단, 오차가 존재하지 않아야 하며, 문제의 해결을 위해서 반드시 BigDecimal 클래스의 API 문서를 참조하시오.

나의 코딩)

package ex4;

import java.math.*;
import java.util.Scanner;

public class Ex1_0624 {
	public static void main(String [] args) {
		Scanner sc= new Scanner(System.in);
		System.out.print("실수 1 입력 : ");
		String db1 = sc.nextLine();
		System.out.print("실수 2 입력 : ");
		String db2 = sc.nextLine();
		
		BigDecimal e1 = new BigDecimal(db1); // 문자열을 넘겨 받음
		BigDecimal e2 = new BigDecimal(db2);
		
		BigDecimal result = e1.subtract(e2);
		System.out.println("두 실수 차의 절대값 : "+result.abs());
		
		//e1.subtract(e2) : result of "e1-e2"
		//e1.abs() : Returns a BigDecimal whose value is the absolute value of this BigDecimal
		
	}
}

ㄴ실수를 문자열로 받는다. 더블로 받는 순간부터 오차가 발생한다.

 

- Math class

- 수학 계산에 유용한 메서드로 구성되어 있고, 메서드가 모두 static으로 정의 되어 있다.
- 단 대부분의 메서드가 육십분인 디그리 단위가 아닌 라디안 단위로 정의 되어 있다.

계속.

'ICIA 수업일지' 카테고리의 다른 글

2021.06.28 수업일지  (0) 2021.07.03
2021.06.25 수업일지  (0) 2021.06.26
2021.06.23 수업일지  (0) 2021.06.26
2021.06.22 수업일지  (0) 2021.06.26
2021.06.21 수업일지  (0) 2021.06.26