Backend/JAVA

[JAVA] 특강 3일차 : 자바 문제풀이 & 코드리뷰

JOKUN 2022. 6. 23. 23:47

 

 

 

parseInt & break & finally

📑문제

 

⭕정답

ABCD

 

🔍풀이

package jokun.test;

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

        while(!false){
            try {
                System.out.print("A");
                Integer.parseInt("Hello World");
            } catch (Exception e) {
                System.out.print("B");
                break;
            } finally {
                System.out.print("C");
            }
        }
        System.out.println("D");
    }
}

 

 

 

 

 

CallByValue

📑문제

 

⭕정답

Hello

 

🔍풀이

package jokun.test;

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

        String str1 = "Hello";
        String space = " ";
        test0622_Ex02 question_7 = new test0622_Ex02();

        question_7.method1(str1 + space);
        System.out.println(str1);
    }
    private void method1(String str1) {
        String str2 = "World";
        str1 += str2;
    }
}

 

 

 

 

 

final & 상수 값 변경

📑문제

 

⭕정답

15행

 

🔍풀이

package jokun.test;

import java.util.ArrayList;
import java.util.List;

public class test0622_Ex03 {
    public static void main(String[] args) {
        final List<String> list1 = new ArrayList<>();
        
        list1.add("This");
        list1.add("list");
        list1.add("is");
        list1.add("final.");
        
        List<String> list2 = new ArrayList<>();

        System.out.println(list2.size());
        
        list1 = list2;

        System.out.println(list1);
    }
}

 

package jokun.test;

import java.util.ArrayList;
import java.util.List;

public class test0622_Ex03 {
    public static void main(String[] args) {
        final List<String> list1 = new ArrayList<>();

        list1.add("This");
        list1.add("list");
        list1.add("is");
        list1.add("final.");

        List<String> list2 = new ArrayList<>();
        list2.addAll(list1);

        System.out.println(list2.size());
        
        System.out.println(list1);
    }
}

 

 

 

 

HashMap & clear() & isEmpty & 키 중복

📑문제

 

⭕정답

flase

 

🔍풀이

package jokun.test;

import java.util.HashMap;

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

        HashMap<Integer, Integer> map = new HashMap<>();
        
        map.put(1, 2);
        map.put(3, 4);
        
        map.clear();
        
        if(map.isEmpty()) {
            map.put(1, 1);
            map.put(1, 2);
        }

        System.out.println(map.get(1) > map.size());

    }
}

 

 

 

 

 

 

NullPointerException & Null Cheak

📑문제

 

⭕정답

런타임 에러

 

🔍풀이

package jokun.test;

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

        Integer a = method1("1");
        
        if (a == 0 && a != null) {
            System.out.println(a);
        }
    }
    
    private static Integer method1 (String str) {
        if (Integer.parseInt(str) > 0){
            return null;
        }else {
            return 100;
        }
    }
}

 

[에러 없이 실행하게 만들기]

package jokun.test;

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

        Integer a = method1("1");

        if (a != null) {
            System.out.println("a값 : " + a);
        } else {
            if( a == null){
                System.out.println("a값이 null이네요!");
            }
        }
    }

    private static Integer method1 (String str) {
        if (Integer.parseInt(str) > 0){
            return null;
        }else {
            return 100;
        }
    }
}