前言
本教程旨在幫助初學者系統地掌握Java的基礎知識。我們將從Java的基本語法開始,逐步深入到面向對象編程、異常處理、多線程編程等核心概念。無論你是編程新手,還是希望夯實基礎的開發者,這份指南都將帶你走進Java的世界,打下堅實的編程基礎。
1. 基本語法
- 變量與數據類型:理解不同的數據類型(如 int、double、boolean、char 等),以及如何聲明和初始化變量。
int number = 10;double price = 29.99;boolean isTrue = true;char letter = 'A';
2. 操作符
- 算術操作符:加(
+
)、減(-
)、乘(*
)、除(/
)和取模(%
)。
int sum = 5 + 3;int difference = 5 - 3;int product = 5 * 3;int quotient = 5 / 3;int remainder = 5 % 3;
- 比較操作符:等于(
==
)、不等于(!=
)、大于(>
)、小于(<
)、大于等于(>=
)、小于等于(<=
)。
boolean isEqual = (5 == 3);boolean isNotEqual = (5 != 3);boolean isGreater = (5 > 3);
- 邏輯操作符:與(
&&
)、或(||
)、非(!
)。
boolean andResult = (true && false);boolean orResult = (true || false);boolean notResult = !true;
3. 控制結構
- 條件語句:
if
、else if
、else
和switch
。
if (condition) {// code block} else if (anotherCondition) {// another code block} else {// another code block}switch (expression) {case value1:// code blockbreak;case value2:// code blockbreak;default:// code block}
- 循環結構:
for
、while
和do-while
。
for (int i = 0; i < 10; i++) {// code block}int j = 0;while (j < 10) {// code blockj++;}int k = 0;do {// code blockk++;} while (k < 10);
4. 方法
- 方法定義與調用:如何定義方法,并在程序中調用它們。
public int sum(int a, int b) {return a + b;}public static void main(String[] args) {MyClass myObject = new MyClass();int result = myObject.sum(5, 3);}
5. 面向對象編程(OOP)
- 類與對象:理解類的定義和對象的創建。
public class Car {// attributesString color;int maxSpeed;// methodspublic void display() {System.out.println("Car color: " + color);System.out.println("Max speed: " + maxSpeed);}}
- 構造方法:如何定義和使用構造方法初始化對象。
public class Car {String color;int maxSpeed;// Constructorpublic Car(String color, int maxSpeed) {this.color = color;this.maxSpeed = maxSpeed;}}Car myCar = new Car("Red", 180);
- 封裝:使用修飾符(
private
、public
、protected
)保護類的屬性。
public class Employee {private String name;private double salary;// Getter and Setter methodspublic String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}}
- 繼承:通過
extends
關鍵字實現類的繼承。
public class Animal {void eat() {System.out.println("This animal eats food.");}}public class Dog extends Animal {void bark() {System.out.println("The dog barks");}}Dog myDog = new Dog();myDog.eat();myDog.bark();
- 多態:方法重載和方法重寫。
// Method Overloadingpublic class MathUtils {public int add(int a, int b) {return a + b;}public double add(double a, double b) {return a + b;}}// Method Overridingpublic class Animal {void sound() {System.out.println("This animal makes a sound");}}public class Cat extends Animal {void sound() {System.out.println("The cat meows");}}Animal myCat = new Cat();myCat.sound(); // Output: The cat meows
6. 包和導入
- 包:組織代碼結構。
package com.example.myapp;public class MyApp {// class code}
- 導入:使用其他包的類。
import java.util.Scanner;public class MyApp {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// code}}
7. 文件 I/O
- 基本的文件讀寫:使用
FileReader
、FileWriter
等進行文件操作。
import java.io.FileWriter;import java.io.IOException;public class MyApp {public static void main(String[] args) {try {FileWriter myWriter = new FileWriter("filename.txt");myWriter.write("Hello, world!");myWriter.close();} catch (IOException e) {e.printStackTrace();}}}