๊ธฐํƒ€/Java

JAVA(2)

stonesy 2024. 1. 28. 19:52
728x90

๐Ÿ“–Java(2)

๐Ÿ”์—๋Ÿฌ์™€ ์˜ˆ์™ธ

์–ด๋–ค ์›์ธ์— ์˜ํ•ด ์˜ค๋™์ž‘ํ•˜๊ฑฐ๋‚˜ ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋˜๋Š” ๊ฒฝ์šฐ

Error

ํ”„๋กœ๊ทธ๋žจ์„ ์ž˜๋ชป ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ

Exception

  • Checked Exception: ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋Œ€์ฒ˜์ฝ”๋“œ ์œ ๋ฌด๋ฅผ ํ™•์ธ(์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋Œ€์ฒ˜์ฝ”๋“œ๊ฐ€ ์—†๋‹ค๋ฉด ์ปดํŒŒ์ผ์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค, ๋Œ€์ฒ˜์ฝ”๋“œ O)
  • Unchecked Exception: ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋Œ€์ฒ˜์ฝ”๋“œ ์œ ๋ฌด๋ฅผ ํ™•์ธ(์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋Œ€์ฒ˜์ฝ”๋“œ๊ฐ€ ์—†์–ด๋„ ์ปดํŒŒ์ผ์„ ์ง„ํ–‰ํ•œ๋‹ค. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜, ๋Œ€์ฒ˜์ฝ”๋“œ X)

 

โœ”๏ธtry~catch ๊ตฌ๋ฌธ

try{
}
catch(XXException c){ //๋˜์ง„ ์˜ˆ์™ธ๋ฅผ ๋ฐ›์Œ
	//์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ฒ˜๋ฆฌํ•  ์ฝ”๋“œ
}
public class SimpleException {
    public static void main(String[] args) {
	//java.lang.ArrayIndexOutOfBoundsException : unchecked exception
        int[] intArray = { 10 };

        try {
            System.out.println(intArray[2]);
        }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์™„๋ฃŒ");
        }
        
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");     
    }
}

Exception ๊ฐ์ฒด์˜ ์ •๋ณด ํ™œ์šฉ

Throwable์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ

  • getMessage(): ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
  • getCause(): ์˜ˆ์™ธ์˜ ์›์ธ์ด ๋˜๋Š” Throwable ๊ฐ์ฒด ๋˜๋Š” null ๋ฐ˜ํ™˜
  • printStackTrace(): ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ๊นŒ์ง€์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์Šคํƒ ์ถœ๋ ฅ, ๋””๋ฒ„๊น…์˜ ์ˆ˜๋‹จ์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ๋จ

 

Q. catch์—์„œ ์•„๋ฌด๊ฒƒ๋„ ๊ธฐ๋กํ•˜์ง€ ์•Š์œผ๋ฉด?

์˜ค๋ฅ˜์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ๊ทธ ์ •๋ณด๋ฅผ ์ž˜ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

try~catch๋ฌธ์—์„œ์˜ ํ๋ฆ„

  • try ๋ธ”๋ก์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด
    • JVM์ด ํ•ด๋‹น Exception ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ›„์— ๋˜์ง„๋‹ค.(Throw)
      • throw new XXException()
    • ๋˜์ ธ์ง„ exception์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” catch ๋ธ”๋ก์—์„œ ๋ฐ›์€ ํ›„ ์ฒ˜๋ฆฌ
      • ์ ๋‹นํ•œ catch ๋ธ”๋ก์„ ๋งŒ๋‚˜์ง€ ๋ชปํ•˜๋ฉด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์‹คํŒจ
    • ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฉด try~catch๋ธ”๋ก์„ ๋ฒ—์–ด๋‚˜ ๋‹ค์Œ ๋ฌธ์žฅ ์‹คํ–‰
  • try ๋ธ”๋ก์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด
    • catch ๋ฌธ์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  try~catch ๋ธ”๋ก์˜ ๋‹ค์Œ ํ๋ฆ„ ๋ฌธ์žฅ์„ ์‹คํ–‰

 

Q.

int num = new Random().nextInt(2);

try{
	System.out.println("code 1, num: "+num);
	int i = 1/num;
	System.out.println("code 2, ์˜ˆ์™ธ ์—†์Œ");
}catch(ArithmeticException e){
	System.out.println("code 3, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์™„๋ฃŒ");
}

System.out.println("code 4");

num=0์ผ ๋•Œ,

code 1, num: 1
code 3, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์™„๋ฃŒ
code 4

num=1์ผ ๋•Œ

code 1, num: 1
code 2, ์˜ˆ์™ธ ์—†์Œ
code 4

 

โœ”๏ธUnchecked Exception๊ณผ Checked Exception

Exception ์„ค๋ช… ์ข…๋ฅ˜

ArrayIndexOutOfBoundsException ๋ฐฐ์—ด์˜ ์ ‘๊ทผ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ UnCheckedException
NegativeArraySizeException ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์Œ์ˆ˜๋กœ ์ง€์ •ํ•œ ๊ฒฝ์šฐ UnCheckedException
NullPointerException ๊ฐ์ฒด๋ฅผ ์„ ์–ธ๋งŒ ํ•˜๊ณ  ์ƒ์„ฑํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ ‘๊ทผํ•œ ๊ฒฝ์šฐ UnCheckedException
ClassCastException Reference ํƒ€์ž…์˜ ์ž˜๋ชป๋œ ํ˜•๋ณ€ํ™˜์‹œ ๋ฐœ์ƒ UnCheckedException
NumberFormatException ๋ฌธ์ž์—ด๋กœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Primitive๋กœ ๋ณ€ํ™˜์‹œ ์ž˜๋ชป ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฐœ์ƒ UnCheckedException
IndexOutOfBoundsException List์—์„œ ์ž˜๋ชป๋œ index์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒฝ์šฐ UnCheckedException
ArithmeticException 0์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒฝ์šฐ UnCheckedException
IllegalArgumentException ๋ฉ”์„œ๋“œ์— ์ž˜๋ชป๋œ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ UnCheckedException
InterruptedException ์“ฐ๋ ˆ๋“œ ์ˆ˜ํ–‰์ค‘ ์ค‘๋‹จ ๋ช…๋ ค์— ๋”ฐ๋ฅธ ์˜ค๋ฅ˜ UnCheckedException
FileNotFoundException ์ง€์ •ํ•œ ๊ฒฝ๋กœ์— ํŒŒ์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ CheckedException
IOException ๋ฐ์ดํ„ฐ๋ฅผ IOํ•˜๋Š” ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ CheckedException
SQLException DB์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ CheckedException

 

