1. 생성자 : 어떤 일을 시작하기 전에 준비를 하는 것, 기본값을 설정함
public class Person{ //객체생성
String name; //멤버변수
int age;
public Person(){ //생성자(인자x)
this.name="사람"ㅣ
this.age=1;
}
//Person a = new Person(); 일 때 받아옴
public Person(String name, int age){ //생성자(인자o) - 인자의 유무, 갯수에 따라 여러개 생성 가능
this.name=name;
this.age=age;
}
//Person p = new Person("이", 1); 일 때 받아옴
public void toPrint(){
System.out.printIn(this.name + "님의 나이는" + this.age + "살 입니다.");
}
}
public static void main(String[] args)
{
Person p1 = new Person(); //사람님의 나이는 1살입니다.
p1.toPrint();
Person p2 = new Person("이흥직", 40); //이흥직니의 나이는 40살 입니다.
p2.toPrint();
}
2. 예외 : 예기치 못한 오류에 대한 처리
public static void main(String[] args){
try{
코드실행
}
catch(ArithmeticException e){
산술계산 오류발생시 실행
}
catch(Exception e){
모든 오류 발생시 실행
}
finally{
모든 오류처리가 종료된 후 (무조건)실행
}
}
public static void main(String[] args){
int a = 10;
int b = 2;
try{
System.out.printIn("Start");
System.out.printIn("결과값은"+(a/b));
System.out.printIn("End");
}
catch(ArithmeticException e){
System.out.printIn("산술오류 발생");
}
catch(Exception e){
System.out.printIn("오류 발생");
}
finally{
System.out.printIn("프로그램 종료");
}
}
//Start
//결과값은5
//End
//프로그램 종료
//예외상황
public static void main(String[] args){
int a = 10;
int b = 0;
try{
System.out.printIn("Start");
System.out.printIn("결과값은"+(a/b));
System.out.printIn("End");
}
catch(ArithmeticException e){
System.out.printIn("산술오류 발생");
}
catch(Exception e){
System.out.printIn("오류 발생");
}
finally{
System.out.printIn("프로그램 종료");
}
}
//Start
//산술오류 발생
//프로그램 종료
다음 Java 프로그램의 실행 결과는?
class Ref{
int a; //멤버변수
Ref(int x){ //생성자 ref
a = x; //this.a=x; 에서 this생략
}
int sum(Ref obj){
int k;
k=obj.a-a; //k=-1(obj.a=3, a=4) 롸???????????
a=10; obj.a=20; //obj2.a=10; obj1.a=20;
return k;
}
}
class PassRef{
public static void main(String args[]){
Ref obj1 = new Ref(3);
Ref obj2 = new Ref(4);
int k1 = obj2.sum(obj1);
System.out.print("obj1.a="+obj1.a); //20
System.out.print("obj1.a="+obj1.a); //10
}
}
다음 Java 프로그램의 실행 결과에서 4번째줄에 출력되는 것은? throw
public static void foo() throws Exception{
try{
System.out.printIn("문장E");
throw new Exception();
}
catch(Exception e){
System.out.printIn("문장F");
throw e;
}
finally{
System.out.printIn("문장G");
}
}
public static void main(String[] args){
try{
System.out.printIn("문장A");
foo(); //헷갈려...............
System.out.printIn("문장B");
}
catch(Exception e){
System.out.printIn("문장C");
}
System.out.printIn("문장D");
}
//A
//E
//F
//G
//B
//D
위 문제 변형
public static void foo() throws Exception{
try{
System.out.printIn("문장E");
throw new Exception(); // 예외처리를 되돌려보냄
}
}
public static void main(String[] args){
try{
System.out.printIn("문장A");
foo(); //foo에서 예외처리를 받아옴
System.out.printIn("문장B");
}
catch(Exception e){
System.out.printIn("문장C");
}
System.out.printIn("문장D");
}
//A
//E
//C
//D

반응형
'정보처리기사 > [흥달쌤] 실기_JAVA 특강 (완)' 카테고리의 다른 글
JAVA특강 / 06. Static (0) | 2022.04.29 |
---|---|
JAVA특강 / 05. 접근지정자 (0) | 2022.04.29 |
JAVA특강 / 04. 메서드 오버로딩&오버라이딩 (0) | 2022.04.29 |
JAVA특강 / 03. 상속 (0) | 2022.04.29 |
JAVA특강 / 01. 객체&인스턴스 (0) | 2022.04.28 |