본문 바로가기
반응형

java37

[Java] Inheritance vs Delegation (상속과 위임) 객체지향 프로그래밍에서 클래스를 설계할 때 상속(Inheritance)과 위임(Delegation) 방식을 고민하게 된다. 상속(Inheritance)과 위임(Delegation) 방식의 차이점과 어떤 상황에서 뭘 써야할지에 대해 알아보자. 클래스의 상속(Inheritance) 객체지향 프로그래밍 언어에서 클래스를 상속(Inheritance)을 통해 정의할 수 있다. class Person { protected int age; protected String name; public Persion(int age, String name) { this.age = age; this.name = name; } public int getAge() { return this.age; } public String getN.. 2019. 7. 2.
[Java] Maven 빌드시 "warning: [options] bootstrap class path not set in conjunction with 1.7" 메이븐 프로젝트를 빌드 할 때, "warning: [options] bootstrap class path not set in conjunction with 1.7" 이라는 경고 메시지가 뜨는 경우가 있다. 이는 메이븐 프로젝트에서 사용한 JAVA 버전과 빌드시 사용한 JAVA 버전이 맞지 않았을 때 발생한다. 자바 프로그램을 개발 할 때, 개발환경으로 여러 버전의 자바를 설치해 사용하는 경우가 흔하다. 예를 들어 JDK 1.7, JDK 1.8 버전이 설치되었을 때를 생각해보자. 메이븐 프로젝트는 1.7 기준으로 작성되어있지만 JAVA_HOME 환경 변수의 JDK는 1.8 경로를 가리킬 때 이런 메시지가 뜰 수 있다. 해결 방법으로는 메이븐 빌드 할 때, 메이븐 프로젝트의 Java 버전으로 JAVA_HOM.. 2019. 6. 20.
[Java] 자바 클래스에서 Getter 메소드 얻어오기 (Introspector) 자바 객체를 이용해서 데이터를 주고 받을 때, 객체 클래스의 Getter 메소드를 호출해야하는 경우가 있다. 특정 객체 클래스의 Getter 메소드를 리플렉션(Reflection)을 이용해서 가져올 수도 있지만 전달하는 객체가 자바빈 객체라면 다음 코드를 사용할 수도 있다. import java.beans.Introspector; import java.beans.PropertyDescriptor; public class Example { private String name; private String address; public String getName() { return name; } public String getAddress() { return address; } public String getA.. 2019. 6. 19.
IntelliJ Code Coverage for Class - Java Synthetic Class, Method, Field 자바 애플리케이션을 구현하고 테스트 케이스를 짜면서 IntelliJ의 'Coverage' 기능을 사용했다. IntelliJ의 'Coverage' 기능은 테스트 케이스가 애플리케이션 코드의 모든 메소드와 클래스, 코드 라인을 수행했는지 알려준다. (링크 : IntelliJ Coverage 기능) 테스트 케이스를 작성하고 코드 커버리지를 확인하는데 이상하게 클래스 항목이 100%를 찍지 못하고 있었다. 다음 자바 소스코드를 보자. public class Coverage { private String name; private String address; public static class Builder { private String name; private String address; public Builde.. 2019. 6. 5.
자바 Exception (Serializable) 정의시 does not define a 'serialVersionUID' Warning 해결하기 자바 프로젝트에서 예외처리를 위해 Exception 클래스를 정의하다보면 "'XXX' does note define a 'serialVersionUID'"라는 경고 문구를 만나게 될 때가 있다. 다음 Exception 클래스를 작성하면 개발환경 설정에 따라서 경고 메시지를 보게 된다. public class TestException extends IOException { public TestException(String message) { super(message); } } 경고 문구를 해석하자면 'serialVersionUID'를 정의하지 않았다는 의미다. 'serialVersionUID'가 뭐길래 경고 메시지를 띄우는 것일까? serialVersionUID serialiVersionUID를 정의하지 .. 2019. 6. 2.
Java Reflection Exception : IllegalAccessException,IllegalArgumentException, InvocationTargetException 설명 자바 리플렉션을 이용해서 클래스의 메소드를 호출할 일이 있었다. 클래스에 있는 모든 Getter 메소드를 호출해서 멤버 값을 가져오는 코드를 다음과 같이 작성했다. import java.lang.reflect.Method; public class Test { private String name; private String address; public Test(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public String getAddress() { return address; } private static boolean isGetter(M.. 2019. 6. 1.
자바 컬렉션(Java Collection)들의 Big O (시간 복잡도, Time Complexity) 자바는 다양한 컬렉션 타입들을 제공한다. 이 컬렉션 타입들은 내부 구현에 따라 다양한 시간 복잡도를 갖는데, 이 특징을 잘 알고 사용해야 불필요한 성능저하를 피할 수 있다. List Add Remove Get Contains Data Structure ArrayList O(1) O(n) O(1) O(n) Array LinkedList O(1) O(1) O(n) O(n) LinkedList CopyonWriteArrayList O(n) O(n) O(1) O(n) Array LinkedList의 시간복잡도에서 remove 연산의 시간복잡도가 O(1)인 이유는 삭제할 엘리먼트에 대한 레퍼런스를 알고 있다고 가정하기 때문에 얻어진 것이다. 만약 삭제할 대상을 탐색하는 동작(get)까지 감안한다면 O(n)이 맞을.. 2019. 5. 31.
[Java] SAXParser Exception - "[Fatal Error] :1:1: Premature end of file" SAX 파서를 이용한 라이브러리를 사용하다가 "[Fatal Error] :1:1: Premature end of file." 혹은 "[Fatal Error] :1:1: 예기치 않은 파일의 끝입니다."라는 Exception을 만나게 되는 경우가 있다. 이 예외는 SAX 파서의 입력 스트림이 예기치 않게 끝났을 경우에 발생한다. 대표적으로 SAX 파서에 빈 문자열을 입력하면 발생하게 된다. 다음 예제를 살펴보자. import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.. 2019. 5. 30.
[Java] Unmodifiable Collection vs Immutable 차이점 카프카 컨슈머 코드리뷰를 하다가 'Unmodifiable Collection'이라는 것을 보게 되었다. 카프카에서 본 소스코드는 다음과 같았다. private List drainRecords(int n) { if (isDrained() || position >= records.size()) { drain(); return Collections.emptyList(); } // using a sublist avoids a potentially expensive list copy (depending on the size of the records // and the maximum we can return from poll). The cost is that we cannot mutate the returned .. 2019. 5. 29.
반응형