java入门
Java 是一种面向对象的高级编程语言,由 Sun Microsystems 于1995年发布,现在由 Oracle Corporation 维护和开发。它以其平台无关性、简洁性和广泛的应用范围而闻名。Java 语言由 James Gosling 和他的团队在 Sun Microsystems 公司开发,最初名为 Oak,后改名为 Java。
Java 通过 Java 虚拟机(JVM)实现了跨平台特性。Java 程序编译成字节码后,可以在任何安装了 JVM 的系统上运行。Java 是一种纯粹的面向对象编程语言,所有代码都以类和对象的形式存在。Java 提供自动垃圾回收机制,帮助开发者管理内存,减少内存泄漏问题。拥有丰富的标准库,提供了大量现成的功能,可以极大地提高开发效率。内置对多线程的支持,可以方便地开发并发应用程序。提供了多层次的安全机制,包括字节码验证、沙盒执行模型等,确保程序的安全性。
- Java SE(Standard Edition):Java 标准版,提供了开发桌面应用和服务器端应用的基础库和工具。
- Java EE(Enterprise Edition):Java 企业版,扩展了 Java SE,提供了开发企业级应用所需的 API 和运行时环境。
- Java ME(Micro Edition):Java 微型版,用于开发嵌入式系统和移动设备应用。
- JavaFX:用于构建富互联网应用的图形和媒体包,主要用于替代 Swing 和 AWT。
安装 Java 开发环境:
- 下载并安装 Java Development Kit (JDK)。
- 安装集成开发环境 (IDE):推荐使用 IntelliJ IDEA、Eclipse 。
- Java 官方文档是最全面的学习资源,涵盖了语言的各个方面。你可以在 Oracle 的 Java SE 文档 上找到详细信息。
1. 语言基础
基本语法:
-
变量和数据类型:
int myNumber = 5; double myDouble = 5.99; char myChar = 'A'; boolean myBool = true; String myString = "Hello";
-
算术操作符:
int sum = 5 + 3; // 加法 int diff = 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 isLess = (5 < 3); // 小于 boolean isGreaterOrEqual = (5 >= 3); // 大于等于 boolean isLessOrEqual = (5 <= 3); // 小于等于
逻辑操作符:
boolean and = (true && false); // 逻辑与 boolean or = (true || false); // 逻辑或 boolean not = !true; // 逻辑非
控制结构:
-
条件语句:
int x = 10; if (x > 0) { System.out.println("x is positive"); } else { System.out.println("x is not positive"); }
-
循环语句:
for (int i = 0; i < 5; i++) { System.out.println("i = " + i); } int j = 0; while (j < 5) { System.out.println("j = " + j); j++; }
2. 面向对象编程 (OOP)
类和对象:
-
定义类:
public class Dog { // 属性 String breed; int age; // 方法 void bark() { System.out.println("Woof!"); } }
-
创建对象:
public class Main { public static void main(String[] args) { Dog myDog = new Dog(); myDog.breed = "Labrador"; myDog.age = 3; myDog.bark(); } }
继承和多态:
-
继承:
public class Animal { void makeSound() { System.out.println("Some sound"); } } public class Dog extends Animal { void makeSound() { System.out.println("Woof"); } }
-
多态:
public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // 输出 "Woof" } }
3. 常用类库
字符串处理:
String s = "Hello, World!";
int length = s.length();
String upperCase = s.toUpperCase();
boolean contains = s.contains("World");
集合框架:
-
列表:
import java.util.ArrayList; ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); for (String fruit : list) { System.out.println(fruit); }
-
映射:
import java.util.HashMap; HashMap<String, Integer> map = new HashMap<>(); map.put("Apple", 1); map.put("Banana", 2); for (String key : map.keySet()) { System.out.println(key + ": " + map.get(key)); }
4. 文件和输入输出
读取文件:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
写入文件:
import java.io.FileWriter;
import java.io.IOException;
public class WriteFile {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, World!");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
5. 异常处理
处理异常:
public class ExceptionExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds!");
} finally {
System.out.println("The try-catch block is finished.");
}
}
}
6. Java 标准类库
日期和时间:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class DateTimeExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Date: " + date);
System.out.println("Time: " + time);
System.out.println("DateTime: " + dateTime);
}
}
7. 多线程编程
创建线程:
public class MyThread extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
实现 Runnable 接口:
public class MyRunnable implements Runnable {
public void run() {
System.out.println("This code is running in a thread");
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
Java 是一门强大且广泛使用的编程语言,适用于各种应用场景,包括桌面应用、Web 应用、企业级应用、移动应用和大数据处理等。通过系统学习 Java 的基本语法、面向对象编程思想和常用类库,你可以为后续的深入学习和开发复杂项目打下坚实基础.