john mitchell byeong-mo chang - sookmyungcs.sookmyung.ac.kr/~chang/lecture/gradpl08/java.pdf ·...
TRANSCRIPT
Java
John Mitchell&
Byeong-Mo Chang
Outline
Language Overview• History and design goals
Classes and Inheritance• Object features• Encapsulation• Inheritance
Types and Subtyping• Primitive and ref types• Interfaces; arrays• Exception hierarchy• Subtype polymorphism and
generic programming
Virtual machine overview• Loader and initialization• Linker and verifier• Bytecode interpreter
Bytecode• bytecodes
Origins of the language
James Gosling and others at Sun, 1990 - 95Oak language for “set-top box”• small networked device with television display
– graphics– execution of simple programs– communication between local program and remote site– no “expert programmer” to deal with crash, etc.
Internet application• simple language for writing programs that can be transmitted
over network
Design Goals
Portability• Internet-wide distribution: PC, Unix, Mac
Reliability• Avoid program crashes and error messages
Safety• Programmer may be malicious
Simplicity and familiarity• Appeal to average programmer; less complex than C++
Efficiency • Important but secondary
General design decisions
Simplicity• Almost everything is an object• All objects on heap, accessed through pointers• No functions, no multiple inheritance, no go to, no operator
overloading, few automatic coercions
Portability and network transfer• Bytecode interpreter on many platforms
Reliability and Safety• Typed source and typed bytecode language• Run-time type and bounds checks• Garbage collection
Java System
The Java programming language Compiler and run-time system• Programmer compiles code• Compiled code transmitted on network• Receiver executes on interpreter (JVM)• Safety checks made before/during execution
Library, including graphics, security, etc.• Large library made it easier for projects to adopt Java• Interoperability
– Provision for “native” methods
Java Release History
1995 (1.0) – First public release1997 (1.1) – Nested classes• Support for function objects
2001 (1.4) – Assertions• Verify programmers understanding of code
2004 (1.5) – Tiger• Generics, foreach, Autoboxing/Unboxing,• Typesafe Enums, Varargs, Static Import, • Annotations, concurrency utility libraryhttp://java.sun.com/developer/technicalArticles/releases/j2se15/
Improvements through Java Community Process
Outline
Objects in Java• Classes, encapsulation, inheritance
Type system• Primitive types, interfaces, arrays, exceptions
Virtual machine• Loader, verifier, linker, interpreter• Bytecodes
Language Terminology
Class, object - as in other languagesField – data member Method - member functionStatic members - class fields and methodsthis - selfPackage - set of classes in shared namespaceNative method - method written in another language, often C
Java Classes and Objects
Syntax similar to C++Object • has fields and methods• is allocated on heap, not run-time stack• accessible through reference (only ptr assignment)• garbage collected
Dynamic lookup• Similar in behavior to other languages• Static typing => more efficient than Smalltalk• Dynamic linking, interfaces => slower than C++
Point Class
class Point {private int x;protected void setX (int y) {x = y;}public int getX() {return x;}Point(int xval) {x = xval;} // constructor
};
• Visibility similar to C++, but not exactly (later slide)
Object initialization
Java guarantees constructor call for each object• Memory allocated• Constructor called to initialize memory• Some interesting issues related to inheritance
We’ll discuss later …
Cannot do this (would be bad C++ style anyway):• Obj* obj = (Obj*)malloc(sizeof(Obj));
Static fields of class initialized at class load time• Talk about class loading later
Encapsulation and packages
Every field, method belongs to a classEvery class is part of some package• Can be unnamed default
package• File declares which package
code belongs to
packageclass
field
method
packageclass
field
method
Visibility and access
Four visibility distinctions• public, private, protected, package
Method can refer to• private members of class it belongs to• non-private members of all classes in same package• protected members of superclasses (in diff package)• public members of classes in visible packages
Visibility determined by files system, etc. (outside language)
Qualified names (or use import)
• java.lang.String.substring()
package class method
Inheritance
Similar to Smalltalk, C++Subclass inherits from superclass• Single inheritance only (but Java has interfaces)
Some additional features• Conventions regarding super in constructor and finalize
methods• Final classes and methodS
Example subclass
class ColorPoint extends Point {// Additional fields and methodsprivate Color c;protected void setC (Color d) {c = d;}public Color getC() {return c;}// Define constructorColorPoint(int xval, Color cval) {
super(xval); // call Point constructorc = cval; } // initialize ColorPoint field
};
메쏘드 재정의(Method overriding)
메쏘드재정의란무엇인가?
• 자식클래스가상속된메쏘드를자신이원하는대로재정의하는것
• 새로운메쏘드는부모메쏘드와 이름과서명(signature)이같아야한다.
메쏘드의서명(signature)
• 메쏘드의매개변수이름, 개수, 순서, 타입
재정의된메쏘드실행
• 그메쏘드를실행하는객체의타입에따라호출될메쏘드가결정된다.
메쏘드재정의: 예
class Fruit {int grams;int cals_per_gram;
int total_calories( ) { /* … */ }void peel( ) { System.out.print(“peel a Fruit”); }
}
class Lemon extends Fruit {void squeeze( ) { /* … */ }// Fruit 클래스의 peel() 메쏘드를 Lemon 클래스가 재정의void peel( ) {
super.peel();System.out.println(“, which is a lemon”);
}}
메쏘드재정의: 예
class Example {public static void main(String args[]) {
Fruit fruit = new Fruit();Lemon lemon = new Lemon();
fruit.peel();lemon.peel();
fruit = lemon;fruit.peel();
}}
중복 정의 vs. 재정의
중복정의
• 한클래스내에같은이름의여러개의메쏘드
• 서로다른서명을갖는경우
• 비슷한연산을다른매개변수에대해서다른방식으로정의하는데사용
재정의
• 부모클래스의메쏘드를 자식클래스가재정의
• 서명이같아야한다.
• 시그너처가다르다면어떻게될까?
super 참조
super는 슈퍼클래스를 지칭하는 참조• 슈퍼클래스의멤버필드나메소드를지칭할때사용• super.field• super.method()
super()• super()는 슈퍼클래스의 생성자를 호출• super()는 생성자의 맨 처음 부분에 위치• 서브클래스생성자에서슈퍼클래스의생성자를호출
바인딩(Binding)
바인딩이란무엇인가?
• 이름이가리키는대상을결정하는것
obj.doIt();
• 이호출은호출될 메쏘드를결정즉바인딩한다.
• 컴파일시간에결정하면언제나같은메쏘드가호출될것이다.
동적바인딩(dynamic binding)
• 그러나 Java는동적바인딩을한다
• runtime binding, late binding 이라고도한다.
• 왜동적바인딩을하는가?– 프로그램설계에유연성을제공한다.
다형성(Polymorphism)
다형성(polymorphism)의 의미
• 많은 형태를 갖는다.
• "having many forms“
다형 참조(polymorphic reference) 변수
• 때에 따라 다른 타입의 객체를 참조할 수 있다.
• 다형 참조를 통해 호출된 메쏘드는 호출할 때마다 다를 수 있다.
• Java에서 모든 객체 참조는 다형적일 수 있다.
다형참조
객체참조 변수
• 선언된 클래스의 객체와
• 선언된 클래스의 자손 클래스의 객체를 참조할 수 있다.
예:Fruit fruit;if ( … )
fruit = new Fruit();else
fruit = new Lemon();fruit.peel();
Fruit
Lemon
동적 바인딩
호출될 메쏘드 결정
• 참조 변수의 타입이 아니고
• 실행시간에 참조되고 있는 객체의 타입에 의해 결정된다.
fruit.peel();
• fruit가 Fruit 객체를 참조하는 경우
– Fruit의 peel 호출
• fruit가 Lemon 객체를 참조하는 경우
– Lemon의 peel 호출
Fruitpeel()
Lemonpeel()
Class Object
Every class extends another class• Superclass is Object if no other class named
Methods of class Object• GetClass – return the Class object representing class of the object • ToString – returns string representation of object• equals – default object equality (not ptr equality)• hashCode• Clone – makes a duplicate of an object• wait, notify, notifyAll – used with concurrency• finalize
Final classes and methods
Restrict inheritance• Final classes and methods cannot be redefined
Examplejava.lang.String
Reasons for this feature• Important for security
– Programmer controls behavior of all subclasses– Critical because subclasses produce subtypes
• Compare to C++ virtual/non-virtual– Method is “virtual” until it becomes final
Outline
Objects in Java• Classes, encapsulation, inheritance
Type system• Primitive types, interfaces, arrays, exceptions
Virtual machine• Loader, verifier, linker, interpreter• Bytecodes
Java Types
Two general kinds of times• Primitive types – not objects
– Integers, Booleans, etc
• Reference types– Classes, interfaces, arrays– No syntax distinguishing Object * from Object
Static type checking• Every expression has type, determined from its parts• Some auto conversions, many casts are checked at run time• Example, assuming A <: B
– Can use A x and type – If B x, then can try to cast x to A– Downcast checked at run-time, may raise exception
Classification of Java types
Reference Types
Primitive Types
int
Shape
Object[ ]
Object
Shape[ ]
boolean …
Throwable
Square Square[ ]Circle Circle[ ]
longfloatbyte
Exception types
user-defined arrays
Subtyping
Primitive types• Conversions: int -> long, double -> long, …
Class subtyping similar to C++• Subclass produces subtype• Single inheritance => subclasses form tree
Interfaces• Completely abstract classes
– no implementation
• Multiple subtyping– Interface can have multiple subtypes (extends, implements)
Arrays• Covariant subtyping – not consistent with semantic principles
Java class subtyping
Signature Conformance • Subclass method signatures must conform to those of
superclassThree ways signature could vary• Argument types• Return type • Exceptions
How much conformance is needed in principle?Java rule • Java 1.1: Arguments and returns must have identical types, may
remove exceptions • Java 1.5: covariant return type specialization
Interface subtyping: example
interface Shape {public float center();public void rotate(float degrees);
}interface Drawable {
public void setColor(Color c);public void draw();
}class Circle implements Shape, Drawable {
// does not inherit any implementation// but must define Shape, Drawable methods
}
Properties of interfaces
Flexibility• Allows subtype graph instead of tree• Avoids problems with multiple inheritance of implementations
(remember C++ “diamond”)
Cost• Offset in method lookup table not known at compile • Different bytecodes for method lookup
– one when class is known– one when only interface is known
• search for location of method• cache for use next time this call is made (from this line)
Array types
Automatically defined• Array type T[ ] exists for each class, interface type T• Cannot extended array types (array types are final)• Multi-dimensional arrays as arrays of arrays: T[ ] [ ]
Treated as reference type• An array variable is a pointer to an array, can be null• Example:
Circle[] x = new Circle[array_size]• Anonymous array expression
new int[] {1,2,3, ... 10}Every array type is a subtype of Object[ ], Object• Length of array is not part of its static type
Array subtyping
Covariance• if S <: T then S[ ] <: T[ ]
Standard type errorclass A {…}class B extends A {…}B[ ] bArray = new B[10]A[ ] aArray = bArray // considered OK since B[] <: A[]
aArray[0] = new A() // compiles, but run-time error
// raises ArrayStoreException
Covariance problem again …
Remember Simula problem• If A <: B, then A ref <: B ref• Needed run-time test to prevent bad assignment• Covariance for assignable cells is not right in principle
Explanation• interface of “T reference cell” is
put : T → T refget : T ref → T
• Remember covariance/contravariance of functions
Outline
Objects in Java• Classes, encapsulation, inheritance
Type system• Primitive types, interfaces, arrays, exceptions
Virtual machine• Loader, verifier, linker, interpreter• Bytecodes
Java Implementation
Compiler and Virtual Machine• Compiler produces bytecode• Virtual machine loads classes on demand, verifies bytecode
properties, interprets bytecode
Why this design?• Bytecode interpreter/compilers used before
– Pascal “pcode”; Smalltalk compilers use bytecode
• Minimize machine-dependent part of implementation– Do optimization on bytecode when possible– Keep bytecode interpreter simple
• For Java, this gives portability– Transmit bytecode across network
A.classA.java JavaCompiler
B.class
Loader
Verifier
Linker
Bytecode Interpreter
Java Virtual Machine
Compile source code
Network
Java Virtual Machine Architecture
JVM memory areas
Java program has one or more threadsEach thread has its own stackAll threads share same heap
method area heap Java
stacksPC
registers
native method stacks
Class loader
Runtime system loads classes as needed• When class is referenced, loader searches for file of compiled
bytecode instructions
Default loading mechanism can be replaced • Define alternate ClassLoader object
– Extend the abstract ClassLoader class and implementation– ClassLoader does not implement abstract method loadClass, but
has methods that can be used to implement loadClass
• Can obtain bytecodes from alternate source – VM restricts applet communication to site that supplied applet
Example issue in class loading and linking:
Static members and initialization
class ... {/* static variable with initial value */
static int x = initial_value /* ---- static initialization block --- */static { /* code executed once, when loaded */ }}
Initialization is important• Cannot initialize class fields until loaded
Static block cannot raise an exception• Handler may not be installed at class loading time
JVM Linker and Verifier
Linker• Adds compiled class or interface to runtime system• Creates static fields and initializes them• Resolves names
– Checks symbolic names and replaces with direct references
Verifier• Check bytecode of a class or interface before loaded• Throw VerifyError exception if error occurs
Verifier
Bytecode may not come from standard compiler• Evil hacker may write dangerous bytecode
Verifier checks correctness of bytecode• Every instruction must have a valid operation code • Every branch instruction must branch to the start of some other
instruction, not middle of instruction • Every method must have a structurally correct signature • Every instruction obeys the Java type discipline
Last condition is fairly complicated .
Bytecode interpreter
Standard virtual machine interprets instructions• Perform run-time checks such as array bounds• Possible to compile bytecode class file to native code
Java programs can call native methods• Typically functions written in C
Multiple bytecodes for method lookup• invokevirtual - when class of object known
• invokeinterface - when interface of object known
• invokestatic - static methods
• invokespecial - some special cases
Type Safety of JVM
Run-time type checking• All casts are checked to make sure type safe• All array references are checked to make sure the array index is
within the array bounds• References are tested to make sure they are not null before they
are dereferenced.
Additional features• Automatic garbage collection • No pointer arithmetic
If program accesses memory, that memory is allocated to the program and declared with correct type
Java Sandbox
Four complementary mechanisms• Class loader
– Separate namespaces for separate class loaders– Associates protection domain with each class
• Verifier and JVM run-time tests– NO unchecked casts or other type errors, NO array overflow– Preserves private, protected visibility levels
• Security Manager– Called by library functions to decide if request is allowed– Uses protection domain associated with code, user policy– Recall: stack inspection problem on midterm
Outline
Objects in Java• Classes, encapsulation, inheritance
Type system• Primitive types, interfaces, arrays, exceptions
Virtual machine• Loader, verifier, linker, interpreter• Bytecodes
JVM의 데이터 타입
기초타입(Primitive types)• 정수 관련 타입
– byte(8 bits), short(16 bits), int(32 bits), long(64 bits), char(16 bits, UNICODE)
• 부동소수점 : float(32 bits), double(64 bits)• boolean : true /false• returnAddress : 바이트코드 명령어 주소
참조 타입(Reference types)• 클래스(class)• 배열(array)• 인터페이스9interface)
바이트코드 명령어
8-비트연산코드를갖는 202개명령어
스택기반실행• 오퍼랜드스택을사용• 명령어실행전오퍼랜드들을스택에적재하고결과값도스택에적재• 레지스터는없고대신에지역변수사용
오퍼랜드타입명시• iadd : 정수덧셈
복잡한명령어들• 메모리할당• 모니터/ 쓰레드동기화• 메쏘드호출
바이트코드 명령어 카테고리
Category No. Example
arithmetic operation 24 iadd, lsub, frem logical operation 12 iand, lor, ishl numeric conversion 15 int2short, f2l, d2I pushing constant 20 bipush, sipush, ldc, iconst_0, fconst_1 stack manipulation 9 pop, pop2, dup, dup2 flow control instructions 28 goto, ifne, ifge, if_null, jsr, ret managing local variables 52 astore, istore, aload, iload, aload_0 manipulating arrays 17 aastore, bastore, aaload, baload creating objects and array 4 new, newarray, anewarray, multianewarry object manipulation 6 getfield, putfield, getstatic, putstatic method call and return 10 invokevirtual, invokestatic, areturn miscellaneous 5 throw, monitorenter, breakpoint, nop
바이트코드 명령어 타입
타입힌트• iadd, isub, istore,...• 실행 전 타입 검사 가능
• 보다 안전한 시스템 가능
• 타입 접두사 없는 명령어
– pop, dup, invokevirtual, …
type code
int long float double byte char short reference
i l f d b c s a
예: 자바 코드
static int factorial(int n){
int res;for (res = 1; n > 0; n--) res = res * n;return res;
}
예: 바이트코드
0: iconst 1 // push 11: istore 1 // store it in register 1 (변수 res)2: iload 0 // push register 0 (매개변수 n)3: ifle 14 // if negative or null, goto PC 146: iload 1 // push register 1 (res)7: iload 0 // push register 0 (n)8: imul // mutiply the two integers at top of stack9: istore 1 // pop result and store it in register 1 (res)10: iinc 0, 01 // decrement register 0 (n) by 111: goto 2 // goto PC 214: iload 1 // load register 1 (res)15: ireturn // return its value to caller
자바 스택 및 메쏘드 호출
쓰레드 당 하나의 자바 스택• 자바 스택 위에 스택 프레임들이 쌓인다.
메쏘드 호출 당 하나의 스택 프레임• 오퍼랜드 스택(Operand stack)• 지역 변수(Local variable)• 프레임 크기는 메쏘드가 컴파일될 때 결정된다.
쓰레드와 자바 스택
Java stacks
Thread 1
Thread 2
Thread 3
Thread 1 Thread 2 Thread 3Stack frame
Stack frame
Stack frame
Stack frame
Stack frame
Stack frame
Stack frame
Stack frame
Stack frame
Thread 3
native method stackspc registers
JVM uses stack machine
JavaClass A extends Object {
int ivoid f(int val) { i = val + 1;}
}
BytecodeMethod void f(int)
aload 0 ; object ref thisiload 1 ; int valiconst 1 iadd ; add val +1putfield #4 <Field int i>return data
area
local variables
operandstack
Return addr, exception info, Const pool res.
JVM Stack frame
refers to const pool
메쏘드 호출 명령어
invokevirtual• 객체의 가상(virtual, instance) 메쏘드 호출• 묵시적 매개변수 : this
invokeinterface• 참조 타입이 인터페이스일 때 객체의 가상 메쏘드 호출• 묵시적 매개변수 : this
invokespecial• 실체 초기화 메쏘드, 전용 메쏘드, 수퍼클래스 메쏘드 호출• 묵시적 매개변수 : this
invokestatic• 클래스의 정적 메쏘드(static method) 호출
메쏘드 호출과 this
메쏘드 호출의 구현• 대상 객체(target object)는 0-번째 매개변수로 전달• x.m(…); m(x, …);
메쏘드 선언의 구현• this는 0-번째 형식 매개변수 이름• m(…) { … } m(this, …) { … }
메쏘드 호출과 프레임
메쏘드 m 호출 전• 호출의 대상 객체(target object) 주소를 스택에 넣는다.• 실 매개변수 값들을 스택에 넣는다.
메쏘드 m 호출 후• m을 위한 새로운 프레임을 자바 스택에 넣는다• m의 형식 매개변수에 실 매개변수 값을 복사한다. • m 의 다른 지역 변수를 초기화한다.
메쏘드 호출 예
int method1 () {….method2(a, b);….
}
Local variables
…..
this
a
b
method1
Local variables
…..
this
a
b
Other local variablesof method 2
method1
method2
호출전 호출후
invokevirtual
int add12and13() {
return addTwo(12, 13);
}
Method int add12and130 aload_0 // Push local variable 0 (this)1 bipush 12 // Push int constant 123 bipush 13 // Push int constant 135 invokevirtual #4 // Method Example.addtwo(II)I8 ireturn // Return int on top of operand stack
// it is the int result of addTwo()
invokestatic
int add12and13() {
return addTwoStatic(12, 13);
}
Method int add12and130 bipush 122 bipush 134 invokestatic #3 // Method Example.addTwoStatic(II)I7 ireturn
메쏘드 테이블(Method Table)
클래스 당 자료 구조• 클래스의 메쏘드들에 대한 포인터를 갖는 테이블• 메쏘드 호출을 위한 자료구조• 각 객체는 해당 클래스의 메쏘드 테이블 포인터를 갖는다.
메쏘드 호출 구현• 객체로부터 메쏘드 테이블 포인터를 얻는다. • 메쏘드 아이디를 이용하여 해당 메쏘드의 주소를 얻는다. • 그 주소로 점프한다.
상속과 재정의• 서브클래스는 수퍼클래스의 메쏘드 테이블을 상속 받는다.• 메쏘드가 재정의되면 해당 메쏘드 테이블 엔트리도 갱신된다.
클래스와 메쏘드 테이블
class A {
int foo() {…}
void bar() {…}
};
class B extends A {
int foo() {…}
float boo() {…}
};
address of foo()
address of bar()
01
address of foo()
address of bar()
01
address of boo() 2
Method table of A
Method table of B
메쏘드 호출 예
A a = new A();
a.foo();
….
a = new B();a.foo();
a.bar();
….
a
Data
Data
new A( )
new B( )
address of foo()
address of bar()
01
address of foo()
address of bar()
01
address of boo()2
Object referencevariable
Objects Method tables
예외
JVM은 비정상적 실행 상태를 탐지한다. • Array index out of bounds• Load and link error• Run out of memory
throw 문• 예외를 발생시킨다.
예외 클래스
• Exception 클래스의 서브클래스로 정의• 모든 예외는 예외 클래스의 객체로 throw된다.
예외 처리
예외관리자
• 예외 발생 지점의 문맥(context)를 저장한다. • 예외를 처리할 수 있는 가장 가까운 catch 절을 찾는다.
– 발생된 예외의 타입과 예외 테이블의 catch 절에 선언된 타입을 비교한다.
• 찾으면 예외(에 대한 참조)를 오퍼랜드 스택에 넣고 catch 절을실행한다.
• 찾지 못하면 해당 자바 쓰레드를 종료한다.
정상적인 실행 흐름은 catch 절의 예외 처리기와 분리된다.
JVM의 try-catch
void catchOne() {
try {
tryItOut();
} catch (TestExc e) {
handleExc(e);
}
}
Method void catchOne()
0 aload_0 // Beginning of try block
1 invokevirtual #6 // Method Example.tryItOut()V
4 return // End of try block; normal return
5 astore_1 // Store thrown value in local variable 1
6 aload_0 // Push this
7 aload_1 // Push thrown value
8 invokevirtual #5 // Invoke handler method:
// Example.handleExc(LTestExc;)V
11 return // Return after handling TestExc
Exception table:
From To Target Type
0 4 5 Class TestExc
Java Summary
Objects • have fields and methods• alloc on heap, access by pointer, garbage collected
Classes• Public, Private, Protected, Package (not exactly C++)• Can have static (class) members• Constructors and finalize methods
Inheritance• Single inheritance• Final classes and methods
Java Summary (II)
Subtyping• Determined from inheritance hierarchy• Class may implement multiple interfaces
Virtual machine• Load bytecode for classes at run time• Verifier checks bytecode• Interpreter also makes run-time checks
– type casts– array bounds– …
• Portability and security are main considerations
Some Highlights
Dynamic lookup• Different bytecodes for by-class, by-interface• Search vtable + Bytecode rewriting or caching
Subtyping• Interfaces instead of multiple inheritance• Awkward treatment of array subtyping (my opinion)
Bytecode-based JVM• Bytcode verifier• Security: security manager, stack inspection
Links
Enhancements in JDK 5• http://java.sun.com/j2se/1.5.0/docs/guide/language/index.html
J2SE 5.0 in a Nutshell • http://java.sun.com/developer/technicalArticles/releases/j2se15/
Generics• http://www.langer.camelot.de/Resources/Links/JavaGenerics.ht
m