Vous êtes sur la page 1sur 37

Hiu thm v Java

Ni dung
D liu kiu nguyn thy v i tng Tham chiu Gii phng b nh Gi v kim sot truy cp Kiu hp thnh (composition) Vo ra vi lung d liu chun

Nguyn Vit H

Thm v Java

Ti liu tham kho


Thinking in Java, chapter 2, 4, 5 Java how to program, chapter 4,5,6,7,8

Nguyn Vit H

Thm v Java

Kiu d liu nguyn thy


Java cung cp cc kiu nguyn thy
s: byte, short, int, long, float, double
khng c khi nim unsigned kch thc c nh trn mi platform

logic: boolean k t: char

D liu kiu nguyn thy khng phi l i tng


int a = 5; if (a==b)

Tn ti lp i tng tng ng: Interger, Float,..


Interger count = new Interger(0);

Nguyn Vit H

Thm v Java

Kiu d liu char byte short int long float double boolean

rng (bits) 16 0x0

Gi tr cc tiu 0xffff

Gi tr cc i

8 -128 (-27) 16 -32768 (-215) 32 - 231, 0x80000000 64 - 263 32 1.40129846432481707e-45 64 4.94065645841246544e-324

+127 (27-1) 32767 (215-1) + 231 - 1, 0x7fffffff + 263 - 1 3.40282346638528860e+38 1.79769313486231570e+308

Nguyn Vit H

Thm v Java

D liu c lu tr u
D liu kiu nguyn thy
thao tc thng qua tn bin

D liu l thuc tnh ca i tng


i tng c thao tc thng qua tham chiu

Vy bin kiu nguyn thy, tham chiu v i tng c lu tr u?

Nguyn Vit H

Thm v Java

3 vng b nh cho ng dng


code static data constants temporary data static memory

stack memory

dynamic data heap memory

Nguyn Vit H

Thm v Java

Tham chiu
i tng c thao tc thng qua tham chiu
l con tr ti i tng thao tc trc tip ti thuc tnh v phng thc khng c cc ton t con tr php gn (=) khng phi l php ton copy ni dung i tng

tham chiu c lu tr trong vng nh static/stack nh cc con tr trong C/C++

Nguyn Vit H

Thm v Java

Ton t New
Phi to mi i tng mt cch tng minh bng ton t new
cp pht vng nh ng c to trong b nh Heap

V d:
MyDate d; MyDate birthday; d = new MyDate();
Nguyn Vit H Thm v Java 9

Php gn =
Php gn khng phi l copy thng thng
copy ni dung ca tham chiu hai tham chiu s tham chiu n cng i tng Integer m = new Integer(10); Integer n = new Integer(20); m = n; n.setValue(50); System.out.print(m);

Nguyn Vit H

Thm v Java

10

New v =
MyDate d; new operation MyDate birthday; d = new MyDate(26,9,2005); birthday = d;
assign operation
Static/Stack memory

d birthday
Nguyn Vit H Thm v Java

Heap memory

26-9-2005
11

Ton t quan h ==
So snh ni dung ca cc d liu kiu nguyn thy (int, long, float, ) So snh ni dung ca tham chiu ch khng so snh ni dung ca i tng do tham chiu tr n
Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); -false true
Nguyn Vit H Thm v Java 12

So snh ni dung i tng


class MyDate { ... boolean equalTo(MyDate d) { ... } } ... MyDate d1 = new MyDate(10,10,1954); MyDate d2 = new MyDate(d1); System.out.println(d1.equalTo(d2));
13

Nguyn Vit H

Thm v Java

Gii phng b nh ng
(Garbage collection)
Lp trnh vin khng cn phi gii phng i tng JVM ci t c ch Garbage collection gii phng t ng cc i tng khng cn cn thit
tuy nhin, GC khng nht thit hot ng vi mi i tng

GC tng tc pht trin v tng tnh n nh ca ng dng


Khng phi vit m gii phng i tng Do , khng bao gi qun gii phng i tng
Nguyn Vit H Thm v Java 14

GC hot ng nh th no
S dng c ch m?
mi i tng c mt s m cc tham chiu tr ti gii phng i tng khi s m = 0

Gii phng cc i tng cht


kim tra tt c cc tham chiu nh du cc i tng cn c tham chiu gii phng cc i tng khng c tham chiu
Nguyn Vit H Thm v Java 15

Garbage Collection
MyDate openDate = new MyDate(1,10,2005); MyDate startDate = new MyDate(10,10,2005); openDate = startDate; released automatically openDate

1-10-2005

startDate

10-10-2005

Nguyn Vit H

Thm v Java

16

Truyn tham s v nhn gi tr tr li


Truyn gi tr
i vi d liu kiu nguyn thy gi tr ca tham s (RValue) c copy ln stack c th truyn hng s (vd: 10, 0.5, )

Truyn tham chiu


i vi i tng ni dung ca tham chiu (LValue) c copy ln stack
Nguyn Vit H Thm v Java 17

Truyn tham s tr
class MyDate { ... public boolean setYear(int y) { ... } public int getYear() { return year; } ... } ... MyDate d = new MyDate(); d.setYear(1975); int y = d.getYear();

Nguyn Vit H

Thm v Java

18

Truyn tham chiu


class MyDate { int year, month, day; public MyDate(int y, int m, int d) { year = y; month = m; day = d; } public void copy(MyDate d) { d.year = year; d.month = month; d.day = day; } public MyDate copy() { return new MyDate(day, month, year); } ... }

Nguyn Vit H

