고급 자바 8 교육 (6일 중 1일차)
티맥스소프트 연구소에 연구소장으로 재직 중이던 2013년 10월에 진행한 자바 언어 강의 내용입니다.
JVM에 대한 이해와 Java 8에 대한 소개를 포함하려고 노력하였습니다.
아래 강의 동영상이 있습니다.
http://javadom.blogspot.com/2017/07/8-6.html
7. 자바 교육 계획
1일 : Language, String,
ClassLoader, Proxy
2일 : GC, Collections
3일 : Thread, Java Memory
Model
4일 : AQS, ForkJoin,
Concurrent Utils
5일 : IO, Generics, Annotation,
RMI
6일 : Unsafe, Lambda (Java 8)
7
713년 10월 10일 목
8. History of Java
http://www.java.com/en/javahistory/timeline.jsp
1991 : Green Project Begins (Consumer Electronic Market)
1995 : Java Technology announced
1996 : JDK 1.0 released
1997 : JDK 1.1 released
1999 : Java 2 (JDK 1.2) released
2004 : J2SE 5 released
2006 : Open Source Java announced. Java SE 6 Released
2009 : Oracle acquires Sun Microsystems
2011 : Java SE 7 Released
2014 : Java SE 8 will be released
8
813년 10월 10일 목
9. Getting Started
JDK (Java Development Kit)
Java Document
API Document
Java Language Specification, Java VM Specification
http://docs.oracle.com/javase/specs/
Source code : OpenJDK project page
http://openjdk.java.net
9
913년 10월 10일 목
11. Java Types : Class,
Interface, Primitive
Types : Class, Interface, Functional Interface (JDK 8)
Parameterized Types : Generics (JDK 5)
enum type : compiled to a child class of
java.lang.Enum
Primitive : non-object type. for performance
byte, char, short, int, long, float, double
boolean
11
1113년 10월 10일 목
12. Enum type
not a real Java type
Joshua Bloch’s typesafe enum pattern implementation by Java
compiler
Non-VM type. It’s compiled as a a final child class of
java.lang.Enum class.
static values() method
static valueOf(String) method
protected constructor (String name, int ordinal)
Can be safely used in switch clause as if it’s a cardinal type
12
1213년 10월 10일 목
13. Data Conversion
Widening conversion (implicit)
byte < short < int < long < float < double
char < int
Narrowing conversion (explicit cast required)
Boxing/unboxing conversion (JDK 5)
Numeric promotion
unary op : byte, short, char -> int
binary op : double <- float <- long <- int <- byte, short, char
3 + 3.0f => 6.0f (int to float promotion occurs)
(byte) 3 + (short) 4 => (int) 7 (binary op cannot produce byte, short, char)
13
1313년 10월 10일 목
14. Operator
==, !=, !, &, ^, |, &&, ||, ? :
<, <=, >, >=, +, -, *, /, %, ++, --, <<, >>, >>>, ~, |
unsigned right shift operator (>>>)
Java의 Numeric type은 모두 signed. right shift 시에 부호 비트를 shift할지 여부.
>>의 경우에는 상위 비트에 채워지는 값이 부호 비트 값, >>>의 경우에는 부호 비트의 값에
상관없이 0이 채워진다.
0B1111_1111_1111_1111_1111_1111_1111_1101 ((int) -3)
0B1111_1111_1111_1111_1111_1111_1111_1110 (-3 >> 1 = -2)
0B0111_1111_1111_1111_1111_1111_1111_1110 (-3 >>> 1 = 2147483646)
NOTE: 0b(이진수)와 숫자 사이의 가시성을 위한 ‘_’는 JDK 7부터의 기능
14
1413년 10월 10일 목
15. java.lang.Object
Everything except some primitive types are objects
public Class<?> getClass();
public int hashCode();
protected Object clone() throws CloneNotSupportedException;
public String toString();
public final void notifyAll();
public final void wait(long timeout) throws InterruptedException;
public final void wait() throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException;
protected void finalize() throws Throwable;
15
1513년 10월 10일 목
16. java.lang.String
Immutable (StringBuffer/StringBuilder 참고)
+ operator
compiled to StringBuilder.append() after String conversion using Object.toString()
Compile time String constant reduction : “abc” + “def” -> “abcdef”
Performance problems
Too many concatenation
Concatenation always requires reallocation
Too many objects problem
String constant pool in JVM (Moved from Perm Area to Heap since JDK 7 Hotspot JVM)
intern() method : equals() and ==
16
1613년 10월 10일 목
17. Flow Control
If, while, do - while statement
Continue and break
Exception Handling
try - catch
17
1713년 10월 10일 목
19. For Each (JDK 5)
Syntatic sugar for easy collection iteration
즉, JVM은 이러한 syntax를 전혀 모름
List<String> list = Arrays.asList("a", "b", "c");
for (String s : list) {
System.out.println(s);
}
19
1913년 10월 10일 목
20. Switch /w Strings (JDK 7)
JVM generates more efficient code for switching
depending on Strings
first switch using hashCode
then goto the case statement and call equals
20
2013년 10월 10일 목
22. Try with Resources (JDK 7)
java.lang.AutoCloseable
java.lang.Throwable.addSuppressed(Th
rowable)
java.lang.Throwable.getSuppressed()
Syntactic Sugar
try (BufferedReader br = new
BufferedReader(new FileReader(path))) {
return br.readLine();
}
22
BufferedReader br = null;
try {
br = new BufferedReader(new
FileReader(path));
return br.readLine();
} finally {
if (br != null) {
br.close();
}
}
2213년 10월 10일 목
23. Object Oriented Features
package, class, interface, inner class
Access modifiers
Class access modifiers, Member access modifiers
Inheritance : Single inheritance, Multiple inheritance using interface
Polymorphism
Method overriding/Field hiding
Method overloading
Parameterized types (Generics)
23
2313년 10월 10일 목
24. Access Modifiers
public : class, method, field
private : method, field, inner class only
protected : method, field, inner class only
package private : class, method, field
Less restricted More restricted
public < protected < default (package private) < private
24
2413년 10월 10일 목
25. Inner Class
static inner class
instance inner class
anonymous inner class
automatic name binding
compiler generates boilerplate codes
25
2513년 10월 10일 목
26. this and super
Self or parent referencing in inheritance
Constructor
compiler generates super call if none exist
Static field/method (class field/method)
Instance field/method
final/abstract modifier
26
2613년 10월 10일 목
27. ClassLoader
java.lang.ClassLoader
defineClass, findClass, loadClass
parent ClassLoader
Bootstrap class loader
System.getProperty(“sun.boot.class.path”), -Xbootclasspath: option
Application class loader
parent loader is Extension class loader : jre/lib/ext loading, -classpath option
Custom class loader
Parent first (default)/child first policy
27
2713년 10월 10일 목
28. loadClass
loadClass can be done in parallel (JDK 7)
protected static boolean registerAsParallelCapable();
JDK 7 removed synchronized modifier
see the source code
1.call findLoadedClasses()
2.call parent.loadClass() or findBootstrapClassOrNull()
3.still not found then call findClass()
28
2813년 10월 10일 목
29. class bytecode format
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info
constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
29
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info
methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
2913년 10월 10일 목
32. 다음 시간 예정
JVM의 동작을 Hello, World로..
classloader의 역할도 확실하게 JVM 안에서
지난 강의 reminder부터 (강의 내용에서 한발 나간 다른 측면
을 가볍게 소개)
inner class
class loader / proxy
syntactic sugar들에 대한 정리
32
3213년 10월 10일 목