문제 https://leetcode.com/problems/container-with-most-water/ Container With Most Water - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이코드 package SITE03_leetcode.medium; import java.util.Arrays; /** * https://leetcode.com/problems/zigzag-conversion/ */ public class M007_leetCode..
Read more제네릭 타입 클래스와 인터페이스 선언에 타입 매개변수가 쓰이면 이를 제네릭 클래스 혹인 제네릭 인터페이스라고 한다. 이를 통틀어 제네릭 타입(generic type)이라고 한다. List 예를들어, List 인터페이스는 원소의 타입을 나타내는 타입 매개변수 E 를 받는다. 각각의 제네릭 타입은 일련의 매개변수화 타입을 정의한다. List 은 원소의 타입이 String 인 리스트를 뜻하는 매개변수화 타입이다. 여기서 String 이 정규(formal) 타입 매개변수 E 에 해당하는 실제(actual) 타입 매개변수다. 로 타입 (Raw Type) 제네릭 타입을 하나 정의하면 그에 딸린 로 타입(raw type)도 함께 정의된다. 로 타입이란, 제네릭 타입에서 타입 매개변수를 전혀 사용하지 않을 때를 말한다..
Read more톱레벨 클래스 소스 파일 하나에 톱레벨 클래스를 여러개 선언하더라도 자바 컴파일러는 불평하지 않는다. 하지만 아무런 득이 없고 심각한 위험을 감수해야한다. 어느 소스파일을 먼저 컴파일하느냐에 따라 결과가 달라질 수 있는 위험이 있는데, 이를 예제를 통해 알아보자. Utensil.java package com.java.effective.item25; class Utensil { static final String NAME = "pan"; } class Dessert { static final String NAME = "cake"; } Main.java package com.java.effective.item25; public class Main { public static void main(String[]..
Read more중첩 클래스 중첩 클래스(nested class)란 다른 클래스 안에 정의된 클래스를 말한다. 중첩된 클래스는 자신을 감싼 바깥 클래스에서만 쓰여야하며, 그 외의 쓰임새가 있다면 톱레벨 클래스로 만들어야한다. 중첩 클래스의 종류 1) 정적 멤버 클래스 바깥 클래스와 함께 쓰일때만 유용한 public 도우미 클래스 2) (비정적) 멤버 클래스 바깥 클래스의 인스턴스와 암묵적으로 연결된다. 어댑터를 정의할때 자주 쓰인다. 멤버 클래스에서 바깥 인스턴스를 참조할 필요가 없다면 무조건 정적 멤버 클래스로 만들자. 3) 익명 클래스 바깥 클래스의 멤버가 아니며, 쓰이는 시점과 동시에 인스턴스가 만들어진다. 비정적인 문맥에서 사용될 때만 바깥 클래스의 인스턴스를 참조할 수 있다. 자바에서 람다를 지원하기 전에 즉석..
Read more태그 달린 클래스 태그 클래스란, 두가지 이상의 의미를 표현할 때 그 중 현재 표현하는 의미를 태그값으로 알려주는 클래스다. package com.java.effective.item23; class Figure { enum Shape { RECTANGLE, CIRCLE }; // 태그 필드 - 현재 모양을 나타낸다. final Shape shape; // 다음 필드들은 모양이 사각형(RECTANGLE)일 때만 쓰인다. double length; double width; // 다음 필드는 모양이 원(CIRCLE)일 때만 쓰인다. double radius; // 원용 생성자 Figure(double radius) { shape = Shape.CIRCLE; this.radius = radius; } // 사각..
Read more문제 https://leetcode.com/problems/reverse-integer Reverse Integer - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이코드 class Solution { public int reverse(int x) { long result = 0; // int) 1534236469 잘못된 결과 while (x != 0) { // X 가 0이라면 마지막까지 계산 종료 int target = x; target = x % 10; ..
Read more상수 인터페이스 인터페이스는 자신을 구현한 클래스의 인스턴스를 참조할 수 있는 타입 역할을 한다. 클래스가 어떤 인터페이스를 구현한다는 것은 자신의 인스턴스로 무엇을 할수 있는지를 클라이언트에 얘기해주는 것이다. 인터페이스는 이 용도로만 사용해야한다. 예외적으로 상수 인터페이스라는 것은 static final 필드로만 가득찬 인터페이스인데, 메서드가 존재하지 않는다. public interface PhysicalConstantsInterface { static final String MESSAGE = "Hello"; static final String ALERT = "World"; } 상수 인터페이스는 클래스에서 정규화된 이름을 사용하는 것을 피하고자 인터페이스에 구현한 것이다. 상수 인터페이스는 잘못 ..
Read more문제 https://leetcode.com/problems/zigzag-conversion ZigZag Conversion - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이코드 package SITE03_leetcode.medium; import java.util.Arrays; /** * https://leetcode.com/problems/zigzag-conversion/ */ public class M006_leetCode6_ZigZagConversio..
Read more디폴트 메서드의 문제 자바 8 전에는 기존 구현체를 깨뜨리지 않고는 인터페이스에 메서드를 추가할 방법이 없었다. 인터페이스에 메서드를 추가하면 컴파일 오류가 발생한다. 인터페이스에 메서드를 추가할 수 있도록 디폴트 메서드가 자바 8이후로 출현했지만 위험이 완전히 사라진건 아니다. 디폴트 메서드를 선언하면, 그 인터페이스를 구현한 후 디폴트 메서드를 재정의하지 않은 모든 클래스에서 디폴트 구현이 쓰이게 된다. 모든 기존 구현체들과 매끄럽게 연동되진 않는다. 디폴트 메서드는 구현 클래스에 대해 아무것도 모른채 합의 없이 무작정 삽입될 뿐이다. 문제상황 자바 8에서 컬렉션 인터페이스들에 다수의 디폴트 메서드가 추가됬지만, 모든 상황에서 불변식을 해치지 않는 디폴트 메서드를 작성하긴 어려웠다. Collectio..
Read more요건사항 USER 테이블에서 USER_GRP_ID 의 그룹 별 USER_GENDER 개수와 USER_AGE 개수를 알고싶다. SELECT * FROM USER; USER_ID USER_GENDER USER_AGE USER_GRP_ID 1 F 22 U001 2 F 22 U001 3 M 20 U001 4 M 20 U002 5 F 23 U003 6 M 23 U003 원하는 결과 USER_GRP_GENDER_CNT USER_GRP_AGE_CNT USER_GRP_ID 2 2 U001 1 1 U002 2 1 U003 설명 ** U001의 경우 USER_GENDER = F,M, USER_AGE = 22, 20 일 경우로 각 2개, 2개가 나온다. ** U002의 경우 USER_GENDER= M, USER_AGE = ..
Read more문제 https://leetcode.com/problems/median-of-two-sorted-arrays Median of Two Sorted Arrays - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이코드 package SITE03_leetcode.medium; import java.util.Arrays; /** * https://leetcode.com/problems/median-of-two-sorted-arrays */ public class M..
Read moreSerializable interface Serializable.java public interface Serializable { } Person.java class Person implements Serializable { String name; String job; // transient String job; // transient : 이 멤버는 직렬화 대상에서 제외한다. (default 값 null 로 출력될거임) public Person() { } public Person(String name, String job) { this.name = name; this.job = job; } public String toString() { return name + "," + job; } } Serializ..
Read more다중 구현 메커니즘 자바가 제공하는 다중 구현 메커니즘은 인터페이스, 추상클래스 2가지다. 자바 8부터 인터페이스도 디폴트 메서드를 제공할 수 있게되었다. 디폴트 메서드란? https://devfunny.tistory.com/350 자바8의 default 메서드 등장 디폴트 메서드의 등장 자바 8에서는 기본 구현을 포함하는 인터페이스를 정의하는 2가지 방법을 제공한다. 만약 인터페이스를 바꾸게 되었을때, 해당 인터페이스를 구현한 모든 클래스의 구현 devfunny.tistory.com 추상클래스 추상 클래스와 인터페이스의 가장 큰 차이는 추상 클래스가 정의한 타입을 구현하는 클래스는 반드시 추상 클래스의 하위 클래스가 되어야한다는 점이다. 자바는 단일 상속만 가능하므로, 추상 클래스 방식은 새로운 타입을..
Read moreinterface Comparable 매개변수를 1개 받는다. package java.lang; import java.util.*; public interface Comparable { /* ... */ public int compareTo(T o); } Comparable 구현하기 1) 오름차순 public class Member implements Comparable { ... public int compareTo(Member member) { /** 오름차순 출력 */ if (this.memberId > member.memberId) { return 1; } else if (this.memberId < member.memberId) { return -1; } else { return 0; } } }..
Read more문제 https://leetcode.com/problems/longest-palindromic-substring Longest Palindromic Substring - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이코드 package SITE03_leetcode.medium; /** * https://leetcode.com/problems/longest-palindromic-substring/ */ public class M003_leetCode5_Long..
Read more