Professional Documents
Culture Documents
Lesson 01 - Java
Lesson 01 - Java
Ni dung
JDK
B cng c dnh cho lp trnh vin java
c gi l Java Development Kit (JDK).
T version 1.2, b toolkit ny c i tn
thnh Java 2 Software Developer Kit
(J2SDK).
Phin bn hin ti l 1.8.
JDK
JDK gm cc thnh phn:
Compiler.
Debugger.
Java Runtime Environment (JRE).
IDE
Netbeans.
Eclipse.
IntelliJ.
Jbuilder.
Jcreator.
IDE
Trong bi ging ny IDE c s dng l
Netbeans.
Netbeans
Download Netbeans
Truy cp vo trang https://netbeans.org/
Download Netbeans
Ci t
Ci t
Ci t
To project java vi
netbeans
To project java vi
netbeans
To project java vi
netbeans
To project java vi
netbeans
To project java vi
netbeans
C php
Cch t tn bin
Tn bt u bng k t , k t gch di hay
k t $.
Tn khng c cha cc k t khong
trng, k hiu php ton.
Tn khng c trng t kha.
Tn c di bt k.
Tn phn bit hoa thng.
Cch t tn class
Nh cch t tn bin tuy nhin tn class ch
gm cc k t.
Tn class phi trng vi tn file.
Ton t
Ch thch
Ch thch trong java ging c++.
// ch thch mt dng
/* ch thch mt khi lnh
....
*/
Cc kiu d liu
Cc kiu d liu c bn
Kiu s nguyn:
byte: -128 n 127.
short: -215 n 215 - 1.
int: -231 n 231 - 1.
long: -263 n 263 - 1.
Cc kiu d liu c bn
Kiu s thc:
float: 32 bits.
double: 64 bits.
Kiu k t
char: 16 bits.
Kiu l lun
boolean
Kiu chui
Kiu chui:
String: mt dy cc k t Unicode 16 bits (char).
Dng ton t + ni chui.
Phi so snh bng phng thc compareTo.
compareTo
HelloWorld
HelloWorld
Lp v cc i tng c bn
Lp Math
L mt phn ca gi java.lang.
Cha cc phng thc c chc nng tnh
ton v ton hc.
Nh:
Ly tha (pow)
Cn (sqrt)
Tr tuyt i (abs)
....
Lp Collections
Lp h tr x l cc thao tc v mng.
Gm cc interface
Collection
Set
List
Map
Collection
Collection
java.util.Queue
java.util.List
java.util.Set
Summary
List
Cc lp con ca List:
ArrayList
Vector
LinkedList
Set
Cc lp con ca set:
HashSet
TreeSet
....
Map
Map
Cc lp con ca Map:
HashTable
LinkedHashMap
HashMap
TreeMap
Cc thao tc c bn trn
map
Lp I/O
Cc lp chnh lin quan n vic x l IO
u nm trong gi java.io.
Cc lp x l IO trong java c chia thnh 2
loi chnh:
InputStream v OutputStream x l byte, s.
Reader v Writer x l k t v text.
InputStream
InputStream nh ngha cc phng thc sau
int available( )
void close( )
void mark(int numBytes)
boolean markSupported( )
int read( )
int read(byte buffer[ ])
int read(byte buffer[ ], int offset, int numBytes)
void reset( )
long skip(long numBytes)
InputStream
Mt s lp con tiu biu ca InputStream:
FileInputStream
FilterInputStream
ByteArrayInputStream
StringBufferInputStream
SequenceInputStream
PipedInputStream
OutputStream
OutputStream nh ngha cc phng thc
sau:
void close( )
void flush( )
void write(int b)
void write(byte buffer[ ], int offset, int numBytes)
void write(byte buffer[ ])
OutputStream
Mt s lp con ca OutputStream:
FileOutputStream
FilterOutputStream
ByteArrayOutputStream
PipedOutputStream
Console I/O
Console I/O
c v ghi file
c v ghi file di dng byte stream c
thc hin thng qua 2 lp FileInputStream
v FileOutputStream.
c file
To i tng FileInputStream, khi to i
tng vi tn file cn c
Dng cc hm read() c d liu
gi close() ng stream li.
V d
Ghi file
To i tng FileOutputStream, khi to i
tng vi tn file cn ghi.
dng cc hm write() ghi d liu.
dng flush() a d liu cn trong buffer
vo file.
gi close() ng stream li.
V d
c file
To i tng FileInputStream, khi to i
tng vi tn file cn c.
To i tng DataInputStream vi u vo
l FileInputStream.
Dng cc hm read() ca lp
DataInputStream c d liu.
gi close() ng stream.
V d
Ghi file
To i tng FileOutputStream, khi to
i tng vi tn file cn c.
To i tng DataOutputStream vi u
vo l FileOutputStream.
Dng cc hm write() ca lp
DataInputStream ghi d liu.
gi close() ng stream.
V d
V d
Reader
Reader nh ngha cc phng thc sau:
void mark(int numChars)
boolean markSupported( )
int read( )
int read(char buffer[ ])
abstract int read(char buffer[ ], int offset, int
numChars)
int read(CharBuffer buffer)
boolean ready( )
void reset( )
long skip(long numChars)
Reader
Mt s lp con ca Reader:
CharArrayReader
FilterReader
InputStreamReader
PipedReader
StringReader
FileReader
Writer
Writer nh ngha cc phng thc sau:
Writer append(char ch)
Writer append(CharSequence chars)
Writer append(CharSequence chars, int begin,
int end)
void write(String str, int offset, int numChars)
void write(int ch)
void write(char buffer[ ])
void write(String str)
Writer
Mt s lp con ca Writer:
CharArrayWriter
OutputStreamWriter
PipedWriter
StringWriter
PrintWriter
FileWriter
V d
FileReader
FileWriter
InputStreamReader
OutputStreamWriter
PrintWriter
BufferedReader
Lp Thread
Dng lp trnh a lung (multithread).
Nm trong gi java.lang
Lp Thread
Mt thread c th c cc trng thi
Running
Suspended
Blocked
To thread
C 2 cch
K tha lp Thread
Implement interface Runnable
To thread
Lm vic vi Thread
Lp trnh mt ng dng multi thread tng
tc vi bin n vi cc yu cu sau:
Thread 1: nu n < 10000 th cng n thm 1 v
xut ra ch up.
Thread 2: nu n > 0 th gim n i 1 v xut ra ch
down.
Lm vic vi Thread
ng b ha
S dng phng thc Synchronized.
Monitor trn i tng.
iu khin thread
Suspend : tm dng thc thi thread
Resume : kch hot li thread ang tm
dng
Stop : chm dt hot ng ca thread
Ch : cc lnh trn b hy trong phin
bn Java 1.6
Lp Net
Dng lp trnh mng.
Nm trong gi java.net.
Lp Net
Http
URL
URLConnection
TCP - Socket
Socket l u ni trn kt ni 2 chiu gia 2
ng dng trn h thng mng
TCP - ServerSocket
ServerSocket lng nghe trn 1 port ch
cc ng dng kt ni ti.
Khi c client kt ni ti, server ng s to
ra 1 Socket kt ni vi socket ca client.
Server v Client c th c/ghi trn ng
lin kt va to ra.
TCP - Socket
Server
UDP - Datagram
Datagram l mt thng ip c gi ra
mng.
S dng giao thc UDP
Khng m bo : ni dung, thi gian nhn.
Thc hnh