» The variables that are listed as part of a method declaration.. » Belong only to the class, but not created for only object of the class» All objects of the class share the same copy o
Trang 1[214441]
Trang 2 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 can’t 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
Trang 4char 16-bit Unicode \u0000 (0) \uffff (65.535)
Trang 6• Reference to Tham chiếu tới một giá trị hay là tập hợp các giá trị
mà biến khai báo
• Các kiểu dữ liệu dẫn xuất:
Trang 7 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;
int i = 100000;
Trang 8» 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) {
}
Trang 9» 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
Trang 10 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>
}
}
Trang 11• Từ khóa final chỉ dẫn đến 1 biến không thể thay đổi giá trị.
• Các hàm và lớp cũng có thể được khai báo final
– Hàm final không thể viết chồng
– Lớp final không thể là lớp con
1;
Trang 12The 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
Trang 15Account
p=0
q=0 [int]
20
Trang 17 Primitive Data Types (byte,short,int,long,float,double,Boolean,chat)
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;
Trang 18String string1 = "abcd";
String string2 = "abcd";
String pool (String intern pool) is a special storage area in Java
Trang 19String 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
Trang 21 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
Trang 22private int value ;
public Mutable(int value) {
this value = value;
}
}
class Immutable {
private final int value ;
public Immutable(int value) {
this value = value;
}
}