Java起源于1995年,由Sun Microsystems开发推出。其设计目标是运行于跨平台的环境中,因此Java程序可以在任何支持Java的平台上运行,无需重新编译。Java以其简洁、安全、可靠和跨平台性等特点,成为全球范围内非常流行的一种编程语言。
主流应用领域
Java在Web开发、桌面应用开发、移动应用开发(尤其是Android应用开发)、大数据处理、人工智能等领域广泛应用。从网站后台到大型企业系统,从移动应用到游戏开发,Java都有其独特的应用价值。
开发工具与环境
为了方便开发者进行Java编程,市面上提供了多种IDE(集成开发环境)和工具包。其中,Eclipse和IntelliJ IDEA是广受欢迎的两种开发工具。这些工具提供了代码编辑、调试、版本控制、插件支持等功能,极大地提升了开发效率。
环境搭建
-
下载与安装Java JDK:首先,访问Oracle官方网站下载Java Development Kit(JDK)。确保下载与操作系统(Windows、macOS、Linux)对应的版本。安装过程中,选择添加到PATH环境变量选项。
# Windows Download the JDK from the official Oracle website. Ensure you select the version compatible with your operating system. During installation, opt for adding the JDK to the PATH environment variable. # macOS Download the JDK from the official Oracle website. Select the macOS version and ensure you add the JDK to the PATH environment during installation. # Linux Download the JDK from the official Oracle website. Choose the appropriate Linux version and add the JDK to the PATH environment during installation.
-
配置环境变量:安装完成后,在系统环境变量中新增或编辑“Path”变量,确保其中包含了JDK的安装目录下的bin目录。
- 验证Java版本:打开命令行工具(如Windows的cmd或macOS的Terminal),输入
java -version
或javac -version
命令,查看已安装的Java版本。
变量与数据类型
Java的基本数据类型包括:byte、short、int、long、float、double、char 和 boolean。变量的声明遵循以下格式:
数据类型 变量名;
例如:
int age;
double price;
运算符与表达式
Java支持基本算术运算符(如+、-、*、/、%)、比较运算符(如==、!=、>、<、>=、<=)以及逻辑运算符(如&&、||、!)。
int a = 10;
int b = 20;
int sum = a + b; // 加法
int diff = a - b; // 减法
int product = a * b; // 乘法
int quotient = a / b; // 除法
int remainder = a % b; // 取模
控制结构
控制结构用于控制程序执行流程,包括条件语句(如if
、else
)和循环语句(如for
、while
)。
int num = 5;
if (num > 0) {
System.out.println("Number is positive.");
} else {
System.out.println("Number is not positive.");
}
int i = 1;
while (i <= 5) {
System.out.println("Loop iteration: " + i);
i++;
}
面向对象编程(OOP)原理
封装、继承、多态
面向对象编程的核心概念包括封装、继承和多态。
-
封装:将数据和操作数据的方法绑在一起,形成类。例如:
public class Student { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
-
继承:子类可以继承父类的属性和方法,实现代码复用。例如:
public class Teacher extends Student { private String subject; public void setSubject(String subject) { this.subject = subject; } public String getSubject() { return subject; } }
- 多态:通过接口或抽象类实现不同对象可以以统一的方式被处理。例如使用
Animal
接口或Shape
抽象类定义不同类型的动物或形状。
Java类库(Java API)提供了丰富的类和接口,包括基础的数据结构、集合框架、输入输出流、网络编程、线程管理等。
Java集合框架
集合框架是Java类库中大量用于存储和操作数据的类。其中包括:
- List:存储数据有序集合(如ArrayList、LinkedList)。
- Set:无序、不重复元素的集合(如HashSet、TreeSet)。
- Map:键值对的映射(如HashMap、TreeMap)。
import java.util.ArrayList;
import java.util.List;
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits);
线程与并发编程
Java提供了Thread
类和Runnable
接口来处理多线程,以及synchronized
关键字和ReentrantLock
类用于线程同步和互斥。
public class Counter implements Runnable {
private int count = 0;
public synchronized void increment() {
count++;
}
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
increment(); // 线程安全操作
}
}
}
public static void main(String[] args) {
Counter counter = new Counter();
Thread thread1 = new Thread(counter);
Thread thread2 = new Thread(counter);
thread1.start();
thread2.start();
}
实践项目:构建一个简单的Java应用
项目概述
建立一个简单的待办事项应用,用户可以输入任务、修改任务、删除任务以及查看所有任务列表。
项目代码
任务类(Task.java)
public class Task {
private String description;
private boolean isComplete;
public Task(String description) {
this.description = description;
this.isComplete = false;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
}
主应用类(TodoListApp.java)
import java.util.ArrayList;
import java.util.List;
public class TodoListApp {
private List<Task> tasks;
public TodoListApp() {
this.tasks = new ArrayList<>();
}
public void addTask(String description) {
Task task = new Task(description);
tasks.add(task);
}
public void removeTask(int index) {
tasks.remove(index);
}
public void completeTask(int index) {
if (index >= 0 && index < tasks.size()) {
Task task = tasks.get(index);
task.setComplete(true);
}
}
public void printTasks() {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + (tasks.get(i).isComplete() ? "[X] " : "[ ] ") + tasks.get(i).getDescription());
}
}
public static void main(String[] args) {
TodoListApp app = new TodoListApp();
app.addTask("Read a book");
app.addTask("Prepare lunch");
app.printTasks();
app.completeTask(0);
app.printTasks();
app.removeTask(1);
app.printTasks();
}
}
共同學習,寫下你的評論
評論加載中...
作者其他優質文章