[LTM] Chương 3.1 : Biến, hằng, toán tử, kiểu dữ liệu, Cấu trúc điều khiển

Người đăng: share-nhungdieuhay on Thứ Hai, 24 tháng 3, 2014

Basics of the Java Language 

Keywords

Comment: 3 types
/* Here is comment which 
   extends across two lines  */
/* * This comment has  especial meaning for the  javadoc unitily.It will be part of documenttation     
        automaticlly generated By the javadoc program 
     */ 
// this comment extends to the end of this line of text 
Command line and block
// A program to display the message
// "Hello World!" on standard output
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
} // end of class HelloWorld
Data Type
  • Xác định loại dữ liệu được lưu trữ trong biến
  • Xác định những tác vụ có thể thực hiện trên dữ liệu
  • Có 2 loại kiểu dữ liệu trong Java:
  1. Primitive data types (kiểu cơ bản/nguyên thủy)
  2. Reference data types (kiểu tham chiếu)

Các kiểu dữ liệu cơ bản( nguyên thủy)


Các kiểu dữ liệu tham chiếu
Một biến thuộc kiểu dữ liệu tham chiếu chỉ chứa tham khảo đến giá trị thực sự được biểu diễn bởi biến đó.

Hằng ký tự
Dãy ký tự đặc biệt dùng để biểu diễn các ký tự không thể nhập trực tiếp vào một chuỗi.

2. Variables(biến )
Variables must be declared (khai báo) before using,
Syntax(cú pháp)
datatype identifier [=value][, identifier[=value]...]; 
Three components of a declaration are:
  •  Data type
  •  Name
  •  Initial value to be assigned (optional)
Ex:
int numberOfStudents; 
String name; 
int x=10; 
boolean isFinished; 
char firstInitial, middleInitial, lastInitial;

Example

Scope variable(biến cục bộ)
Biến cục bộ có thời gian tồn tại giới hạn và quan hệ chỉ trong phần nhỏ của mã. 

Variables
JAVA có 4 loại biến : 
  1. Biến thành phần: là các thành phần của lớp và được khởi tạo giá trị khi một đối tượng của lớp được tạo ra
  2. Biến tĩnh: là các thành viên của lớp, đại diện cho cả lớp
  3. Biến tham chiếu: được sử dụng để xử lý các đối tượng. 
  4. Biến cục bộ: được khai báo trong các phương thức và các khối, khai báo trước khi dùng
Khởi tạo giá trị
  1. Giá trị mặc định
  2. Chuyển đổi kiểu
Chuyển đổi kiểu dữ liệu: 3 dạng 
 1. Dữ liệu cơ bản 
Cú pháp: (NewType) Value; 
Ex: float f= (float) 100.15D (double->float)
2. Các đối tượng: thành 1 đối tượng của một lớp khác. 
Với điều kiện các đối tượng muốn đổi kiểu phải thuộc các lớp thừa kế nhau.
Cú pháp : (NewClass) Object  ; 
3. Dữ liệu cơ bản sang đối tượng và ngược lại:
  • Trong gói java.lang có sẵn những lớp đặc biệt tương ứng với từng kiểu nguyên thủy: lớp Integer, Float,..
  • Tạo đối tượng, ex: int Intobj = new Integer (32); 
  • Khi muốn lấy lại giá trị ban đầu ta dùng phương thức có sẵn trong lớp tương đương. Như intValue() cho kiểu int... 
Ép kiểu
Một kiểu dữ liệu chuyển sang một kiểu dữ liệu khác

Hai loại ép kiểu
  1. Implicit casting (ngầm định)
  2. Explicit casting (tường minh)

Ép kiểu ngầm định: 
Chuyển kiểu tự động

Điều kiện
  • Kiểu đích phải lớn hơn kiểu nguồn
  • Mở rộng kiểu 
  • Các kiểu phải tương thích với nhau
int a= 100;
double b  = a + 2.5f;

Ép kiểu tường minh
  • Chuyển kiểu có độ chính xác cao hơn sang kiểu có độ chính xác thấp hơn 
  • Thu hẹp kiểu
Ex, float sang int
float a=21.2345f;
int b  = (int)a + 5;

3. Operators 

  • Arithmetic Operators
  • Assignment operators
  • Relational Operators and Conditional Operators
  • Logical Operators

Toán tử toán học

Toán tử một ngôi

Toán tử quan hệ
  • Kiểm tra mối quan hệ giữa hai toán hạng.
  • Luôn trả về một giá trị luận lý (true hay false)
Toán tử điều kiện
“&&”, “||”  : làm việc trên hai toán hạng luận lý

“ ? : ” : toán tử điều kiện chấp nhận 3 toán hạng

Toán tử trên bit
 Làm việc trên dạng biểu diễn nhị phân của dữ liệu

Độ ưu tiên toán tử

Sự kết hợp của toán tử


4. Classes in Java 


Class declaration Syntax


Lớp cơ bản trong Java

Input/ Output

Input: System.in  (ko cho nhập “ “)
Scanner nhap = new Scanner(System.in);
int x = nhap.nextInt();
String e= nhap.next();
Input: Luồng ký tự: BufferedReader (cho “ ”)
BufferedReader in =
new BufferedReader (new InputStreamReader (System.in));
String name = in.readLine ();
int hours = Integer.parseInt (in.readLine());
Double rate = Double.parseDouble (in.readLine());
Output: System.out 
System.out.println (“Hello”);
Express (điều kiện)
Có các loại biểu thức như biểu thức logic, biểu thức số học, biểu thức gán 
  • Ví dụ : a <= 10; 
  • Biểu thức gán: Variable1= Variable2=...=Value; 
  • Biểu thức điều kiện :  
Expression ? Expression_true : Expression_false; 

5. Control Flow

  • All application development environments provide a decision making process called control flow statements that direct the application execution.
  • Flow control enables a developer to create an application that can examine the existing conditions, and decide a suitable course of action. 
  • Loops or iteration are an important programming construct that can be used to repeatedly execute a set of actions. 
  • Jump statements allow the program to execute in a non-linear fashion. 
Control Flow Structures in Java

1. Decision-making
  • if-else statement
  • switch-case statement
2. Loops
  • while loop
  • do-while loop
  • for loop
if-else statement
if (condition)
{
action1;
}
else
{
action2;
}

Example: checkNum

If

switch – case statement
switch(integer expression) { 
    case integer expression: 
         statement(s) 
  break; 
    ... 
    default: 
         statement(s) 
         break; 
Expression : Biểu thức điều kiện phụ thuộc vào kiểu dữ liệu cơ bản (byte, int ...)
switch – case statement
Example

  

while Loop

  • while loops are used for situations when a loop has to be executed as long as certain condition is True. 
  • The number of times a loop is to be executed is not pre-determined, but depends on the condition. 
  • The syntax is:
while (condition)
{
action statements;
.
.
}

Example

do – while Loop

  • The do-while loop executes certain statements till the specified condition is True. 
  • These loops are similar to the while loops, except that a do-while loop executes at least once, even if the specified condition is False. The syntax is:
do
{
action statements;
.
.
} while (condition);
Example

for Loop
Syntax:
for (initialization statements;  condition; increment / decrement statements)
{
action statements;
.
.
}
Example

Jump Statements (nhảy)
Three jump statements are:
  1. break
  2. continue
  3. return
The three uses of break statements are: 
  1. It terminates a statement sequence in a switch statement.
  2. It can be used to exit a loop.
  3. It is another form of goto.
Example

{ 0 nhận xét... read them below or add one }

Đăng nhận xét