Unchecked Exception

public class UncheckedTest{
	public static void main(String[] args){
			int[] list = {1,2,3,4,5};
			System.out.println(list[0]);
	}
}

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ์—๋Ÿฌ ์ฒดํฌ๋ฅผ ๊ฐ•์ œํ•ด์•ผ ํ–ˆ๋‹ค๋ฉด(ArrayIndexOutOfBoundsException ๋“ฑ) try~catch๋ฌธ์„ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ์„ ๊ฒƒ์ด๋‹ค.

 

Checked Exception

์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

public class CheckedExceptionHandling {
    public static void main(String[] args) {
        // TODO: ๋‹ค์Œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณด์ž.
        try {
            Class.forName("abc.Def");
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        // END
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    }

}

 

 

์ฆ‰, Checked exception๊ณผ Unchecked exception์˜ ์ฐจ์ด๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€๊ฐ€ ์•„๋‹ˆ๋ผ ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€?์ด๋‹ค.

checked→๋Œ€์ฒ˜์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ปดํŒŒ์ผ ์ง„ํ–‰X, unchecked→๋Œ€์ฒ˜์ฝ”๋“œ๊ฐ€ ์—†์–ด๋„ ์ปดํŒŒ์ผ ์ง„ํ–‰

 

 

โœ”๏ธ๋‹ค์ค‘ exception handling

  • try ๋ธ”๋ก์—์„œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ
  • ํ•˜๋‚˜์˜ try ๋ธ”๋ก์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ catch ๋ธ”๋ก ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    • ์˜ˆ์™ธ ์ข…๋ฅ˜๋ณ„๋กœ catch ๋ธ”๋ก ๊ตฌ์„ฑ
try{
}catch(XXException e){
	//XXException ๋ฐœ์ƒ์‹œ ์ฒ˜๋ฆฌ ์ฝ”๋“œ
}catch(XXException e){
	//XXException ๋ฐœ์ƒ์‹œ ์ฒ˜๋ฆฌ ์ฝ”๋“œ
}catch(XXException e){
	//XXException ๋ฐœ์ƒ์‹œ ์ฒ˜๋ฆฌ ์ฝ”๋“œ
}

 

  • ๋‹ค์ค‘ catch ๋ฌธ์žฅ ์ž‘์„ฑ ์ˆœ์„œ ์œ ์˜ ์‚ฌํ•ญ
    • JVM์ด ๋˜์ง„ ์˜ˆ์™ธ๋Š” catch๋ฌธ์žฅ์„ ์ฐพ์„ ๋•Œ๋Š” ๋‹คํ˜•์„ฑ์ด ์ ์šฉ๋œ๋‹ค
    • ์ƒ์œ„ ํƒ€์ž…์˜ ์˜ˆ์™ธ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ ๋’ค์— ๋“ฑ์žฅํ•˜๋Š” catch ๋ธ”๋ก์€ ๋™์ž‘ํ•  ๊ธฐํšŒ๊ฐ€ ์—†๋‹ค
    • ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋ฌด๊ด€
    • ์ƒ์† ๊ด€๊ณ„์—์„œ๋Š” ์ž‘์€ ๋ฒ”์œ„(์ž์‹)์—์„œ ํฐ ๋ฒ”์œ„(์กฐ์ƒ)์ˆœ์œผ๋กœ ์ •์˜

 

๋‹ค์ค‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ด์šฉํ•œ checked exception ์ฒ˜๋ฆฌ

  • ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ๋ถˆ๊ฐ€: Checked Exception⇒์ปดํŒŒ์ผ์กฐ์ฐจ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.(checked exception์ด๋ฏ€๋กœ)
public static void main(String[] args){
	Class.forName("abc.Def"); // ClassNotFoundException
	new FileInputStream("Hello.java"); // FileNotFoundException
  DriverManager.getConnection("Hello"); // SQLException
}
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•œ ๊ตฌ์ฒด์ ์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.
public class MultiExceptionHandling {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        // TODO: ๋‹ค์Œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณด์ž.
	try {
	    Class.forName("abc.Def"); // ClassNotFoundException
	    new FileInputStream("Hello.java"); // FileNotFoundException
	    DriverManager.getConnection("Hello"); // SQLException
	}catch(ClassNotFoundException e) {
	    System.out.printf("ํด๋ž˜์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: %s\n", e.getMessage());;
	}catch(FileNotFoundException e) {
	    System.out.printf("ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: %s\n", e.getMessage());;
	}catch(SQLException e) {
	    System.out.printf("DB ์ ‘์† ์‹คํŒจ: %s\n", e.getMessage());;
	}
        // END
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");

    }
}
  • ํ•˜์ง€๋งŒ ์‹ฌ๊ฐํ•˜์ง€ ์•Š๋Š” ์˜ˆ์™ธ๋ฅผ ๊ตณ์ด ์„ธ๋ถ„ํ™”ํ•ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๋‚ญ๋น„์ด๋ฏ€๋กœ ‘|’(or)๋ฅผ ์ด์šฉํ•ด ํ•˜๋‚˜์˜ catch ๊ตฌ๋ฌธ์—์„œ ์ƒ์†๊ด€๊ณ„๊ฐ€ ์—†๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ exception ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค.
