当前位置: 首页 > news >正文

JAVA基础代码示例

1.Hello world程序:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2.变量和数据类型:

public class Variables {
public static void main(String[] args) {
// 基本数据类型
int age = 25; // 整型
double salary = 50000.50; // 双精度浮点型
char grade = 'A'; // 字符型
boolean isJavaFun = true; // 布尔型

// 引用数据类型
String name = "John Doe"; // 字符串

System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: $" + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Java fun? " + isJavaFun);
}
}

3.运算符:

public class Operators {
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));

// 关系运算符
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));

// 逻辑运算符
boolean x = true;
boolean y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
}
}

4.控制流语句:

public class ControlFlow {
public static void main(String[] args) {
// if-else 语句
int number = 10;
if (number > 0) {
System.out.println("Number is positive");
} else if (number < 0) {
System.out.println("Number is negative");
} else {
System.out.println("Number is zero");
}

// switch 语句
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}

// for 循环
System.out.println("For loop:");
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}

// while 循环
System.out.println("While loop:");
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}

// do-while 循环
System.out.println("Do-while loop:");
int j = 1;
do {
System.out.println("Count: " + j);
j++;
} while (j <= 5);
}
}

5.数组:

public class Arrays {
public static void main(String[] args) {
// 声明和初始化数组
int[] numbers = {1, 2, 3, 4, 5};

// 访问数组元素
System.out.println("First element: " + numbers[0]);
System.out.println("Array length: " + numbers.length);

// 遍历数组
System.out.println("Array elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}

// 增强for循环
System.out.println("Enhanced for loop:");
for (int num : numbers) {
System.out.println(num);
}

// 多维数组
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println("Matrix element at [1][2]: " + matrix[1][2]);
}
}

6.方法:

public class Methods {
// 无返回值方法
public static void greet() {
System.out.println("Hello from method!");
}

// 带参数的方法
public static void greetByName(String name) {
System.out.println("Hello, " + name + "!");
}

// 带返回值的方法
public static int add(int a, int b) {
return a + b;
}

// 方法重载
public static double add(double a, double b) {
return a + b;
}

public static void main(String[] args) {
greet();
greetByName("Alice");

int sum1 = add(5, 3);
System.out.println("Sum of integers: " + sum1);

double sum2 = add(5.5, 3.2);
System.out.println("Sum of doubles: " + sum2);
}
}

7.类和对象:

// 定义一个类
class Car {
// 属性(字段)
String brand;
String color;
int year;

// 构造方法
public Car(String brand, String color, int year) {
this.brand = brand;
this.color = color;
this.year = year;
}

// 方法
public void start() {
System.out.println("The " + color + " " + brand + " is starting.");
}

public void stop() {
System.out.println("The " + color + " " + brand + " is stopping.");
}

public void displayInfo() {
System.out.println("Brand: " + brand + ", Color: " + color + ", Year: " + year);
}
}

public class ClassesAndObjects {
public static void main(String[] args) {
// 创建对象
Car myCar = new Car("Toyota", "Red", 2020);

// 调用对象方法
myCar.displayInfo();
myCar.start();
myCar.stop();

// 创建另一个对象
Car yourCar = new Car("Honda", "Blue", 2018);
yourCar.displayInfo();
}
}

8.继承:

// 父类
class Animal {
String name;

public Animal(String name) {
this.name = name;
}

public void eat() {
System.out.println(name + " is eating.");
}

public void sleep() {
System.out.println(name + " is sleeping.");
}
}

// 子类
class Dog extends Animal {
String breed;

public Dog(String name, String breed) {
super(name); // 调用父类构造方法
this.breed = breed;
}

public void bark() {
System.out.println(name + " is barking.");
}

// 方法重写
@Override
public void eat() {
System.out.println(name + " the " + breed + " is eating dog food.");
}
}

public class Inheritance {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Golden Retriever");
myDog.eat();    // 调用重写的方法
myDog.sleep();  // 调用继承的方法
myDog.bark();   // 调用子类特有的方法
}
}

 

http://www.dtcms.com/a/344787.html

相关文章:

  • SpringBoot -- 集成Spring Security (二)
  • LightGBM时序预测详解:从原理到 PSO 参数优化
  • 如何理解面向过程和面向对象,举例说明一下?
  • [docker/大数据]Spark快速入门
  • 【实时Linux实战系列】实时系统中的预测性维护策略
  • 能源行业合同管理难点有哪些?企业该如何应对?
  • FIFO核心原理与机制
  • QGIS 绿色版修正
  • 基于vtkImageViewer2的MPR(二):改进
  • JavaScript 操作 DOM
  • 图论——Floyd算法
  • 四十一、【高级特性篇】API 文档驱动:OpenAPI/Swagger 一键导入测试用例
  • 上市公司能源消耗数据库
  • 【python】os.makedirs和with open
  • 密码管理中硬编码密码
  • (论文速读)并行自回归视觉生成
  • 硬件驱动---linux内核驱动 启动
  • 【LeetCode】21. 合并两个有序链表
  • 开发二手车小程序时,如何确保信息的真实性和可靠性?
  • Prometheus+Grafana监控redis
  • 【连接器专题】连接器接触界面的理解
  • Elasticsearch Rails 集成(elasticsearch-model / ActiveRecord)
  • 高速互联技术——NVLink
  • SpringBoot3集成Oauth2.1——8自定义认证模式(密码模式)
  • 第九届86358贾家庄短片周在山西汾阳贾家庄举办
  • 将博客网站完整迁移至本地虚拟机
  • 爬虫基础学习-授权认证,cookie认证,异常处理
  • 最短路径问题(图论)
  • 中国SM系列密码算法的入门教程
  • 网络实践——Socket编程UDP