java编程代码大全
java编程代码大全详细介绍
以下是Java编程不同方面的代码示例:
一、基础语法
1. Hello World程序
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
这是Java程序的入门示例, main 方法是程序的入口点, System.out.println 用于在控制台输出字符串。
2. 变量与数据类型
public class VariableExample {
public static void main(String[] args) {
// 整数类型
int age = 20;
System.out.println("年龄: " + age);
// 浮点数类型
double height = 1.75;
System.out.println("身高: " + height);
// 字符类型
char gender = 'M';
System.out.println("性别: " + gender);
// 布尔类型
boolean isStudent = true;
System.out.println("是否是学生: " + isStudent);
}
}
展示了如何声明和使用不同的数据类型,包括 int (整数)、 double (双精度浮点数)、 char (字符)和 bool (布尔值)。
3. 运算符
public class OperatorExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
// 算术运算符
System.out.println("加法: " + (a + b));
System.out.println("减法: " + (a - b));
System.out.println("乘法: " + (a * b));
System.out.println("除法: " + (a / b));
System.out.println("取余: " + (a % b));
// 比较运算符
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a == b: " + (a == b));
// 逻辑运算符
boolean condition1 = true;
boolean condition2 = false;
System.out.println("与运算: " + (condition1 && condition2));
System.out.println("或运算: " + (condition1 || condition2));
System.out.println("非运算: " + (!condition1));
}
}
代码中包含了算术运算符(加、减、乘、除、取余)、比较运算符(大于、小于、等于)和逻辑运算符(与、或、非)的使用示例。
二、控制结构
1. if - else语句
public class IfElseExample {
public static void main(String[] args) {
int score = 75;
if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}
根据 score 变量的值判断是否及格。
2. switch语句
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
default:
System.out.println("其他日期");
}
}
}
根据 day 的值输出对应的星期几。
3. for循环
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("当前数字: " + i);
}
}
}
用于遍历从1到5的数字,并输出每个数字。
4. while循环
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("当前数字: " + i);
i++;
}
}
}
和 for 循环类似,也是遍历从1到5的数字,不过是通过 while 条件来控制循环。
三、数组
1. 一维数组的定义和遍历
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
定义了一个整数数组,并通过 for 循环遍历数组元素。
2. 二维数组的定义和遍历
public class TwoDArrayExample {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
展示了二维数组的定义和嵌套 for 循环遍历二维数组的元素。
四、面向对象编程
1. 类和对象的定义
class Person {
// 成员变量
String name;
int age;
// 成员方法
void speak() {
System.out.println("我叫" + name + ", 我" + age + "岁。");
}
}
public class ClassObjectExample {
public static void main(String[] args) {
Person person = new Person();
person.name = "张三";
person.age = 25;
person.speak();
}
}
定义了 Person 类,包含 name 和 age 两个成员变量以及 speak 方法,在 main 方法中创建了 Person 类的对象并调用了 speak 方法。
2. 继承
class Animal {
void eat() {
System.out.println("动物在吃东西。");
}
}
class Dog extends Animal {
void bark() {
System.out.println("狗在叫。");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Dog 类继承自 Animal 类,继承了 eat 方法,并定义了自己的 bark 方法。
3. 多态
class Shape {
void draw() {
System.out.println("绘制形状。");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("绘制圆形。");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("绘制正方形。");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw();
shape2.draw();
}
}
通过多态, Shape 类型的引用可以指向 Circle 或 Square 类的对象,并且根据对象的实际类型调用相应的 draw 方法。