본문 바로가기

프로그래밍/java

Jenkov.com - Java Lambda Expressions Java Lambda Expressions - Single Method Interface 자바8 람다 표현식은 컴파일 시에 인터페이스의 형식을 보고 추정(Inference)하여 기동된다.public interface StateChangeListener { public void onStateChange(State oldState, State newState); }stateOwner.addStateListener(new StateChangeListener() { public void onStateChange(State oldState, State newState) { // do something with the old and new state. } });stateOwner.addStateListener( (.. 더보기
Jenkov.com - Java Enum & Annotation Enum 상수는 아래의 예제 하나면 끝.public enum Level { HIGH (3), //calls constructor with value 3 MEDIUM(2), //calls constructor with value 2 LOW (1) //calls constructor with value 1 ; // semicolon needed when fields / methods follow private final int levelCode; Level(int levelCode) { this.levelCode = levelCode; } public int getLevelCode() { return this.levelCode; } } 자바의 Annotation 은 크게 3가지 방식으로 사용이 가능한데, C.. 더보기
Jenkov.com - Java Interfaces http://tutorials.jenkov.com/java/interfaces.html 자바의 인터페이스는 "메소드 구현에 대한 껍데기"를 제공하는 역할이다.Public 변수를 전달할 수도 있지만, 추천할 만한 구조는 아니다. 즉, 상태 보다는 행위에 대한 정의를 해둔다고 볼 수 있겠다. 그렇다면 추상 클래스와 인터페이스의 차이점은? 구현체가 있냐 없냐의 차이? 자바의 경우 다중상속을 지원하지 않는데, 이러한 효과를 누리고 싶다면?추상클래스와 인터페이스를 섞어서 쓰면 좋은데, 추상클래스 상속 후 인터페이스를 덕지 덕지 붙이지 말고, 인터페이스를 구현한 최종 추상클래스를 상속받는다면 깔끔한 구조가 된다. 더보기
Jenkov.com - Java Abstract Class http://tutorials.jenkov.com/java/abstract-classes.html 자바의 추상 클래스는 크게 사용할 일이 없었으나, 아래와 같이 전/후처리는 정해진 프로세스대로 진행되고, 구현되어야 할 액션만 하위 클래스에서 구현하도록 하는 패턴.public abstract class MyAbstractProcess { public void process() { stepBefore(); action(); stepAfter(); } public void stepBefore() { //implementation directly in abstract superclass } public abstract void action(); // implemented by subclasses public v.. 더보기
Jenkov.com - Java Nested Classes http://tutorials.jenkov.com/java/nested-classes.html 자바 Nested Class 종류는 네 가지(?)나 있다. Local / Anonymous 클래스는 거의 써보지 않았으나, * Static Nested Class : 말 그대로 nested static class public class Outer { public static class Nested() {} }Outer.Nested nested = new Outer.Nested(); * Normal Nested Class : 말 그대로 내장 클래스public class Outer { public class Inner() {} }Outer.Inner inner = new Outer().new Inner(); * L.. 더보기
Jenkov.com - Java String String Concatenation Performance자바 문자열은 컴파일 시에 + 을 StringBuilder 형태로 append 하므로, StringBuilder를 통해 명시적으로 구현하는 것이 성능에 도움. String[] strings = new String[]{"one", "two", "three", "four", "five" }; String result = null; for(String string : strings) { result = result + string; // Call new StringBuilder for every iteration. } StringBuilder temp = new StringBuilder(); for(String string : strings) { tem.. 더보기
Java Math Operators and Math Class http://tutorials.jenkov.com/java/math-operators-and-math-class.html Java Math OperatorsJava Integer Math - cut off int result = 100 / 8;The result of this division would be 12.5 , but since the two numbers are integers, the .5 fraction is cut off. The result is therefore just 12. Java Floating Point Math - use D or Fdouble result = 100D / 8D; Floating Point Precisiondouble resultDbl3 = 0D;System.. 더보기
Jenkov.com - Java Data Types Java Data Typeshttp://tutorials.jenkov.com/java/data-types.html The variable itself does not contain the object, but contains a reference to the object. The reference points to somewhere else in memory where the whole object is stored. Via the reference stored in the variable you can access fields and methods of the referenced object. Object Versions of Primitive Data Types Are Immutable.자바의 기본 .. 더보기
자바의 실수연산에 대한 실수 자바의 경우 모든 실수 연산에서 소수점 오류가 발생하는 것은 아닙니다. 아래의 코드와 결과를 보면 이해하실 겁니다. @Test public void testPrintJavaDouble() { for (double i=0; i 더보기
CopyOnWriteArrayList in Java What is CopyOnWriteArrayList in Java - Example Tutorial java.util.concurrent.CopyOnWriteArrayList 자바를 사용한 지는 1.2 때부터 사용했지만, 1.3 이후부터는 사실상 추가된 Spec에 대해 공부한 적이 별로 없다. 최근 "Effective Java 2nd"을 발췌독 중에 재미있는 내용이 있어 간략히 정리해 본다. 대게 자바의 List 는 ArrayList를 많이 사용하게 되는데 Thread-safe 하지 않기 때문에 Iterator 혹은 For-each 문 내에서 remove 메소드를 사용하면 ConcurrentModificationException 예외를 자주 만나게 된다. 이런 경우 나는 별도의 객체를 생성하여 add 하.. 더보기