25
“Object Programming(2)” 객체지향 프로그래밍(2)

Java mentoring of samsung scsc 2

  • Upload
    -

  • View
    138

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Java mentoring of samsung scsc   2

“Object Programming(2)”

객체지향 프로그래밍(2)

Page 2: Java mentoring of samsung scsc   2

1. Static

Page 3: Java mentoring of samsung scsc   2

Static?

당연하게 사용해오던 것

public class staticmain {

public String name;

public static void main(String[] args) {

DumpClass dc = new DumpClass();

dc.print();

System.out.println(name);

printsn();

}

private void printsn() {

};

}

public class DumpClass {

public void print() {

}

}

같은 메서드호출무엇이 다를까?

Page 4: Java mentoring of samsung scsc   2

Static?

당연하게 사용해오던 것

public class staticmain {

public static String name;

public static void main(String[] args) {

DumpClass dc = new DumpClass();

dc.print();

System.out.println(name);

printsn();

}

private static void printsn() {

};

}

public class DumpClass {

public void print() {

}

}

Static이 무엇이길래. 추가하자 마자실행이 되는것일까?

Page 5: Java mentoring of samsung scsc   2

클래스 메서드와 인스턴스 메서드

public class staticmain {

public static void main(String[] args) {

System.out.println(Calcuator.add(200,100));

System.out.println(Calcuator.sub(100,100));

System.out.println(Calcuator.mul(200,100));

System.out.println(Calcuator.div(200,100));

System.out.println(Calcuator.num0);

System.out.println(Calcuator.num1);

System.out.println("Check!!!");

Calcuator calc = new Calcuator();

calc.num0 = 200;

calc.num1 = 100;

System.out.println(calc.add());

System.out.println(calc.sub());

System.out.println(calc.mul());

System.out.println(calc.div());

}

}

class Calcuator{

static double num0 = 1000, num1 = 1000;

double add() {return num0 + num1;}

double sub() {return num0 - num1;}

double mul() {return num0 * num1;}

double div() {return num0 / num1;}

static double add(double num0, double num1) {

return num0 + num1;

}

static double sub(double num0, double num1) {

return num0 - num1;

}

static double mul(double num0, double num1) {

return num0 * num1;

}

static double div(double num0, double num1) {

return num0 / num1;

}

}

결과는 같아도 다르다!

- 공유 변수를 사용해야하는 것에 Static을 붙인다.- 클래스변수는 인스턴스를 생성하지 않아도 사용가능하다.- 클래스메서드는 인스턴스변수를 사용할수 없다.- 메서드 내에서 인스턴스변수를 사용하지 않는다면, static을 붙이는 것을 고려한다.

Page 6: Java mentoring of samsung scsc   2

클래스 메서드와 인스턴스 메서드

public class InstanceTest {

void instanceMethod() {}

static void staticMethod() {}

void instanceMethod2() {

instanceMethod();

staticMethod();

}

static void staticMethod2() {

instanceMethod();

staticMethod();

}

}

package statica;

public class InstanceTest {

int instance_var;

static int static_var;

void instanceMethod() {

System.out.println(instance_var);

System.out.println(static_var);

}

static void staticeMethod() {

System.out.println(instance_var);

System.out.println(static_var);

}

}

Page 7: Java mentoring of samsung scsc   2

Static – *Runtime Data Areas?

Class loaderSubSystem

ExecutionEngine

MethodArea

PC RegistersJava Virtual

Machine stacks

Heap

Native MethodStacks

Runtime Data Area

Java API

Java ClassFile

NativeMethodLibraries

Page 8: Java mentoring of samsung scsc   2

*PC Registers

CPU에 직접 인스트럭션을 수행하지 않음.(이는 JAVA의 철학과 관계가있다)

Tread마다 하나씩 존재하며 Thread가 시작할때 생성

Pc RegistersThread

Java 메서드를수행중

Native 메서드를 수행중

Page 9: Java mentoring of samsung scsc   2

*Java Virtual Machine Stacks

Thread의 수행 정보를 기록하는 메모리 영역

