Académique Documents
Professionnel Documents
Culture Documents
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
Nguyn Vit H
Thm v Java
Nguyn Vit H
Thm v Java
Kiu d liu char byte short int long float double boolean
Gi tr cc tiu 0xffff
Gi tr cc i
Nguyn Vit H
Thm v Java
D liu c lu tr u
D liu kiu nguyn thy
thao tc thng qua tn bin
Nguyn Vit H
Thm v Java
stack 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
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
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 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
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 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
Nguyn Vit H
Thm v Java
19
Nguyn Vit H
Thm v Java
20
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
Nguyn Vit H
Thm v Java
24
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
Nguyn Vit H
Thm v Java
28
Nguyn Vit H
Thm v Java
29
Nguyn Vit H
Thm v Java
Nguyn Vit H
Thm v Java
31
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
Nguyn Vit H
Thm v Java