public class MultiExceptionHandling {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        // TODO: ๋‹ค์Œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณด์ž.
	try {
	    Class.forName("abc.Def"); // ClassNotFoundException
	    new FileInputStream("Hello.java"); // FileNotFoundException
	    DriverManager.getConnection("Hello"); // SQLException
	}catch(ClassNotFoundException | FileNotFoundException e) {
	    System.out.printf("์ž์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: %s\n", e.getMessage());;
	}catch(SQLException e) {
	    System.out.printf("DB ์ ‘์† ์‹คํŒจ: %s\n", e.getMessage());;
	}
        // END
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    }
}
  • ํ•˜์ง€๋งŒ ๊ณ„์ธต ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ “์ƒ์† ๊ด€๊ณ„์—์„œ๋Š” ์ž‘์€ ๋ฒ”์œ„(์ž์‹)์—์„œ ํฐ ๋ฒ”์œ„(์กฐ์ƒ)์ˆœ์œผ๋กœ ์ •์˜”ํ•ด์•ผ ํ•œ๋‹ค.
public class HierachyException {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        String src = "./.project";
        // TODO: ์ƒ์† ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋‹ค์Œ์—์„œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณด์ž.
        try {
             FileInputStream input = new FileInputStream(src);
             int readData = -1;
             while ((readData = input.read()) != -1) {
              System.out.print((char) readData);
             }
        }catch(FileNotFoundException e) {
            System.out.printf("์ฝ์œผ๋ ค๋Š” ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.: %s\n", e.getMessage());
        }catch(IOException e) {
            System.out.printf("ํŒŒ์ผ ์ฝ๊ธฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.: %s\n", e.getMessage());;
        }

        // END
        System.out.println("ํŒŒ์ผ ์ฝ์Œ ์™„๋ฃŒ!");
    }

}

 

 

 

โœ”๏ธtry ~ catch ~ finally

finally๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ์ƒ๊ด€ ์—†์ด ์–ธ์ œ๋‚˜ ์‹คํ–‰

  • ์ค‘๊ฐ„์— return์„ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ๋„ finally ๋ธ”๋ก์„ ๋จผ์ € ์ˆ˜ํ–‰ ํ›„ ๋ฆฌํ„ด ์‹คํ–‰
public class tryCatchFinallyTest{
	public static void main(String[] args){
		int num = new Random().nextInt(2);
		try{
			System.out.println("1");
			int i = 1/num;
			System.out.println("2");
		}catch(ArithmeticException e){
			System.out.println("3");
		}finally{
			System.out.println("4");
		}
	}
	System.out.println("5");
}
//num=0์ผ ๋•Œ
1
3
4
5
//num=1์ผ ๋•Œ
1
2
4 //finaly๋Š” ์ค‘๊ฐ„์— return์„ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ์—๋„ finally ๋ธ”๋ก์„ ๋จผ์ € ์ˆ˜ํ–‰ ํ›„์— ๋ฆฌํ„ด์„ ์‹คํ–‰ํ•œ๋‹ค.

finally๋Š” ์ฃผ๋กœ ์ž์›์„ ์ •๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋˜ํ•œ try-catch-finally๋ฅผ ํ†ตํ•ด ๋™์ž‘์ด ๋ฌถ์—ฌ ์›€์ง์ด๋„๋กํ•จ์œผ๋กœ์จ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ๋„ ์กด์žฌํ•œ๋‹ค.

public class UseInstaller {
    public static void main(String[] args) {
        // TODO: InstallApp์„ ์ด์šฉํ•˜๋ฉด์„œ ์ž์›์ด ํ™•์‹คํžˆ ์ •๋ฆฌ๋˜๋„๋ก ํ•ด๋ณด์ž.
	InstallApp app = new InstallApp();
	try {
	    app.copy();
	    app.install();
	}catch(Exception e) {
	    e.printStackTrace();
	}finally {
	    app.delete();
	}
	    // END
      System.out.println("์„ค์น˜ ์ข…๋ฃŒ");
    }
}

 

 

*try ๊ตฌ๋ฌธ์ด ์ˆ˜ํ–‰๋œ๋‹ค๋ฉด ํ•ญ์ƒ ์ˆ˜ํ–‰๋˜๋Š” ๋ฌธ์žฅ

  1. try ๋ฌธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„๋„
  2. try ๋ฌธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋„
  3. try ๋ฌธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์ง€๋งŒ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋œ ๊ฒฝ์šฐ์—๋„
  4. try ๋ฌธ์ด๋‚˜ catch ๋ฌธ์—์„œ return ๋œ ๊ฒฝ์šฐ์—๋„

๋‹จ, System.exit(~)์— ์˜ํ•ด JVM์ด ์ข…๋ฃŒ๋˜๋ฉด finally ๋ฌธ์€ ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

โœ”๏ธtry-with-resource

  • JDK 1.7 ์ด์ƒ์—์„œ ๋ฆฌ์†Œ์Šค์˜ ์ž๋™ close ์ฒ˜๋ฆฌ
try(๋ฆฌ์†Œ์Šค_ํƒ€์ž…1 res1 = ์ดˆ๊ธฐํ™”; ๋ฆฌ์†Œ์Šค_ํƒ€์ž…2 res2 = ์ดˆ๊ธฐํ™”; ...){
	//์˜ˆ์™ธ ๋ฐœ์ƒ ์ฝ”๋“œ
}catch(Exception e){
	//exception handling ์ฝ”๋“œ
}
  • try ์„ ์–ธ๋ฌธ์— ์„ ์–ธ๋œ ๊ฐ์ฒด๋“ค์— ๋Œ€ํ•ด ์ž๋™ close ํ˜ธ์ถœ(finalliy ์—ญํ• )
    • ๋‹จ, ํ•ด๋‹น ๊ฐ์ฒด๋“ค์ด AutoCloseable interface๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ
      • ๊ฐ์ข… I/O stream, socket, connection, …
    • ํ•ด๋‹น ๊ฐ์ฒด๋Š” try ๋ธ”๋ก์—์„œ ๋‹ค์‹œ ํ• ๋‹นํ•  ์ˆ˜ ์—†์Œ

 

 

