安装 Java 开发环境
- 下载 JDK:从 Oracle 官方网站 或 OpenJDK 下载并安装 Java Development Kit (JDK)。
- 配置环境变量:将 JDK 的
bin
目录添加到系统的PATH
环境变量中。
编写第一个 Java 程序
使用文本编辑器(如 VSCode、Notepad++)或 IDE(如 IntelliJ IDEA、Eclipse)编写 Java 程序。
HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
编译和运行 Java 程序
1. 编译 Java 程序
打开命令行窗口,进入到包含 HelloWorld.java
文件的目录,运行以下命令:
javac HelloWorld.java
此命令会生成一个 HelloWorld.class
文件,这是 Java 字节码文件。
2. 运行 Java 程序
继续在命令行窗口中运行以下命令:
java HelloWorld
输出:
Hello, World!
基础语法
1. 变量和数据类型
public class VariablesExample {
public static void main(String[] args) {
int myNum = 5; // 整数类型
double myDouble = 5.99; // 浮点数类型
char myLetter = 'D'; // 字符类型
boolean myBool = true; // 布尔类型
String myText = "Hello"; // 字符串类型
System.out.println(myNum);
System.out.println(myDouble);
System.out.println(myLetter);
System.out.println(myBool);
System.out.println(myText);
}
}
2. 控制结构
- 条件语句
public class ConditionExample {
public static void main(String[] args) {
int x = 20;
if (x > 10) {
System.out.println("x is greater than 10");
} else {
System.out.println("x is less than or equal to 10");
}
}
}
- 循环语句
public class LoopExample {
public static void main(String[] args) {
// for 循环
for (int i = 0; i < 5; i++) {
System.out.println("i is: " + i);
}
// while 循环
int j = 0;
while (j < 5) {
System.out.println("j is: " + j);
j++;
}
// do-while 循环
int k = 0;
do {
System.out.println("k is: " + k);
k++;
} while (k < 5);
}
}
3. 数组
public class ArrayExample {
public static void main(String[] args) {
int[] myNumbers = {10, 20, 30, 40};
for (int i = 0; i < myNumbers.length; i++) {
System.out.println(myNumbers[i]);
}
}
}
4. 方法
public class MethodExample {
public static void main(String[] args) {
int result = addNumbers(5, 3);
System.out.println("Result is: " + result);
}
public static int addNumbers(int a, int b) {
return a + b;
}
}
面向对象编程 (OOP)
1. 类和对象
public class Dog {
String name;
int age;
// 构造方法
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public void bark() {
System.out.println(name + " says: Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", 3);
myDog.bark();
}
}
2. 继承
// 父类
public class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
// 子类
public class Dog extends Animal {
public void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // 调用继承自 Animal 类的方法
myDog.bark(); // 调用 Dog 类的方法
}
}
3. 多态
public class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
public class Cat extends Animal {
public void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.makeSound();
myDog.makeSound();
myCat.makeSound();
}
}
通过上述基础用法,你可以开始编写和运行简单的 Java 程序。如果需要更详细的教程或遇到具体问题,欢迎继续提问。