160 likes | 307 Vues
This document explores the fundamental concepts of object-oriented programming (OOP) using Java. It covers the creation of objects, memory management with stack and heap, and explains the importance of references. It discusses primitive types versus wrapper classes, object lifetime, and how Java handles memory through garbage collection. Practical examples help illustrate the use of classes, methods, and the static keyword. This resource is perfect for beginners looking to understand OOP principles and their implementation in Java.
E N D
2. Everything Is an Object Java : “pure” Object-oriented language junyoung@sparcs.kaist.ac.kr
A씨 성적 관리 입력하기 합계내기 출력하기 ….. 이름: A씨 전화번호: 010.***.**** E-mail: AA@sparcs.org Object 명함 A씨’s 생활?? A씨’s 명함 A씨 성적관리 You manipulate objects with reference. • Everything → Object • Object → reference로 조작한다! B A C
You must create all the object Ex)String s; • s 라는 이름의 String형을 가리킬 reference만 만든다. • 이 상태에서 s는 어떤 것도 참조하고 있지 않다! (null) 모든 object들은 생성되어야 한다! (‘new’ keyword) Ex) s =newString(“asdf”); • 참조 s에 새로운 객체를 생성하여 연결… String s = new String(“asdf”); asdf s
Create the object • Object가 생성되면! Ex) String s =newString(“asdf”); • s 라는 reference(참조)는 stack에 저장! • new 로 생성된 object(객체)는 heap에 저장! • heap • 모든 java object가 저장되는 memory pool. • 메모리 저장공간을 유연하게 사용가능 (컴파일러는 heap에 어느 정도 공간을 차지하는지,어느 정도 시간 동안 저장되는지 알 필요가 없다!) • 대신, heap의 storage를 할당하는데 시간이 많이 걸림. • 간단한 변수까지도 heap에 저장? → 비효율적
Special case : primitive types • new 가 필요 없는 기본형! • object, reference가 생성되지 않는다. • 값 자체가 정해진 size만큼 stack에 저장되어짐 (portablility) Ex) int c = 1; char name = ‘A’; boolean p = true;
Special case : primitive types • wrapper class • you can make a non-primitive object on the heap to represent that primitive type. Ex) int c = 1; Integer c=new Integer(1); char name = ‘A’; Character name = new Character (‘A’);
Lifetime of variable • scoping { int x=12; { int q=96; // x, q 모두 있음 } // x 만 남아있음 // q “out of scope” } • {~} 범위 내에서 선언된 변수는 그 안에서만 살아있다!
Lifetime of object { String s = new String(“a str”); } • new에서 object가 heap에 생성됨 • } scope의 끝에서! stack의 reference는 사라지고.. object는 heap에 그대로 남아있다.. • But, reference가 사라졌으므로 더 이상 이 object에 접근할 방법이 없다! • Garbage collector!더 이상 Reference 되고 있지 않은 object를 찾아 이것을 차지하던 memory를 free하게!
class • object를 생성하는 형판 (object의 type) • java program → class들의 집합 classATypeName{ Class1 var1 = value1; char var2 = ‘a’; int var3 = 5; void method1 (){ … } int method2 (){ … } } ….. ATypeName a = newATypeName();
class • Fields : Member variables (멤버 변수) • 다른 class의 object / primitive type Class1 var1 = value1; char var2 = ‘a’; int var3 = 5; • Methods : Member function (멤버 함수) • object의 행위, 기능 … Ex) a.vv=47; System.out.println(a.var2);
성적 관리 입력하기 합계내기 출력하기 ….. Methods returnType MethodName( arg1, arg2, … ){ … } • returnType : method를 호출한 위치에서 return되는 형(type) • void 는.. return 되는 값이 없다는 뜻 class 성적관리 { void 입력하기(입력할 숫자) { } int 합계내기(성적1, 성적2, …) { } } … 성적관리 AA = new 성적관리(); AA.입력하기(100); int sum = AA.합계내기(100, 80, 90);
Using other components • 이미 정의되어있는 class 이용 해보자. • package • a set of class • java, util • package • import java.util.ArrayList; • 표준 java library에 있는 util이란 폴더에 있는 ArrayList class를 사용한다.. • import java.util.*;
The ‘static’ keyword • 특정한 object의 것이 아니다! • static method • class의 object를 생성하지 않아도 호출 가능하다. Ex) class StaticTest{ static void method(){ } } … StaticTest.method();
The ‘static’ keyword • 특정한 object의 것이 아니다! Ex) class StaticTest{ static int i=47; } … StaticTest st1 = new StaticTest(); StaticTest.i++; // st1.i = 48 StaticTest st2 = new StaticTest(); StaticTest.i++; // st1.i = st2.i = 49
HelloDate.java • 책 보고 해보기! import java.util.*; public class HelloDate{ public static void main(String[] args){ System.out.println( “Hello” ); System.out.print( new Date() ); } }