โœ”๏ธthrows ํ™œ์šฉ

Declare Exception (์„ ์–ธ์  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ)

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ณณ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ง์ ‘ ํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ

์˜ˆ์™ธ๋ฅผ ๋˜์ ธ์„œ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•œ ๊ณณ์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์œ„์ž„.

 

๋ชฉ์ 

  • ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ณณ์—์„œ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ด ๊ณ ์ •๋˜๋ฏ€๋กœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•œ ๊ณณ์— ๋งž๊ฒŒ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.
    • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•œ ๊ณณ์— ๋งž๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์œ„์ž„
    • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์˜ ๋‹ค์–‘์„ฑ์„ ์ œ๊ณต
  • ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜์—์„œ ์—ฌ๋Ÿฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•œ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜์—์„œ ๋˜‘๊ฐ™์€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์ด ์•ˆ๋˜๊ณ , ์ˆ˜์ •์ด ์šฉ์ดํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๋ฅผ ํ•œ ๊ณณ(์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š” ๊ณณ)์œผ๋กœ ๋˜์ ธ์„œ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๋ฅผ return์„ ํ†ตํ•ด์„œ ์ „๋‹ฌํ•˜๊ณ  ๋ฉ”์„œ๋“œ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ํ†ตํ•ด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค.

 

ํ˜•์‹

[modifiers] ๋ฆฌํ„ดํƒ€์ž… ๋ฉ”์„œ๋“œ์ด๋ฆ„([์ธ์ž๋“ค, ...]) [throws ์˜ˆ์™ธ๋“ค, ...]{
//์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ๋œ ์ฝ”๋“œ
}
  • ์˜ˆ์™ธ๊ฐ€ ์—†์–ด์ง€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ˆœํžˆ ์ „๋‹ฌ๋จ
  • ์˜ˆ์™ธ๋ฅผ ์ „๋‹ฌ๋ฐ›์€ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์˜ ์ฑ…์ž„ ๋ฐœ์ƒ

 

checked exception๊ณผ throws

  • checked exception์€ ๋ฐ˜๋“œ์‹œ try~catch ๋˜๋Š” throws๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
  • ํ•„์š”ํ•œ ๊ณณ์—์„œ try~catch ์ฒ˜๋ฆฌ
  • runtime exception์€ throws ํ•˜์ง€ ์•Š์•„๋„ ์ „๋‹ฌ๋˜์ง€๋งŒ
  • ํ•˜์ง€๋งŒ ๊ฒฐ๊ตญ์€ try~catch๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
package chapter07;

/**
 * ์‚ฌ์šฉ์ž ์ •์˜ Exception
 * 
 * UncheckedException
 * - ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•˜์ง€ ์•Š์•„๋„ ์ปดํŒŒ์ผ error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
 * 	=>์‹คํ–‰์‹œ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
 * - ์ข…๋ฅ˜
 * 	RuntimeException๊ณผ RuntimeException์„ ์ƒ์† ๋ฐ›์€ sub๋“ค
 * 
 * - ์‚ฌ์šฉ์ž ์ •์˜ UncheckedException
 * 	RuntimeException๊ณผ RuntimeException์„ ์ƒ์†๋ฐ›์€ sub๋“ค์„ ์ƒ์†๋ฐ›๋Š”๋‹ค.
 * 
 * CheckedException
 * - ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
 * 	=> ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•ด์•ผ ํ•œ๋‹ค.
 * - ์ข…๋ฅ˜
 * 	RuntimeException๊ณผ RuntimeException์„ ์ƒ์† ๋ฐ›์€ sub๋“ค์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์˜ˆ์™ธ๋“ค
 *	ex) Exception, IOException
 * - ์‚ฌ์šฉ์ž ์ •์˜ CheckedException
 * 	RuntimeException๊ณผ RuntimeException์„ ์ƒ์†๋ฐ›์€ sub๋“ค์„ ์ƒ์†๋ฐ›๋Š”๋‹ค.
 * 	ex) Exception
 */