Thread 별로 하나씩 존재하며 Thread가 시작할때 수행(동기화 문제가 발생하지 않는 직접적인 원인)

Stack Frame

Stack Frame

Stack Frame

Stack Frame

Stack Frame

Stack Frame

Thread

CurrentLocal Variable Section- Method Parameters

- Local variable

Operand Stack- JVM의 Work Space

Frame Data-Constant pool Resolution- Normal Method Returen

-Exception Dispatch

Page 10: Java mentoring of samsung scsc   2

*Java Virtual Machine Stacks - Ex

Class CallStackTest {

public static void main(String[] ar) {

Sysout(“메인 시작!”);

firstMethod();

Sysout(“메인 끝!”);

}

static void firstMethod() {

Sysout(“first Method 시작!”);

secondMethod();

Sysout(“first Method 끝!”);

}

static void secondMethod() {

Sysout(“(“Second Method 시작!”);

Sysout(“(“Second Method 끝!”);

}

}

Page 11: Java mentoring of samsung scsc   2

*Local Variable Section

Method의 Parameter Variable과 LocalVariable의 저장공간

reference 0 [this]

int a0

int a1

long a2

float a3

reference a4

double a5

reference a6

int a7

int a8

int a9

Class CallStackTest {

public int LocalVariableSecionMethod(CallStackTest this, int a0, char a1, long a2,

float a3, Objecta4, double a5, String a6, byte a7, short a8, boolean a9) {

return 0;

}

}- Primitive Type과 Reference 타입을구분하여 사용하여야 하는 이유가이곳에 있다.

-char, byte, boolean형으로 선언한 것은JVM의 지원여부와 JVMS의 저장형태에따라 달라진다.LocalVariableSection이나 Operand Stack에서는 int형으로 변환하여 저장되고Heap에서는 복원되어 자장된다.

- boolean은 Heap에서도 int형을 유지한다.

Page 12: Java mentoring of samsung scsc   2

1 byte 2 byte 4 byte 8 byte

논리형 boolean

문자형 char

정수형 byte short int long

실수형 float double

참조형 String

*Local Variable Section

Page 13: Java mentoring of samsung scsc   2

*Operand Stack

연산을 위해 사용되는 데이터 및 결과를 처리하는 곳

Class SumPro {

public void sum() {

int num0, num1, result;

num0 = 1;

num1 = 2;

result = num0 + num1;

}

}

public void sum();

Code:

0: iconst_1

1: istore_1

2: bipush 2

3: istore_2

4: iload_1

5: iload_2

6: iadd

7: istore_3

8: return

ref

OS

LVSnum0

num1

result

this

Page 14: Java mentoring of samsung scsc   2

*Frame Data

Constant Pool Resolution 정보와 Method가 정상 종료됬을떄의정보 비정상 종료시 Exception정보들이 저장되는 공간

Symbolic Reference(참조정보)

Direct Reference

Resolution

변환

Constant Pool

ref

ref

ref

LiteralConstant(상수정보)

Page 15: Java mentoring of samsung scsc   2

*Frame DataClass SumPro {

public void sum() {

int num0, num1, result;

num0 = 1;

num1 = 2;

try {

result = num0 + num1;

}catch(NullPointerException e) {

result = 0;

}

}

}

public void sum();

Code:

0: iconst_1

1: istore_1

2: bipush 2

3: istore_2

4: iload_1

5: iload_2

6: iadd

7: istore_3

8: goto 11

9: astore4

10: iconst_0

11: istore_3

12: return

Exception table:

from to target trpe

4 8 9 Class n…

ref

OS

LVSnum0

num1

result

this

Page 16: Java mentoring of samsung scsc   2

*Native Method Stacks

StackFrame

StackFrame

StackFrame

JavaStacks

StackFrame

JavaMethod 1

NativeMethod 1

JavaMethod 2

Native FunctionCall

NativeMethodStack

NMS를 호출하면 Method의 StackFrame을Push하여 Native Function을 계속 실행!

C로 작성 -> C StackC++로 작성 -> C++ Stack

생성

새로운Stack Frame

생성!

새로운Stack Frame

생성!

Page 17: Java mentoring of samsung scsc   2

*Method Area

Type Information

Type Information inMethod Area

Load된 Type을 저장하는 논리적 메모리 공간.모든 Thead들의 공유하는 메모리 영역

JVM이 작동할때 생성되며 GC의 대상이된다.

Constant Pool

Field Information

Method Information

Class Variables

Reference to class(ClassLoader)

Reference to class(Class)

Page 18: Java mentoring of samsung scsc   2

*Heap

Object Layout

Object, Array 객체 두가지 종류만 저장되는 공간.모든 Thread에 공유된다.

Instruction(new, newarray, anewarray, multianewarray)만 존재

VTable

Lock workd

Object Data

VTable

Lock workd

Object Data

Array Size

Object Information-Debug Data-Class name-Object Type

(Object or Array)-Reference filed 여부

-Object size, Array Length- Offset Information

VTable

0Thread ID(15bits) Count

1Hash Code(10 bits)

Monitor index (20 bits)

Connection Bit(1bit) Recursion Count(5 bits)

Recursion Bit(1 bits)

Page 19: Java mentoring of samsung scsc   2

*Heap

Nursery

Allocation Space Survivor Space Tenured Space

Tenured

-Allocation Space : Obj가 최초로 할당되는곳-Survivor Space : AS가 꽉차거나 Failure가 발생시

AS의 Obj를 이동하는 곳-Tenured Space: Nursery 영역의 성숙된 Obj를 Promotion하는 곳

-Parallel Compaction Algorithm을 사용함.

EX)T1 T2

Page 20: Java mentoring of samsung scsc   2

*Runtime Data Areas

Class Variable – MA에 할당. 모든 Thread 공유Member Variable – MA정보를 바탕으로 Heap에 할당

Parameter Variable – MA의 Method info에 할당Local Variable – JVMS내의 LVS에 할당

Class RuntimeDataAreasTest {

static int ci= 3;

static String cs = “Static Class Var”

int mi = 4;

string ms = “Member”

void method(int pi, String ps) {

int li = 5;

String is = “S_Local_Var”

}

}

Page 21: Java mentoring of samsung scsc   2

*Runtime Data Areas

Java Stack Java Heap Method Area

Method frame

Local Variable

3

2

1

0

is – ref4

li – 5

ps

pi

Method frame

mi = 4;ms = ref2

String Instance : ref1

“Static Class Var”

String Instance : ref2

“Member”

String Instance : ref3

String Instance : ref4

“S_Local_Var”

[Ljava/lang/String]

RuntimeData Area Testclass

Filed Information

Class Variable

ci 3cs ref1

Method informations

variableArrange();Void method(int, String);

Static {};

Constant Pool

Operand Stack

Page 22: Java mentoring of samsung scsc   2

*Runtime Data Area Simulation과 GC의 종류

Page 23: Java mentoring of samsung scsc   2

클래스 메서드와 인스턴스 메서드

기본값명시적초기화변환

클래스초기화블럭

기본값명시적초기화변환

인스턴스초기화블럭

생성자

- 클래스변수 추기화 시점 : 클래스가 처음 로딩될 때 단한번 초기화 된다.

-인스턴스변수 초기화시점 : 인스턴스가 생성될 때마다 각 인스턴스별로초기화가 이루어진다.

Page 24: Java mentoring of samsung scsc   2

클래스 메서드와 인스턴스 메서드public class InitTest {

static int static_var = 1;

int instance_var = 1;

static {

static_var = 2;

}

{

instance_var = 2;

}

InitTest() {

instance_var = 3;

}

}

클래스 초기화 인스턴스 초기화

기본값 명시적초기화

클래스초기화블럭

기본값 명시적초기화블럭

인스턴스초기화블럭

생성자

static_var = 0 static_var = 1 static_var = 2 static_var = 2instance_var = 0

static_var = 2instance_var = 1

static_var = 2instance_var = 2

static_var = 2instance_var = 3

1 2 3 4 5 6 7

Page 25: Java mentoring of samsung scsc   2

감사합니다.