Biến là nơi lưu trữ dữ liệu trong bộ nhớ.
Mỗi biến có tên, kiểu dữ liệu và giá trị.
Cú pháp khai báo:
kiểu_dữ_liệu tên_biến = giá_trị;
Ví dụ:
int tuoi = 20;
double diemTB = 8.5;
String hoTen = "Nguyen Van A";
boolean laSinhVien = true;
Chia thành 2 nhóm chính:
byte (1 byte)short (2 byte)int (4 byte)long (8 byte)float (4 byte, số thực)double (8 byte, số thực chính xác cao)char (ký tự, 2 byte, Unicode)boolean (chỉ true hoặc false)Ví dụ:
public class DemoKieuDuLieu {
public static void main(String[] args) {
int a = 10; // số nguyên
double b = 5.75; // số thực
char c = 'A'; // ký tự
boolean d = true; // đúng/sai
String name = "Java"; // chuỗi
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("name = " + name);
}
}
Toán tử số học: + - * / %
→ Dùng cho phép tính cơ bản.
Toán tử gán: = += -= *= /= %=
→ Dùng để gán giá trị.
Toán tử so sánh: == != > < >= <=
→ Kết quả trả về true/false.
Toán tử logic: && || !
→ Kết hợp nhiều điều kiện.
Toán tử tăng/giảm: ++ --
→ Tăng hoặc giảm giá trị biến.
Toán tử điều kiện (ternary): ?:
→ Cú pháp: biểu_thức ? giá_trị_đúng : giá_trị_sai
Ví dụ:
public class DemoToanTu {
public static void main(String[] args) {
int x = 10, y = 3;
// Toán tử số học
System.out.println("x + y = " + (x + y));
System.out.println("x % y = " + (x % y));
// Toán tử gán
x += 5; // x = x + 5
System.out.println("x sau khi += 5: " + x);
// Toán tử so sánh
System.out.println("x > y ? " + (x > y));
// Toán tử logic
boolean check = (x > 5 && y < 5);
System.out.println("x > 5 && y < 5: " + check);
// Toán tử điều kiện
String result = (x % 2 == 0) ? "chẵn" : "lẻ";
System.out.println("x là số " + result);
}
}
import java.util.Scanner;
public class Bai1_HinhChuNhat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập chiều dài: ");
double dai = sc.nextDouble();
System.out.print("Nhập chiều rộng: ");
double rong = sc.nextDouble();
double chuVi = (dai + rong) * 2;
double dienTich = dai * rong;
System.out.println("Chu vi = " + chuVi);
System.out.println("Diện tích = " + dienTich);
sc.close();
}
}
import java.util.Scanner;
public class Bai2_ChanLe {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập một số nguyên: ");
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println(n + " là số chẵn");
} else {
System.out.println(n + " là số lẻ");
}
sc.close();
}
}
import java.util.Scanner;
public class Bai3_SoLonNhat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập số thứ nhất: ");
int a = sc.nextInt();
System.out.print("Nhập số thứ hai: ");
int b = sc.nextInt();
System.out.print("Nhập số thứ ba: ");
int c = sc.nextInt();
int max = a;
if (b > max) max = b;
if (c > max) max = c;
System.out.println("Số lớn nhất là: " + max);
sc.close();
}
}
import java.util.Scanner;
public class Bai4_XepLoai {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập điểm Toán: ");
double toan = sc.nextDouble();
System.out.print("Nhập điểm Lý: ");
double ly = sc.nextDouble();
System.out.print("Nhập điểm Hóa: ");
double hoa = sc.nextDouble();
double dtb = (toan + ly + hoa) / 3;
System.out.println("Điểm trung bình = " + dtb);
if (dtb >= 8) {
System.out.println("Xếp loại: Giỏi");
} else if (dtb >= 6.5) {
System.out.println("Xếp loại: Khá");
} else if (dtb >= 5) {
System.out.println("Xếp loại: Trung bình");
} else {
System.out.println("Xếp loại: Yếu");
}
sc.close();
}
}
import java.util.Scanner;
public class Bai5_PTBac2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập a: ");
double a = sc.nextDouble();
System.out.print("Nhập b: ");
double b = sc.nextDouble();
System.out.print("Nhập c: ");
double c = sc.nextDouble();
if (a == 0) {
if (b == 0) {
if (c == 0) {
System.out.println("Phương trình vô số nghiệm.");
} else {
System.out.println("Phương trình vô nghiệm.");
}
} else {
double x = -c / b;
System.out.println("Phương trình có nghiệm: x = " + x);
}
} else {
double delta = b * b - 4 * a * c;
if (delta < 0) {
System.out.println("Phương trình vô nghiệm.");
} else if (delta == 0) {
double x = -b / (2 * a);
System.out.println("Phương trình có nghiệm kép: x = " + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("Phương trình có 2 nghiệm phân biệt:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
sc.close();
}
}