class MyChecked extends Exception{
    public MyChecked(String msg) {
	super(msg); //getMessage()๋ฅผ ํ†ตํ•ด ๋ฉ”์„ธ์ง€๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    }
}

class MyUnChecked extends RuntimeException{
    public MyUnChecked(String msg) {
	super(msg); //getMessage()๋ฅผ ํ†ตํ•ด ๋ฉ”์„ธ์ง€๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    }
}

class ExceptionUse{
    public static int mod(int i, int j) {
	//MyUnChecked RuntimeException์„ ์ƒ์†๋ฐ›์•„์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•˜์ง€ ์•Š์•„๋„ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
	if(j==0) throw new MyUnChecked("0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
	return i/j;
    }
    
    public static int div(int i, int j) throws MyChecked{
	//MyChecked๋Š” Exception์„ ์ƒ์† ๋ฐ›์€ CheckedException์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.
	if(j==0) throw new MyChecked("0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
	return i/j;
    }    
}

public class ExceptionTest5 {
    public static void main(String[] args) {
	//UnCheckedException์„ ๋˜์ง€๋Š” mod ๋ฉ”์„œ๋“œ๋Š” ํ•„์š”์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•œ๋‹ค.
	ExceptionUse.mod(256,8);
	try {
	    //CheckException์„ ๋˜์ง€๋Š” div ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœ์‹œ ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
	    ExceptionUse.div(256,8);
	}catch(MyChecked e){
	    e.printStackTrace();
	}
    }
}

 

 

 

โœ”๏ธ๋กœ๊ทธ ๋ถ„์„๊ณผ ์˜ˆ์™ธ์˜ ์ถ”์ 

  • Throwable์˜ printStackTrace๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์Šคํƒ ์ •๋ณด ์กฐํšŒ ๊ฐ€๋Šฅ
    • ์ตœ์ดˆ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ์—์„œ๋ถ€ํ„ฐ ์˜ˆ์™ธ ๋ฐœ์ƒ ๋ฉ”์„œ๋“œ๊นŒ์ง€์ด ์Šคํƒ ์ •๋ณด ์ถœ๋ ฅ
  • ๊ผญ ํ™•์ธํ•ด์•ผํ•  ์ •๋ณด
    • ์–ด๋–ค ์˜ˆ์™ธ์ธ๊ฐ€? - ์˜ˆ์™ธ ์ข…๋ฅ˜
    • ์˜ˆ์™ธ ๊ฐ์ฒด์˜ ๋ฉ”์‹œ์ง€๋Š” ๋ฌด์—‡์ธ๊ฐ€? - ์˜ˆ์™ธ ์›์ธ
    • ์–ด๋””์„œ ๋ฐœ์ƒํ–ˆ๋Š”๊ฐ€? - ๋””๋ฒ„๊น… ์ถœ๋ฐœ์ 
      • ์ง์ ‘ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋””๋ฒ„๊น… ๋Œ€์ƒ์œผ๋กœ ์‚ผ์„ ๊ฒƒ
      • ์ฐธ์กฐํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(java.xx ๋“ฑ)๋Š” ๊ณผ๊ฐํžˆ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

 

๋งค์„œ๋“œ ์žฌ์ •์˜์™€ throws

๋งค์„œ๋“œ ์žฌ์ •์˜์‹œ ์กฐ์ƒ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๊ฐ€ ๋˜์ง€๋Š” ์˜ˆ์™ธ๋ณด๋‹ค ํฐ ์˜ˆ์™ธ๋ฅผ ๋˜์งˆ ์ˆ˜ ์—†๋‹ค.(๋ถ€๋ชจ๊ฐ€ ์น˜์ง€ ์•Š์€ ์‚ฌ๊ณ ๋ฅผ ์ž์‹์ด ์น  ์ˆ˜ ์—†๋‹ค.)

 

์˜ˆ์™ธ ๋ณ€ํ™˜

ํ•˜์œ„ ๊ณ„์ธต์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” ์ƒ์œ„ ๊ณ„์ธต์— ๋งž๋Š” ์˜ˆ์™ธ๋กœ ๋ฐ”๊ฟ”์„œ ๋˜์ ธ์•ผ ํ•œ๋‹ค.

 

Exception Chaining

ํ•˜์œ„ ๊ณ„์ธต์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ ์ •๋ณด๊ฐ€ ์ƒ์œ„ ๊ณ„์ธต์˜ ์˜ˆ์™ธ๋ฅผ ๋””๋ฒ„๊น…ํ•˜๋Š”๋ฐ ์œ ์šฉํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ

  • ํ•˜์œ„ ๊ณ„์ธต์˜ ์˜ˆ์™ธ๋ฅผ ์›์ธ์œผ๋กœ ์ƒ์œ„ ๊ณ„์ธต์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ด

 

 

 

๐Ÿ”Collection

๋ฐฐ์—ด

๋ฐฐ์—ด์€ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ, ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค.

⇒ํƒ€์ž…์ด ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งค๋ฒˆ ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ํ•„์š”๋กœ ํ•œ๋‹ค.

 

 

Collection Framework

  • java.util ํŒจํ‚ค์ง€
    • ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ œ๊ณต → DB์ฒ˜๋Ÿผ CRUD ๊ธฐ๋Šฅ ์ค‘์š”
  • List, Set, Map(3๋Œ€ ์ฃผ์š” interface)
    • List: ์ž…๋ ฅ ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ, ์ˆœ์„œ๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ํ—ˆ๋ฝ
    • ArrayList, LinkedList
    • Set: ์ž…๋ ฅ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ, ์ˆœ์„œ๊ฐ€ ์—†์–ด์„œ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์—†์Œ → ์ค‘๋ณต ํ—ˆ๋ฝํ•˜์ง€ ์•Š์Œ
    • HashSet, TreeSet
    • Map: key์™€ value์˜ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ง‘ํ•ฉ. ์ˆœ์„œ๋Š” ์—†๊ณ  key์˜ ์ค‘๋ณต ๋ถˆ๊ฐ€, value๋Š” ์ค‘๋ณต ๊ฐ€๋Šฅ
    • HashMap, TreeMap

 

 

โœ”๏ธList

์ž…๋ ฅ ์ˆœ์„œ O, ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต O

ํŠน์ • ์œ„์น˜ O(index O)

  • add(int index, E element)
  • get(int index)
  • indexOf(Object o)
  • lastIndexOf(Object o)
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•ด ์ค€๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ index๋Š” 0๋ถ€ํ„ฐ size()-1๊นŒ์ง€
  • ์ค‘๊ฐ„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” index๋Š” 0๋ถ€ํ„ฐ size()

⇒๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด IndexOutOfBoundsException์ด ๋ฐœ์ƒํ•œ๋‹ค.

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๋ณตํ•ด์„œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • IndexOf(Object o), lastIndexOf(Object o), contains(Object o), remove(Object o)์€ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ **equals(Object o)**๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ต ํ›„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

๋ฐฐ์—ด๊ณผ ArrayList

๋ฐฐ์—ด์€ ๊ฐ„๋‹จํ•˜๊ณ  ์‚ฌ์šฉ์ด ์‰ฌ์šฐ๋ฉฐ ์ ‘๊ทผ ์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ, ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์–ด ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๋ณต์‚ฌํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๋น„์ˆœ์ฐจ์ ์ธ ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ์— ๋งŽ์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค.

⇒๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ArrayList๋„ ํƒœ์ƒ์ ์œผ๋กœ ๋ฐฐ์—ด์˜ ์žฅ-๋‹จ์ ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๊ฐ„๋‹ค.

 

LinkedList

๊ฐ ์š”์†Œ๋ฅผ Node๋กœ ์ €์žฅํ•˜๊ณ  Node๋Š” ๋‹ค์Œ ์š”์†Œ์˜ ์ฐธ์กฐ๊ฐ’๊ณผ ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋จ

  • ๊ฐ ์š”์†Œ๊ฐ€ ๋‹ค์Œ ์š”์†Œ์˜ ๋งํฌ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋ฉฐ ์—ฐ์†์ ์œผ๋กœ ๊ตฌ์„ฑ๋  ํ•„์š”๊ฐ€ ์—†๋‹ค.
public class ListTest {
    public static void main(String[] args) {
			//ArrayList๋Š” ์ €์žฅํ•  ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
			ArrayList<String> list = new ArrayList<>(10);
			list.add("hello");
			
			//๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค node๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์—ฐ๊ฒฐํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑํ•œ๋‹ค.
			LinkedList<String> list2 = new LinkedList<>();
			
			Queue<Integer> queue = new LinkedList<>();
			Queue<Integer> queue2 = new ArrayDeque<>(); //list๋ฅผ ์ƒ์†๋ฐ›์ง€ ์•Š์Œ
    }
}

 

 

๊ฒฐ๋ก 

  • ์†Œ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ArrayList์™€ LinkedList๋Š” ํฐ ์ฐจ์ด๊ฐ€ ์—†์Œ
  • ์ •์ ์ธ ๋ฐ์ดํ„ฐ์˜ ํ™œ์šฉ, ๋‹จ์ˆœํ•œ ๋ฐ์ดํ„ฐ ์กฐํšŒ์šฉ: ArrayList
  • ๋™์ ์ธ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์‚ญ์ œ๊ฐ€ ๋งŽ์€ ์ž‘์—…: LinkedList

 

 

โœ”๏ธSet

์ž…๋ ฅ ์ˆœ์„œ X, ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต X

  • ์ž…๋ ฅ ์ˆœ์„œ๋ฅผ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์ฃผ๋จธ๋‹ˆ์— ๊ตฌ์Šฌ(๋ฐ์ดํ„ฐ)๋ฅผ ๋„ฃ๋Š” ํ˜•ํƒœ
  • ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆœ์„œ(index)๊ฐ€ ์—†์–ด ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
    • ํšจ์œจ์ ์ธ ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ ์ˆ˜๋‹จ
    import java.util.*;
    
    public class ListToSetToListTest {
        public static void main(String[] args){
            List<Integer> numList = Arrays.asList(1,2,2,3,3,3,4,5);
            System.out.println("์ค‘๋ณต์ด ์กด์žฌํ•˜๋Š” numList: "+numList);
    
            Set<Integer> uniqueSet = new HashSet<>(numList);
            System.out.println("Set์„ ํ†ตํ•œ ์ค‘๋ณต์ œ๊ฑฐ uniqueSet: "+uniqueSet);
    
            numList = new ArrayList<>(uniqueSet);
            System.out.println("๋‹ค์‹œ List๋กœ uniqueList: "+numList);
        }
    }
    
    ์ค‘๋ณต์ด ์กด์žฌํ•˜๋Š” numList: [1, 2, 2, 3, 3, 3, 4, 5] Set์„ ํ†ตํ•œ ์ค‘๋ณต์ œ๊ฑฐ uniqueSet: [1, 2, 3, 4, 5] ๋‹ค์‹œ List๋กœ uniqueList: [1, 2, 3, 4, 5]
import java.util.HashSet;
import java.util.Set;

public class SetTest {
    Set<Object> hset = new HashSet<Object>();

    private void addMethod() {
        hset.add(Integer.valueOf(1));
        hset.add("Hello");
        hset.add("Hi");
        hset.add("There");
        hset.add("Hello"); // ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ํ™•์ธ
        hset.add(1); // ๊ธฐ๋ณธํ˜•์€ wrapper๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€
        // TODO: SmartPhone ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž.
        SmartPhone sp1 = new SmartPhone("010");
        SmartPhone sp2 = new SmartPhone("010");
        System.out.println("equals: "+sp1.equals(sp2));
        hset.add(sp1);
        hset.add(sp2);
        // END
        System.out.println("๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ๊ฒฐ๊ณผ: " + hset);
    }

    private void retrieveMethod() {
        System.out.println("๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜: " + hset.size());

        for (Object sobj : hset) {
            System.out.println("๋ฐ์ดํ„ฐ ์กฐํšŒ: " + sobj);
        }
    }

    private void removeMethod() {
        hset.remove("Hello");
        System.out.println("๋ฐ์ดํ„ฐ ์‚ญ์ œ ๊ฒฐ๊ณผ: " + hset);
    }

    public static void main(String[] args) {
        SetTest test = new SetTest();
        test.addMethod();
        test.retrieveMethod();
        test.removeMethod();
    }
}
  • index๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— ํŠน์ • ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. (์ˆ˜์ • X)
  • ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ธฐ์ค€
// TODO: SmartPhone ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž.
hset.add(new SmartPhone("010"));
hset.add(new SmartPhone("010"));

๊ฐ์ฒด์˜ ์ž…์žฅ์—์„œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ธ์‹

// TODO: ๋™์ผํ•œ ๋ฒˆํ˜ธ์˜ SmartPhone์ด๋ฉด ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ•˜์‹œ์˜ค.
    @Override
    public boolean equals(Object obj) {
			if(obj!=null && obj instanceof SmartPhone phone) {
			    return this.number.equals(phone.number);
			}
			return false;
    }
    
    @Override
    public int hashCode() {
			return this.number.hashCode();
    }

๋™์ผํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ธฐ์ค€์€ ๊ฐ์ฒด์˜ equals()๊ฐ€ true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  hashcode๊ฐ’์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ equals()๋ฟ๋งŒ ์•„๋‹ˆ๋ผ hashCode๋„ overrideํ•ด์•ผํ•œ๋‹ค.

 

 

โœ”๏ธMap

  • key์™€ value๋ฅผ ํ•˜๋‚˜์˜ Entry๋กœ ๋ฌถ์–ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌ
    • key: Objectํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ์ค‘๋ณต์„ ํ—ˆ๋ฝํ•˜์ง€ ์•Š์Œ
    • value: Objectํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ์ค‘๋ณต์ด ํ—ˆ๋ฝ ๋จ
  • put(K key, V Value)
  • Java์˜ Map์—์„œ put ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ํ•ด๋‹น ํ‚ค์— ๋Œ€ํ•œ ์ด์ „ ๊ฐ’์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด์ „์— ํ•ด๋‹น ํ‚ค์— ์—ฐ๊ฒฐ๋œ ๊ฐ’์ด ์—†์—ˆ๋‹ค๋ฉด **null**์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
  • containsKey(Object key)
  • get(Object key)
  • keySet()
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapTest {
    public static void main(String[] args){
        Map<String, Integer> ageMap = new HashMap<>();
        ageMap.put("John", 25);
        ageMap.put("Stormy", 5);
        ageMap.put("Stonsy", 23);
        System.out.println(ageMap);

        for(Entry<String, Integer> entry : ageMap.entrySet()){
            String name = entry.getKey();
            int age = entry.getValue();
            System.out.println(name+"์˜ ํ˜„์žฌ ๋‚˜์ด๋Š” "+age+"์ž…๋‹ˆ๋‹ค.");
        }

        ageMap.remove("Stormy");
        System.out.println("stormy ์‚ญ์ œํ›„");

        for(Entry<String, Integer> entry : ageMap.entrySet()){
            String name = entry.getKey();
            int age = entry.getValue();
            System.out.println(name+"์˜ ํ˜„์žฌ ๋‚˜์ด๋Š” "+age+"์ž…๋‹ˆ๋‹ค.");
        }
    }
}

 

 

 

โœ”๏ธ์ •๋ ฌ

์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๋Š” Collection๋“ค๋งŒ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • List ๊ณ„์—ด
  • Set์—์„œ๋Š” SortedSet์˜ ์ž์‹ ๊ฐ์ฒด
  • Map์—์„œ๋Š” SortedMap์˜ ์ž์‹ ๊ฐ์ฒด(key ๊ธฐ์ค€)

 

Comparator์˜ ํ™œ์šฉ

  • ๊ฐ์ฒด๊ฐ€ Comparable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์ง€ ์•Š๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž ์ •์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋Š” ๊ฒฝ์šฐ
    • String์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์ด ์•„๋‹Œ ๊ธ€์ž ์ˆ˜ ๋ณ„๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด?

 

Comparable: ๋‚ด๊ฐ€ ์ •๋ ฌ๋˜๋Š” ๊ฒƒ

Comparator: ๋ฌด์—‡์„ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ

 

import java.util.*;

public class SortTest {
    static class StringLengthComparable implements Comparator<String> {
        @Override
        public int compare(String s1, String s2){
            int len1 = s1.length();
            int len2 = s2.length();

            return Integer.compare(len1, len2);
        }
    }

    public static void main(String[] args){
        //1. ๋ฐฐ์—ด์˜ ์ •๋ ฌ(์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ)
        Integer[] numArr = {2,1,4,3,5};
        Arrays.sort(numArr);
        System.out.println("์ˆซ์ž ๋ฐฐ์—ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: "+Arrays.toString(numArr));

        //2. ๋ฐฐ์—ด์˜ ์ •๋ ฌ(๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ)
        Arrays.sort(numArr, (n1,n2)->n2-n1);
        Arrays.sort(numArr, (n1,n2)->{return n2-n1;});
        System.out.println("์ˆซ์ž ๋ฐฐ์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: "+Arrays.toString(numArr));

        //3. List์˜ ์ •๋ ฌ
        List<Integer> numList = new ArrayList<>();
        //List<Integer> numList = Arrays.asList(5,1,2,4,3);
        numList.add(5);
        numList.add(1);
        numList.add(2);
        numList.add(4);
        numList.add(3);
        Collections.sort(numList); // ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ
        System.out.println("Integer List ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: "+numList);
        Collections.sort(numList,(n1,n2)->(n2-n1));
        System.out.println("Integer List ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: "+numList); // ๋‚ด๋ฆผ์ฐจ์ˆœ์ •๋ ฌ

        //4. String์˜ ์ •๋ ฌ
        List<String> strList = Arrays.asList("011", "010", "111");
        System.out.println("String List: "+strList);
        Collections.sort(strList);
        System.out.println("String List ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: "+strList);

        //5. String ๊ธ€์ž๊ธธ์ด์ˆœ ์ •๋ ฌ
        List<String> strList2 = Arrays.asList("Stormy", "Stonesy", "John");
        Collections.sort(strList2, new StringLengthComparable());
        System.out.println("String List ๊ธ€์ž๊ธธ์ด์ˆœ ์ •๋ ฌ: "+strList2);
    }
}

์ˆซ์ž ๋ฐฐ์—ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: [1, 2, 3, 4, 5]

์ˆซ์ž ๋ฐฐ์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: [5, 4, 3, 2, 1]

Integer List ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: [1, 2, 3, 4, 5]

Integer List ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: [5, 4, 3, 2, 1]

String List: [011, 010, 111]

String List ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ: [010, 011, 111]

String List ๊ธ€์ž๊ธธ์ด์ˆœ ์ •๋ ฌ: [John, Stormy, Stonesy]

 

 

Anonymous Nested Class

  • ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ด ์—†์ด ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค
  • ๋ถ€๋ชจ์˜ ์ด๋ฆ„์„ ๋นŒ๋ ค์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ => ์ƒ์†์ด ๊ธฐ๋ณธ

 

ํ˜•์‹

new ๋ถ€๋ชจ(){ //๊ตฌํ˜„ํ•˜๊ธฐ }

 

 

Lamda

  • ํ•จ์ˆ˜์˜ ์ถ•์•ฝํ˜•
  • Anonymous Nested Class์™€ ๊ฐ™์€ ์—ญํ• 

 

ํ˜•์‹

( )->{ }

 

public class SortTest {
    public static void main(String[] args){
        //1. ๋ฐฐ์—ด์˜ ์ •๋ ฌ(์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ)
        Integer[] numArr = {2,1,4,3,5};
        Arrays.sort(numArr);
        System.out.println(Arrays.toString(numArr));

        //2. ๋ฐฐ์—ด์˜ ์ •๋ ฌ(๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ)
        Arrays.sort(numArr, (n1,n2)->n2-n1);
        Arrays.sort(numArr, (n1,n2)->{return n2-n1;});
        System.out.println(Arrays.toString(numArr));
    }
}

 

 

 

๐Ÿ”Java ์ŠคํŠธ๋ฆผ(Stream)

Java ์ŠคํŠธ๋ฆผ Stream (1) ์ด์ •๋ฆฌ

 

Java ์ŠคํŠธ๋ฆผ Stream (1) ์ด์ •๋ฆฌ

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” Java 8์˜ ์ŠคํŠธ๋ฆผ(Stream)์„ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐœ์˜ ํฌ์ŠคํŠธ๋กœ, ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์„ ์ด์ •๋ฆฌํ•˜๋Š” ์ด๋ฒˆ ํฌ์ŠคํŠธ์™€ ์ข€ ๋” ๊ณ ๊ธ‰ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๋Š” ๋‹ค์Œ ํฌ์ŠคํŠธ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. Java ์ŠคํŠธ

futurecreator.github.io

 

Stream์„ ํ†ตํ•ด ๋ฐฐ์—ด ๋˜๋Š” ์ปฌ๋ Œ์…˜ ์ธ์Šคํ„ด์Šค์— ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์กฐํ•ฉํ•ด์„œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๊ณ  ๊ฐ€๊ณต๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

Stream์˜ ๋˜ ํ•˜๋‚˜์˜ ์žฅ์ ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ(multi-threading)์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

 

โ€ป์˜ˆ์ œ

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest {
    public static class Person{
        String name;
        int age;
        String address;

        Person(){}

        Person(String name, int age, String address){
            this.name = name;
            this.age = age;
            this.address = address;
        }

        @Override
        public String toString(){
            return "name: "+name+", age:"+age+", address: "+address;
        }
    }

    public static void main(String[] args){
        //1. Stream์˜ ์ƒ์„ฑ
        System.out.println("1. Stream ์ƒ์„ฑ");
        String[] arr = {"John", "John","APC", "AcneStudio", "Stormy", "Stonesy"};
        Stream<String> stream = Arrays.stream(arr);
        stream.forEach(System.out::println);

        //2. distinct()๋ฅผ ํ†ตํ•œ ์ค‘๋ณต ์ œ๊ฑฐ
        System.out.println("\\n2. distinct()๋ฅผ ํ†ตํ•œ ์ค‘๋ณต ์ œ๊ฑฐ");
        //์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
        Stream<String> uniqueStream = Arrays.stream(arr).distinct();
        uniqueStream.forEach(System.out::println);

        //3-1. filter๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ์š”์†Œ ์ถ”์ถœ - A ํฌํ•จ ๋ฌธ์ž์—ด ํ•„ํ„ฐ๋ง
        System.out.println("\\n3. filter()๋ฅผ ํ†ตํ•œ ์›ํ•˜๋Š” ์š”์†Œ ์ถ”์ถœ-A ํฌํ•จ ๋ฌธ์ž์—ด filtering");
        Arrays.stream(arr).filter(word->word.contains("A")).forEach(System.out::println);

        //3-2. filter๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ์š”์†Œ ์ถ”์ถœ - ๋ฌธ์ž์—ด ๊ธธ์ด
        System.out.println("\\n4. filter()๋ฅผ ํ†ตํ•œ ์›ํ•˜๋Š” ์š”์†Œ ์ถ”์ถœ-๋ฌธ์ž์—ด ๊ธธ์ด filtering");
        Arrays.stream(arr).filter(word->word.length()>=5).forEach(System.out::println);

        //4. Stream.iterate: ์ฃผ์–ด์ง„ ์ดˆ๊นƒ๊ฐ’๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ฃผ์–ด์ง„ ์—ฐ์‚ฐ์ž๋ฅผ ๋ฐ˜๋ณต์ ์šฉํ•˜์—ฌ ๋ฌดํ•œํ•œ ์š”์†Œ ์ƒ์„ฑ
        System.out.println("\\n5. Stream.iterate()๋ฅผ ํ†ตํ•œ ์š”์†Œ ์ƒ์„ฑ");
        Stream<Integer> numStream = Stream.iterate(3, n->n+1).limit(10);
        numStream.forEach(n->System.out.print(n+" "));
        System.out.println();

        //5. skip()
        System.out.println("\\n6. skip(), before: "+Arrays.toString(arr));
        Arrays.stream(arr).skip(2).forEach(s->System.out.print(s+" "));
        System.out.println();

        //6. ์ •๋ ฌ
        System.out.println("\\n7-1. Stream ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ");
        Integer[] numArr = {4,1,2,5,3};
        Arrays.stream(numArr).sorted().forEach(n-> System.out.print(n+" "));
        System.out.println("\\n7-2. Stream ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ");
        Arrays.stream(numArr).sorted((n1,n2)->n2-n1).forEach(n-> System.out.print(n+" "));
        System.out.println();

        //7. ๊ฐ์ฒด์™€ Stream
        System.out.println("\\n8. ๊ฐ์ฒดStream ํ•„ํ„ฐ๋ง");
        Person[] persons = {new Person("John", 25, "์„œ์šธ"),
                new Person("Stormy", 5, "LA"),
                new Person("Stonesy", 23, "์„œ์šธ"),};
        Stream<Person> personStream = Arrays.stream(persons);
        personStream.filter(p->p.age>=20).forEach(p-> System.out.println(p));
    }
}

 

728x90

'๊ธฐํƒ€ > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JVM  (0) 2024.10.30
JAVA(1)  (0) 2024.01.20
[๋””์ž์ธํŒจํ„ด]Iterator Pattern  (0) 2021.12.02
[Design Pattern]Adapter Pattern  (0) 2021.11.16
[Design Pattern]Command Pattern  (0) 2021.11.16