6. 2.1. Gi畛i thi畛u Java
C叩c phi棚n b畉n ch鱈nh c畛a Java
Standard Edition (Java SE ho畉c J2SE)
o C嘆n g畛i l ng担n ng畛 l畉p tr狸nh Java
o D湛ng 畛 vi畉t: 畛ng d畛ng desktop, applets, java FX, 畛ng d畛ng web kh担ng
c畉n Java EE
Enterprise Edition (Java EE ho畉c J2EE)
o L java ch畉y tr棚n c叩c 畛ng d畛ng server
o D湛ng 畛 vi畉t: Servlet, JSP, JSF, Strut, EJB, Spring, Hibernate,
o V鱈 d畛: Google home page, gmail, Google Maps, Google Docs
Micro Edition (Java ME)
o L java ch畉y tr棚n thi畉t b畛 di 畛ng v nh炭ng
o 畛ng d畛ng vi畉t cho: TDT, PDA, TV set-top box, m叩y in
6
7. 2.1. Gi畛i thi畛u Java
JDK Java Development Kit
JDK l m畛t m担i tr動畛ng ph叩t tri畛n d湛ng 畛 x但y d畛ng c叩c 畛ng d畛ng,
applet v c叩c component b畉ng ng担n ng畛 l畉p tr狸nh Java.
Bao g畛m:
o Development tools (bin/)
o Runtime environment (jre/)
o Additional libraries (lib/)
o Java DB (db/)
o Source code (src.zip)
o
7
8. 2.2. C畉u tr炭c ch動董ng tr狸nh Java
8
/*
* Created on Jul 14, 2005
*
* First Java Program
*/
package com.jds.sample;
import java.util.*;
/**
* @author JDS
*/
public class JavaMain {
public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
}
class Extra {
/*
* class body
*/
}
1. Single Line Comment
// insert comments here
2. Block Comment
/*
* insert comments here
*/
3. Documentation Comment
/**
* insert documentation
*/
Comments
9. 2.2. C畉u tr炭c ch動董ng tr狸nh Java
9
/*
* Created on Jul 14, 2005
*
* First Java Program
*/
package com.jds.sample;
import java.util.*;
/**
* @author JDS
*/
public class JavaMain {
public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
}
class Extra {
/*
* class body
*/
}
1. Package declaration
Used to organize a collection of related classes.
2. Import statement
Used to reference classes.
3. Class declaration
A Java source file can have several
classes but only one public class is
allowed.
A file can have more classes, but
the file name must match with one in it
Declaration order
13. 2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u c董 s畛
B畉ng gi叩 tr畛 c畛a c叩c ki畛u c董 s畛
Type Bits Lowest Value Highest Value
boolean (n/a) false true
char 16 'u0000' [0] 'uffff' [216-1]
byte 8 -128 [-27] +127 [27-1]
short 16 -32,768 [-215] +32,767 [215-1]
int 32 -2,147,483,648 [-231] +2,147,483,647 [231-1]
long 64
-9,223,372,036,854,775,808 [-
263]
+9,223,372,036,854,775,807 [263-1]
float 32 賊1.40129846432481707e-45 賊3.40282346638528860e+38
double 64 賊4.94065645841246544e-324 賊1.79769313486231570e+308 13
14. 2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u tham chi畉u
Ki畛u d畛 li畛u tham chi畉u bi畛u di畛n cho 畛i t動畛ng (object)
Java c坦 3 ki畛u d畛 li畛u tham chi畉u:
o Class
o Array
o Interface
14
16. 2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: Khai b叩o
Khai b叩o v s畛 d畛ng bi畉n
o Bi畉n l m畛t gi叩 tr畛 c坦 th畛 thay 畛i khi ch動董ng tr狸nh th畛c thi
o C叩ch khai b叩o:
o V鱈 d畛:
int total;
int count, temp, result;
Ki畛u d畛 li畛u T棚n bi畉n
int sum = 0;
int base = 32, max = 149;
<ki畛u_d畛_li畛u> <t棚n_bi畉n>;
<ki畛u_d畛_li畛u> <t棚n_bi畉n> = <gi叩_tr畛>;
16
17. 2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: Khai b叩o
Khai b叩o bi畉n c坦 ki畛u d畛 li畛u c董 s畛:
int age = 0;
Khai b叩o bi畉n c坦 ki畛u d畛 li畛u tham chi畉u (khai b叩o object):
String name = new String("Jason");
Date bday = new Date(2018, 12, 24);
17
t棚n
bi畉n
ki畛u
c董 s畛
gi叩 tr畛
kh畛i t畉o
t棚n 畛i
t動畛ng
ki畛u tham
chi畉u
gi叩 tr畛 kh畛i t畉o
18. 2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: c畛c b畛, ton c畛c
Bi畉n c畛c b畛: ph畉i 動畛c kh畛i t畉o gi叩 tr畛 tr動畛c khi s畛 d畛ng
Bi畉n ton c畛c: ph畉i 動畛c khai b叩o b棚n ngoi c叩c hm v b棚n trong
class, v鱈 d畛:
class Example {
static int x = 10;
public static void main(String args[ ]) {
System.out.println(x);
}
}
18
19. 2.3. T畛ng quan l畉p tr狸nh Java
H畉ng
Khai b叩o v s畛 d畛ng h畉ng (constant)
o Gi叩 tr畛 h畉ng kh担ng 動畛c ph辿p thay 畛i trong ch動董ng tr狸nh, n坦 ch畛 動畛c
g叩n gi叩 tr畛 m畛t l畉n 畛 d嘆ng khai b叩o h畉ng
o Trong Java, d湛ng final 畛 khai b叩o h畉ng, v鱈 d畛:
final int MIN_HEIGHT = 69;
19
27. 2.3. T畛ng quan l畉p tr狸nh Java
M畉ng
L m畛t c畉u tr炭c d畛 li畛u cho ph辿p l動u m畛t t畉p c叩c gi叩 tr畛 c湛ng ki畛u
27
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
scores
The entire array
has a single name
Each value has an index
28. 2.3. T畛ng quan l畉p tr狸nh Java
Khai b叩o m畉ng
C炭 ph叩p:
V鱈 d畛 khai b叩o m畉ng:
o int[] scores = new int[100];// m畉ng scores ch畛a 100 ph畉n t畛 s畛 nguy棚n
o String[] names;
names = new String[20]; // m畉ng names ch畛a 20 chu畛i
V鱈 d畛 khai b叩o v g叩n gi叩 tr畛 c湛ng l炭c:
o int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
o char[] letterGrades = {'A', 'B', 'C', 'D', F'};
28
DataType[] arrayName = new DataType[size];
DataType[] arrayName; // only declares the variable
arrayName = new DataType[size];
29. 2.3. T畛ng quan l畉p tr狸nh Java
S畛 d畛ng m畉ng
Duy畛t m畉ng a:
V鱈 d畛:
o for (int i=0; i < scores.length; i++)
System.out.println(scores[i]);
o for (int score : scores)
System.out.println(score);
o for (String n : names)
System.out.println(n);
Bi t畉p: t畉o m畉ng s畛 nguy棚n c坦 10 ph畉n t畛, g叩n gi叩 tr畛 b畉t k畛 v xu畉t m畉ng.
29
for (int i=0; i < a.length; i++)
// process a[i]
for (Type x : a)
// process x
length field
returns the number
of elements
30. 2.4. Ngo畉i l畛
Exception l m畛t s畛 ki畛n x畉y ra trong qu叩 tr狸nh th畛c thi ch動董ng tr狸nh,
ph叩 v畛 lu畛ng b狸nh th動畛ng c畛a ch動董ng tr狸nh
V鱈 d畛:
30
31. 2.4. Ngo畉i l畛
Ph但n lo畉i
Trong Java c坦 2 lo畉i exception l Checked Exception v
Unchecked Exception
o Checked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m compile. Nh畛ng
exception ny th動畛ng li棚n quan 畉n l畛i c炭 ph叩p (syntax) v b畉t bu畛c
ch炭ng ta ph畉i "b畉t" (catch) n坦.
o Unchecked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m runtime.
Nh畛ng exception ny th動畛ng li棚n quan 畉n l畛i logic v kh担ng b畉t bu畛c
ch炭ng ta ph畉i "b畉t" (catch) n坦.
31
32. 2.4. Ngo畉i l畛
X畛 l箪 ngo畉i l畛 trong Java
Keyword
o try catch
o finally
o throw
o throws
32
34. 2.4. Ngo畉i l畛
S畛 d畛ng try...catch: V鱈 d畛 1
34
public class Zero {
public static void main (String[] args) {
try {
int tu = 10;
int mau = 0;
System.out.println (tu/mau);
System.out.println ("This text will not be printed.");
}
catch (ArithmeticException e) {
System.out.println ("Kh担ng th畛 chia cho 0"); // tb l畛i
}
}
}
38. 2.4. Ngo畉i l畛
S畛 d畛ng throw
S畛 d畛ng throw khi ng動畛i l畉p tr狸nh mu畛n t畛 ph叩t sinh ra ngo畉i l畛, vd:
38
public static void main (String[] args) {
try {
final int MIN = 25, MAX = 40;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter an integer value between " + MIN + " and " + MAX + ", inclusive: ");
int value = scan.nextInt();
if (value < MIN || value > MAX)
throw new Exception(); // make an exception
else
System.out.println("Gia tri nhap hop le");
System.out.println ("End of main method."); // may never be reached
}
catch( Exception x ) { // catch the exception
System.out.println("Number is not valid.");
}
39. 2.4. Ngo畉i l畛
S畛 d畛ng throws
Trong tr動畛ng h畛p ph動董ng th畛c c坦 ph叩t sinh ra ngo畉i l畛, n畉u kh担ng
mu畛n b畉t n坦 ngay (nh動 trong v鱈 d畛 tr動畛c) th狸 s畛 d畛ng throws 畛 b叩o
cho ch動董ng tr狸nh bi畉t l ph動董ng th畛c ny s畉 n辿m ra ngo畉i l畛
39
method1() {
try {
invoke method2;
}
catch (Exception ex) {
Process exception;
}
}
method2() throws Exception {
if (an error occurs) {
throw new Exception();
}
}
catch exception throw exception
declare exception
40. 2.4. Ngo畉i l畛
S畛 d畛ng throws: V鱈 d畛
40
public class DelegateExceptionDemo {
public static void main(String args[]){
try {
int num = calculate(9,3);
System.out.println(Lan 1: + num);
num = calculate(9,0);
System.out.println(Lan 2: + num);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
static int calculate(int tu, int mau) throws ArithmeticException {
if (mau== 0)
throw new ArithmeticException("Khong the chia cho 0!");
int num = tu/ mau;
return num;
}
}
42. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Scanner: v鱈 d畛
import java.util.*;
public class Cong2so {
public static void main (String[] args) {
Scanner nhap = new Scanner(System.in);
int x,y;
System.out.print("Nhap so thu nhat: ");
x = nhap.nextInt();
System.out.print("Nhap so thu hai: ");
y = nhap.nextInt();
int tong = x+y;
System.out.println ("The sum is: "+ tong);
}
}
際際滷 42
43. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Random
L畛p Random n畉m trong g坦i java.util
C担ng d畛ng: D湛ng 畛 ph叩t sinh s畛 ng畉u nhi棚n
C叩ch d湛ng:
o T畉o 畛i t動畛ng:
Random rd = new Random();
o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0, n-1]
int a = rd.nextInt(n);
o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0.0, 1.0)
float a = rd.nextFloat();
Vd/ Ph叩t sinh 3 s畛 a,b,c thu畛c [0,50], t狸m s畛 l畛n nh畉t.
43
44. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Math
L畛p Math n畉m trong g坦i java.lang (g坦i m畉c 畛nh)
Ch畛a c叩c ph動董ng th畛c c坦 ch畛c nng t鱈nh to叩n v畛 to叩n h畛c:
o pow: L滴y th畛a
o sqrt: Cn b畉c 2
o abs: Tr畛 tuy畛t 畛i
o
C叩c ph動董ng th畛c trong l畛p Math l nh畛ng ph動董ng th畛c t挑nh (static
methods). V狸 v畉y, ch畛 c畉n g畛i tr畛c ti畉p ph動董ng th畛c th担ng qua t棚n l畛p
m kh担ng c畉n t畉o 畛i t動畛ng
o V鱈 d畛: double value = Math.sqrt(25.0);
44
45. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh
L畛p NumberFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo ki畛u ti畛n t畛 ho畉c
ph畉n trm
L畛p DecimalFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo m畉u 畛nh d畉ng
cho tr動畛c
C畉 2 n畉m trong g坦i java.text
45