본문 바로가기

공부/Java28

인텔리제이 generate 사용시 'final 키워드' 설정방법 Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final 출처 : https://stackoverflow.com/questions/29891467/how-to-setup-intellij-idea-14-to-add-final-keyword-where-possible 2022. 3. 21.
자바 제네릭(Generic)이란? 제네릭이란? 제네릭(Generic)은 클래스 내부에서 사용할 데이터 타입을 외부에서 지정하는 기법을 의미한다. class Person { public T info; } public class main { public static void main(String[] args) { Person p1 = new Person(); Person p2 = new Person(); } } → 앞과 뒤의 모양이 같다!! Person p1 = new Person(); Person p2 = new Person(); 제네릭을 왜 쓸까? 사용예시 (기존코드의 중복) class StudentInfo{ public int grade; public StudentInfo(int grade) { this.grade = grade; } .. 2022. 3. 18.
DTO와 VO 비교 결론 먼저! DTO : 데이터 전달 VO : 값 표현 1. DTO Data Transfer Object 데이터를 전달하기 위해 사용하는 객체 데이터를 담아서 전달하는 바구니 DTO 특징 오직 getter/setter 메서드만 갖는다. Setter를 삭제하고 생성자로 받기! 다른 로직을 갖지 않는다. DTO 예시 Dto 클래스 public class CrewDto{ private final String name; private final String nickName; public CrewDto(String name, String nickName) { this.name = name; this.nickName = nickName; } public String getNickName() { return nickN.. 2022. 3. 17.
Stream.forEach와 Collection.forEach의 차이 1. Collection.forEach 객체를 생성하지 않고 forEach 메서드를 호출 동시성 문제 : 수정 감지시 ConcurrentModificationException을 던져 "즉시" 멈춤 2. Stream.forEach Stream()으로 객체를 생성해야만 forEach를 호출 가능 동시성 문제 : 무조건 리스트 끝까지 돌고 예외를 던진다(NullPointerException) parallelStream 사용시 순서가 보장되지 않을 수 있다. 결론적으로, Collection.forEach는 락이 걸려있기에 멀티쓰레드에서 더 안전하다. 2022. 3. 17.
클래스와 인스턴스 1. 클래스 - 클래스는 객체를 보관, 객체를 꺼낼 수 있다. - 더 이상 필요하지 않을 때, 객체를 반환할 수 있는 저장소/웨어하우스 public class LottoNumber { private static final int MINIMUM_NUMBER = 1; private static final int MAXIMUM_NUMBER = 45; private static final LottoNumber[] NUMBERS = new LottoNumber[MAXIMUM_NUMBER + 1]; private int value; static { IntStream.rangeClosed(LottoNumber.MINIMUM_NUMBER, LottoNumber.MAXIMUM_NUMBER) .forEach(number .. 2022. 3. 15.
테스트하기 좋은 메서드 만들기 (인터페이스 분리) 테스트하기 어려운 메서드 public class Car { private static final int MOVABLE_LOWER_BOUND = 4; private static final int RANDOM_NUMBER_UPPER_BOUND = 10; private final String name; private int position; public Car(String name, int position) { this.name = name; this.position = position; } public void move() { final int number = random.nextInt(RANDOM_NUMBER_UPPER_BOUND); if (number >= MOVABLE_LOWER_BOUND) { pos.. 2022. 3. 14.