java综合语法
1311字约4分钟
2024-11-16
当然,以下是一个综合的 Java 基础和高级语法教程,涵盖了从基本概念到高级特性的广泛主题。这个指南可以帮助您从初学者逐步过渡到更高级的 Java 编程。
Java 综合语法教程
一、基础语法
1. Java 基本概念
程序结构:Java 程序由类组成,每个类包含方法,
main
方法是程序的入口。示例:Hello World
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
2. 数据类型
原始数据类型:
int
,double
,char
,boolean
等。变量声明和初始化
int age = 25; double salary = 50000.50; char grade = 'A'; boolean isEmployed = true;
3. 运算符和控制结构
运算符:
+
,-
,*
,/
,%
,==
,!=
,&&
,||
等。条件语句
if (age > 18) { System.out.println("Adult"); } else { System.out.println("Minor"); }
循环语句
for (int i = 0; i < 5; i++) { System.out.println(i); }
二、面向对象编程
1. 类和对象
定义和使用类
public class Car { String color; int year; void drive() { System.out.println("Car is driving"); } } Car myCar = new Car(); myCar.color = "Red"; myCar.drive();
2. 继承和多态
继承
public class Vehicle { void start() { System.out.println("Vehicle started"); } } public class Car extends Vehicle { void drive() { System.out.println("Car is driving"); } }
多态
public class Animal { void sound() { System.out.println("Animal sound"); } } public class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } }
三、异常处理
try-catch 语句
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }
throw 和 throws
public void checkAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be at least 18"); } }
四、高级语法
1. 泛型
泛型类
public class Box<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; } }
泛型方法
public class Util { public static <T> void printArray(T[] array) { for (T element : array) { System.out.println(element); } } }
2. 多线程
创建线程
public class MyThread extends Thread { @Override public void run() { System.out.println("Thread is running"); } } MyThread thread = new MyThread(); thread.start();
同步
public synchronized void synchronizedMethod() { // 同步代码 }
3. 反射
获取类信息
Class<?> clazz = Class.forName("java.util.ArrayList"); Method[] methods = clazz.getMethods();
动态调用方法
Method method = clazz.getMethod("add", Object.class); List<String> list = new ArrayList<>(); method.invoke(list, "Hello");
4. 输入输出(I/O)
文件读取
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
文件写入
try (BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"))) { writer.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); }
5. Java 8 新特性
Lambda 表达式
List<String> list = Arrays.asList("a", "b", "c"); list.forEach(item -> System.out.println(item));
Stream API
list.stream() .filter(s -> s.startsWith("a")) .forEach(System.out::println);
Optional 类
Optional<String> optional = Optional.ofNullable(getValue()); optional.ifPresent(System.out::println);
通过这个综合教程,您可以从 Java 的基础语法入门,逐步掌握更复杂的编程概念和技术。Java 的强大之处在于其面向对象的特性、丰富的类库和跨平台能力。根据您的具体需求,您可以进一步探索 Java 的其他高级特性和优化技巧。当然,以下是一个综合的 Java 基础和高级语法教程,涵盖了从基本概念到高级特性的广泛主题。这个指南可以帮助您从初学者逐步过渡到更高级的 Java 编程。
Java 综合语法教程
一、基础语法
1. Java 基本概念
程序结构:Java 程序由类组成,每个类包含方法,
main
方法是程序的入口。示例:Hello World
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
2. 数据类型
原始数据类型:
int
,double
,char
,boolean
等。变量声明和初始化
int age = 25; double salary = 50000.50; char grade = 'A'; boolean isEmployed = true;
3. 运算符和控制结构
运算符:
+
,-
,*
,/
,%
,==
,!=
,&&
,||
等。条件语句
if (age > 18) { System.out.println("Adult"); } else { System.out.println("Minor"); }
循环语句
for (int i = 0; i < 5; i++) { System.out.println(i); }
二、面向对象编程
1. 类和对象
定义和使用类
public class Car { String color; int year; void drive() { System.out.println("Car is driving"); } } Car myCar = new Car(); myCar.color = "Red"; myCar.drive();
2. 继承和多态
继承
public class Vehicle { void start() { System.out.println("Vehicle started"); } } public class Car extends Vehicle { void drive() { System.out.println("Car is driving"); } }
多态
public class Animal { void sound() { System.out.println("Animal sound"); } } public class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } }
三、异常处理
try-catch 语句
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }
throw 和 throws
public void checkAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be at least 18"); } }
四、高级语法
1. 泛型
泛型类
public class Box<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; } }
泛型方法
public class Util { public static <T> void printArray(T[] array) { for (T element : array) { System.out.println(element); } } }
2. 多线程
创建线程
public class MyThread extends Thread { @Override public void run() { System.out.println("Thread is running"); } } MyThread thread = new MyThread(); thread.start();
同步
public synchronized void synchronizedMethod() { // 同步代码 }
3. 反射
获取类信息
Class<?> clazz = Class.forName("java.util.ArrayList"); Method[] methods = clazz.getMethods();
动态调用方法
Method method = clazz.getMethod("add", Object.class); List<String> list = new ArrayList<>(); method.invoke(list, "Hello");
4. 输入输出(I/O)
文件读取
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
文件写入
try (BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"))) { writer.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); }
5. Java 8 新特性
Lambda 表达式
List<String> list = Arrays.asList("a", "b", "c"); list.forEach(item -> System.out.println(item));
Stream API
list.stream() .filter(s -> s.startsWith("a")) .forEach(System.out::println);
Optional 类
Optional<String> optional = Optional.ofNullable(getValue()); optional.ifPresent(System.out::println);
通过这个综合教程,您可以从 Java 的基础语法入门,逐步掌握更复杂的编程概念和技术。Java 的强大之处在于其面向对象的特性、丰富的类库和跨平台能力。根据您的具体需求,您可以进一步探索 Java 的其他高级特性和优化技巧。