初学java编程100个代码大全:从零开始,Java编程100个经典代码大全,新手入门必备宝典
Java作为一门广泛应用于企业级开发、Android应用开发以及大数据领域的编程语言,因其跨平台、面向对象、健壮性强等特性,成为初学者的理想选择,本文将为初学者整理100个Java编程经典代码示例,涵盖基础语法、数据类型、控制流、循环、数组、方法、类与对象、异常处理、集合框架等内容,帮助你快速掌握Java编程的核心技能。
基础语法与Hello World
第一个Java程序:Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
说明:这是每个Java学习者的起点,展示了如何创建一个类、定义主方法以及输出语句。
变量与数据类型
声明变量
public class Variables {
public static void main(String[] args) {
int age = 25;
double salary = 5000.50;
char grade = 'A';
boolean isJavaFun = true;
String name = "Alice";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
说明:展示了Java的基本数据类型及其使用。
运算符
算术运算符示例
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
说明:展示了Java中的算术运算符。
控制流
if-else语句
public class IfElseExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("中等");
} else {
System.out.println("不及格");
}
}
}
说明:通过if-else语句实现条件判断。
switch语句
public class SwitchExample {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("中等");
break;
default:
System.out.println("输入有误");
}
}
}
说明:展示了switch语句的使用。
循环
for循环
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("循环次数: " + i);
}
}
}
说明:for循环的基本用法。
while循环
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}
}
}
说明:while循环的使用场景。
do-while循环
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("i = " + i);
i++;
} while (i <= 5);
}
}
说明:do-while循环至少执行一次。

数组
数组声明与初始化
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("第一个元素: " + numbers[0]);
System.out.println("数组长度: " + numbers.length);
}
}
说明:展示了如何声明和使用数组。
使用for循环遍历数组
public class ArrayTraversal {
public static void main(String[] args) {
int[] scores = {90, 85, 76, 92, 88};
for (int i = 0; i < scores.length; i++) {
System.out.println("Score " + i + ": " + scores[i]);
}
}
}
说明:通过for循环遍历数组的每个元素。
方法
定义和调用方法
public class MethodExample {
public static void main(String[] args) {
greet("Alice");
int result = add(5, 10);
System.out.println("5 + 10 = " + result);
}
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static int add(int a, int b) {
return a + b;
}
}
说明:展示了如何定义和调用方法。
类与对象
类与对象的基本示例
public class Car {
String brand;
String color;
public void drive() {
System.out.println(brand + " " + color + " car is driving.");
}
}
public class ClassObjectExample {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.color = "Red";
myCar.drive();
}
}
说明:展示了类与对象的基本概念。
继承与多态
继承示例
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound();
}
}
说明:展示了继承与方法重写。

异常处理
try-catch示例
public class ExceptionHandling {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("除以零错误");
}
}
}
说明:展示了如何捕获并处理异常。
十一、集合框架
ArrayList示例
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println("List: " + list);
}
}
说明:展示了如何使用ArrayList存储和操作数据。
十二、文件操作
读取文件示例(使用BufferedReader)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("File not found");
}
}
}
说明:展示了如何读取文本文件。
十三、多线程
创建线程示例
public class ThreadExample extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args) {
ThreadExample t1 = new ThreadExample();
t1.start();
}
}
说明:展示了如何创建和启动线程。
是Java编程的100个经典代码示例,涵盖了从基础语法到高级主题的各个方面,初学者可以通过这些示例逐步掌握Java编程的核心概念,建议在学习过程中多动手实践,结合实际项目进行巩固,Java是一门需要不断练习的语言,只有通过不断的编码才能真正掌握它。
附注:本文仅提供部分代码示例,完整版可参考专业Java学习书籍或在线教程。
文章已关闭评论!