Thm v Java

19

Truyn tham chiu


MyDate d1 = MyDate(2005, 9, 26); MyDate d2 = MyDate(2000, 1, 1); d1.copy(d2); MyDate d3; d3 = d1.copy();

Nguyn Vit H

Thm v Java

20

Tham chiu this


Java cung cp tham chiu this tr ti chnh i tng ang hot ng this c s dng vo cc mc ch nh
tham chiu tng minh n thuc tnh v phng thc ca i tng truyn tham s v tr li gi tr dng gi constructor
Nguyn Vit H Thm v Java 21

this lm gi tr tr li
class Counter { private int c = 0; public Counter increase() { c++; return this; } public int getValue() { return c; } } ... Counter c = new Counter(); System.out.println(c.increase().increase().getValue());

Nguyn Vit H

Thm v Java

22

this lm tham s
class Document { Viewer vi; ... Document(Viewer v) { vi = v; ... } void display() { v.display(this); } ... }
Nguyn Vit H Thm v Java 23

Gi constructor bng this


class MyDate { private int year, month, day; public MyDate(int y, int m, int d) { ... } // copy constructor MyDate(MyDate d) { this(d.year, d.month, d.day); System.out.println(copy constructor called); } ... } Constructor ch c gi bn trong mt constuctor khc v ch c gi mt ln thi im (v tr) u tin.

Nguyn Vit H

Thm v Java

24

Phng thc v thuc tnh static


C th khai bo phng thc v thuc tnh l tnh (static)
c lp vi i tng c th s dng m khng cn c i tng

Phng thc tnh


khng s dng c thuc tnh thng thng (non-static) khng gi c cc phng thc thng thng
Nguyn Vit H Thm v Java 25

Gi cc lp i tng (package)
Cc lp i tng c chia thnh cc gi
nu khng khai bo th cc lp thuc gi default cc lp trong cng mt tp m ngun lun thuc cng mt gi

Tn ti mc truy cp package
mc package l mc nh (nu khng khai bo tng minh l public hay private) cc i tng ca cc lp thuc cng gi c th truy cp n non-private members ca nhau ch c th to (new) i tng ca lp c khai bo l public ca gi khc

Nguyn Vit H

Thm v Java

26

Hello.java: class HelloMsg { void sayHello() { System.out.println(Hello, world!); } } public class Hello { public static void main(String[] args) { HelloMsg msg = new HelloMsg(); msg.sayHello(); } }
Nguyn Vit H Thm v Java 27

Khai bo v s dng package


Khai bo gi bng lnh package
cc gi c lu tr theo cu trc cy th mc s dng tham s -d to th mc khi bin dch

Dng lnh import khai bo vic s dng mt gi c

Nguyn Vit H

Thm v Java

28

i tng hp thnh (Composition)


i tng c th cha cc i tng khc (cc thuc tnh khng thuc kiu nguyn thy) Thuc tnh l tham chiu phi c to ra bng new hoc c gn cho mt i tng tn ti class Person { private String name; private MyDate birthday; ... }

Nguyn Vit H

Thm v Java

29

Get v Set thuc tnh khng thuc kiu nguyn thy


class Person { public MyDate getBirthday() { return birthday; } } Person p = new Person(...); MyDate d = p.getBirthday(); d.setYear(1900);
30

Nguyn Vit H

Thm v Java

S dng copy constructor


class Person { private String name; private MyDate birthday; public Person(String s, MyDate d) { name = s; birthday = new MyDate(d); } public MyDate getBirthday() { return new MyDate(birthday); } public void setBirthday(MyDate d) { birthday = new MyDate(d); } ... }

Nguyn Vit H

Thm v Java

31

Vo ra t lung d liu chun


Lung ra chun: System.out
xut ra lung ra chun (standard output) c th ti nh hng

Lung thng bo li: System.err


xut ra Console (thit b output chun) khng th ti nh hng

Lung d liu vo chun: System.in


cha sn sng cho s dng
Nguyn Vit H Thm v Java 32

Nhp d liu t lung vo chun


InputStream: lp i tng ng vi lung vo chun
System.in: i tng tng ng cha c phng thc nhp d liu

InputStreamReader: nhp d liu khng thng qua buffer


c tng k t (k c k t c bit)

BufferedReader: s dng buffer


c tng dng
Nguyn Vit H Thm v Java 33

V d
import java.io.*; public class Echo { public static void main(String[] args) throws IOException { InputStreamReader reader; BufferedReader bufReader; reader = new InputStreamReader(System.in); bufReader = new BufferedReader(reader); String s; while( null != (s = bufReader.readLine()) System.out.println(s); } }

Nguyn Vit H

Thm v Java

34

Nhp mt s
import java.io.*; class SimpleIO { public static void main(String args[]) throws IOException { int n; String str; ... str = bufReader.readLine(); Integer num = Integer.valueOf(str); n = num.intValue(); System.out.println(n); } }

Nguyn Vit H

Thm v Java

35

Nhp mt s
import java.io.*; class SimpleIO { public static void main(String args[]) throws IOException { int n; String str; ... str = bufReader.readLine(); n = Integer.valueOf(str).intValue(); System.out.println(n); } }

Nguyn Vit H

Thm v Java

36

Tham s dng lnh


CmdLineParas.java: public class CmdLineParas { public static void main(String[] args) { for (int i=0; i<args.length; i++) System.out.println(args[i]); } } V d: #java CmdLineParas hello world hello world
37

Nguyn Vit H

Thm v Java

Vous aimerez peut-être aussi