분류 전체보기

    [Java] String, StringBuilder, StringBuffer 에 대해

    String, StringBuilder, StringBuffer 에 대해 String 은 불변 객체이다. 불변 객체에 대해 잘 모른다면 이 포스팅부터 보자 (불변 객체) String str = "a"; str = str + "b"; System.out.println(str); // "ab" 위의 String 연산은 기존의 str에 "b"가 붙어서 "ab"가 된 것처럼 보이지만 사실은 새로운 메모리 영역을 할당한 것이다. 이 말인 즉슨, String을 통한 문자열 추가 삭제를 빈번히 하게된다면 힙 메모리 영역에 많은 가비지가 생기게 된다는 것이다. 이럴 때에 사용하는 것이 StringBuilder, StringBuffer 이다. 이 둘은 가변성을 가지기 때문에 동일 메모리에서 문자열 변경이 가능하다. 다음..

    [Java] Immutable Object (불변 객체) 에 대해

    보기 더 편한 양식은 여기서~!(github.com/Be-poz/TIL/blob/master/Java/Immutable%20Object%20(%EB%B6%88%EB%B3%80%20%EA%B0%9D%EC%B2%B4)%20%EC%97%90%20%EB%8C%80%ED%95%B4.md) Immutable Object (불변 객체) 에 대해 불변 객체는 생성 후에 그 상태를 바꿀 수 없는 객체를 말한다. 반대 말로는 가변 객체(mutable)가 있다. 불변 객체는 복제나 비교를 위한 조작을 단순화 시켜주고 성능 개선에 도움을 주지만, 변경 가능한 데이터를 많이 가지고 있는 경우에는 오히려 독이 되기도 한다. class Ex{ public int num; public Ex(int num){ this.num = nu..

    [Spring] @BeforeEach @BeforeAll @AfterEach @AfterAll 에 대해

    @BeforeEach @BeforeAll @AfterEach @AfterAll 에 대해 테스트를 실행 시에 반복되는 세팅 로직 등이 있을 때에 이 어노테이션들을 사용할 수가 있다. 코드로 바로 확인해보자 @SpringBootTest public class eachAllTest { @BeforeEach public void beforeEach() { System.out.println("BeforeEach"); } @BeforeAll static void beforeAll(){ System.out.println("BeforeAll"); } @AfterEach public void afterEach() { System.out.println("AfterEach"); } @AfterAll static void a..

    [Spring] Validator 생성 시 주의해야 할 점, Invalid target 오류

    Validator 생성 시 주의해야 할 점, Invalid target 오류 Custom 한 Validator 를 많이들 생성할 것이다. 이번에 나는 프로젝트를 수행 도중에 크게 막히는 부분이 있었다. Invalid target for Validator [com.ticket.captain.festival.validator.FestivalCreateValidator@5d035ab6]: com.ticket.captain.festival.dto.FestivalUpdateDto@3407ded1 바로 다음과 같은 오류였다. 이 오류가 발생할 당시에 코드는 다음과 같았다. // validate 메서드는 생략 @Component @RequiredArgsConstructor public class FestivalCre..

    [Java] equals, ==, hashCode 의 차이와 재정의 시점에 대해

    equals, ==, hashCode 의 차이와 재정의 시점에 대해 보통 값 타입들을 비교할 때에 == 연산자를 사용한다. int a = 3; int b = 3; //a == b true 하지만, String 타입에서는 == 비교를 사용하면 원하는 결과값을 얻어내지 못할 것이다. 왜냐하면, == 연산은 정확히는 주소값을 비교하는 연산자이기 때문이다. String은 new를 사용해서 새롭게 인스턴스를 만들고 메모리에 올리기 때문에 다른 주소값을 참조하기에 == 연산 시에 false가 리턴된다. String a = "abcd"; String b = "abcd"; String c = new String("abcd"); String d = new String("abcd"); System.out.println(a..

    [자료구조] HashSet, LinkedHashSet, TreeSet 의 원리에 대해

    HashSet, LinkedHashSet, TreeSet 의 원리에 대해 HashSet과 TreeSet은 Set 인터페이스의 구현 클래스이다. LinkedHashSet은 HashSet을 상속받아 구현이 된다. Set은 알다시피 중복값을 허용을 안한다. LinkedHashSet은 HashSet과는 다르게 저장 순서를 유지해주고 TreeSet은 들어온대로 정렬을 해준다. 그렇다면 이들의 내부 코드는 어떻게 되어있길레 이런 기능들을 하는건지 살펴보자. public HashSet() { map = new HashMap(); } public int size() { return map.size(); } public boolean contains(Object o) { return map.containsKey(o); ..

    [Spring] @NotNull, @NotEmpty, @NotBlank 에 대해

    @NotNull, @NotEmpty, @NotBlank import javax.validation.constraints 에 들어가있는 세 어노테이션에 대해 알아보겠다. @NotNull @Setter @Getter public class BoardDto { @NotNull String value; } @BeforeAll public static void setupValidatorInstance(){ validator = Validation.buildDefaultValidatorFactory().getValidator(); } @Test public void validationTest() throws Exception{ BoardDto boardDto = new BoardDto(); Set violation..

    [Spring] @Builder에 대해

    @Builder에 대해 public class Member{ private final String email; private final String name; public static class Builder{ private final String email; private final String name; public Builder email(String val){ email = val; return this; } public Builder name(String val){ name = val; return this; } public Member build(){ return new Member(this); } } private Member(Builder builder){ email = builder.em..