Вы находитесь на странице: 1из 23

0

8
/
0
7
/
2
0
1
4

1

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

JAVA BASIC
Nguyn Xun Vinh
nguyenxuanvinh@hcmuaf.edu.vn
CU TRC D LIU
DATA STRUCTURES
[214441]
0
8
/
0
7
/
2
0
1
4

2

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

1. Basic Language Elements
Identifiers
A name in a program is called an identifier
An identifier in java is composed of a sequence of characters
(include: letter, digits or connecting symbols ($, _).
The first character in an identifier cant be a digit.
Identifiers in java are case sensitive.
Keywords
Key words are reserved identifiers.
In java, all keywords are in lower case.
Literals
A literal denotes a constant value of a particular data type
The value a literal represents remains unchanged in the program.
0
8
/
0
7
/
2
0
1
4

3

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

2. Data Types
0
8
/
0
7
/
2
0
1
4

4

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

2.1 Primitive Data Types
Types Length Values Default
value
byte 8-bit -2
8
2
8
1 0
short 16-bit -2
16
2
16
1 0
int 32-bit -2
31
2
31
1 0
long 64-bit -2
63
2
63
1 0
float 32-bit IEEE 754 floating point +/-3.40282347E+38 +/-
1.40239846E-45
0.0f
double 64-bit IEEE 754 floating point +/-1.79769313486231570E
+/-4.9065645841246544E-324
0.0d
boolean 1-bit true, false false
char 16-bit Unicode \u0000 (0) \uffff (65.535)
0
8
/
0
7
/
2
0
1
4

5

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

2.1 Primitive Data Types
0
8
/
0
7
/
2
0
1
4

6

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

2.2 Reference Data Types
Reference to Tham chiu ti mt gi tr hay l tp hp cc gi tr
m bin khai bo.


Cc kiu d liu dn xut:
0
8
/
0
7
/
2
0
1
4

7

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

3. Literal
The new keyword isn't used when initializing a variable of a
primitive type.
A literal is the source code representation of a fixed value.
Literals are represented directly in your code without requiring
computation
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;

0
8
/
0
7
/
2
0
1
4

8

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

4. Variables
Parameters
The variables that are listed as part of a method declaration. Each
parameter must have a unique name and a defined data type.

public void method(int a, double b, boolean c) {
...
}

0
8
/
0
7
/
2
0
1
4

9

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

Instance variables
Every object of the class will have its own copies of these variables,
which are local to object
The values of these variables at any given time constitute the state of
the object
Instance variables exist as long as object they belong to exist
Static variables
Belong only to the class, but not created for only object of the class
All objects of the class share the same copy of this variable
Class variables exist as long as class exist
Local Variables
Variables declared in methods including parameters or blocks are
called Local variables
After execution of the method or block completes local variables
are no longer accessible
4. Variables
0
8
/
0
7
/
2
0
1
4

1
0

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

Instance variables and local variables
Instance variables are declared inside a class but not inside a method
public class Student{
int num; // num is instance variable
public void method(){}
}

Local variables are declared inside a method including method arguments.
public class Student {
public void sum(int a) {
int x = a + 3;
// a , x are local variables</strong>
}
}
0
8
/
0
7
/
2
0
1
4

1
1

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

5. Constant
T kha final ch dn n 1 bin khng th thay i gi tr.
Cc hm v lp cng c th c khai bo final
Hm final khng th vit chng
Lp final khng th l lp con
static final int VAR = 1;
0
8
/
0
7
/
2
0
1
4

1
2

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory
The JVM divided the memory into following sections.
1. Heap: contains Objects (may also contain reference
variables).
2. Stack: contains methods, local variables and reference
variables.
3. Code: contains your bytecode
4. Static: contains Static data/methods.
This division of memory is required for its effective management.

0
8
/
0
7
/
2
0
1
4

1
3

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory
0
8
/
0
7
/
2
0
1
4

1
4

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory Example 1
class A {
B child = new B();
int e;
// more code
}
class B {
int c;
int d;
// more code
}
public static void main(String args[]) {
A parent = new A();
// more code
}
0
8
/
0
7
/
2
0
1
4

1
5

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory: Example 2
public void m1() {
int x = 20;
m2(10);
}

public void m2(int b) {
boolean c;
//more code
m3();
}

public void m3() {
Account ref = new Account();
//more code
}

public class Account {
int p;
int q;
}
Heap
m3 ref
m2 b c
m1 x
Stack
Account
p=0
q=0
[int]
20
0
8
/
0
7
/
2
0
1
4

1
6

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory: Example 3 (Array)
0
8
/
0
7
/
2
0
1
4

1
7

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory: (Exp 4) Primitive vs Reference
Primitive Data Types (byte,short,int,long,float,double,Boolean,chat)
int a = 1;
int b = 1;
int[] arrayInt = new int[2];
arrayInt[0] = a;
arrayInt[1] = b;

Reference Data Type: Student (int sid, String name)
Student s1 = new Student(1, A);
Student s2 = new Student(2, B);
Student s3 = new Student(1, A);
Student[] arrayStudent = new Student[3];
arrayStudent[0] = s1;
arrayStudent[1] = s2;
arrayStudent[2] = s3;

0
8
/
0
7
/
2
0
1
4

1
8

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory (Exp 5) - String Pool
String string1 = "abcd";
String string2 = "abcd";
String pool (String intern pool) is a special storage area in Java
heap. When a string is created and if the string already exists in
the pool, the reference of the existing string will be returned,
instead of creating a new object and returning its reference.
0
8
/
0
7
/
2
0
1
4

1
9

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

6. JVM Memory: (Exp 7) String Literal vs. String Object
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); //String Object
String s5 = new String("Hello"); // String object
0
8
/
0
7
/
2
0
1
4

2
0

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

7. Mutable vs Immutable
0
8
/
0
7
/
2
0
1
4

2
1

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

7. Why String is immutable
Allow String to Cache its Hashcode
The hashcode of string is frequently used in Java. For example, in a
HashMap. Being immutable guarantees that hashcode will always the
same, so that it can be cashed without worrying the changes.That
means, there is no need to calculate hashcode every time it is used.
This is more efficient.
Security
String is widely used as parameter for many java classes, e.g.
network connection, opening files, etc. Were String not immutable, a
connection or file would be changed and lead to serious security
threat.

0
8
/
0
7
/
2
0
1
4

2
2

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

7. Object Immutable & Mutable
class Mutable{
private int value;
public Mutable(int value) {
this.value = value;
}
}

class Immutable {
private final int value;
public Immutable(int value) {
this.value = value;
}
}

0
8
/
0
7
/
2
0
1
4

2
3

/
X
X

M

N
:

C

U

T
R

C

D


L
I

U

G
V
:

N
G
U
Y

N

X
U

N

V
I
N
H

HI P

Вам также может понравиться