java语言基础

一、Java 特点

  • 面向对象:支持封装、继承、多态
  • 平台无关性:一次编写,到处运行(JVM)
  • 简单易学:语法类似 C/C++,但去除了复杂特性
  • 安全稳定:自动内存管理,异常处理机制
  • 多线程支持:内置并发编程支持

二、基本语法结构

1. 第一个 Java 程序

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

2. 注释

java 复制代码
// 单行注释

/*
   多行注释
   可以跨越多行
*/

/**
 * 文档注释(用于生成 API 文档)
 * @author 作者
 * @version 版本
 */

三、数据类型

1. 基本数据类型(8种)

类型 大小 范围 默认值
byte 1字节 -128 ~ 127 0
short 2字节 -32768 ~ 32767 0
int 4字节 -2³¹ ~ 2³¹-1 0
long 8字节 -2⁶³ ~ 2⁶³-1 0L
float 4字节 单精度浮点 0.0f
double 8字节 双精度浮点 0.0d
char 2字节 Unicode字符 '\u0000'
boolean 1位 true/false false

2. 引用数据类型

  • 类(Class)
  • 接口(Interface)
  • 数组(Array)
  • 字符串(String)

四、变量和常量

java 复制代码
// 变量声明和初始化
int age = 25;
double price = 99.99;
char grade = 'A';
String name = "张三";

// 常量(使用 final 关键字)
final double PI = 3.14159;
final int MAX_SIZE = 100;

五、运算符

1. 算术运算符

java 复制代码
int a = 10, b = 3;
a + b;  // 加:13
a - b;  // 减:7
a * b;  // 乘:30
a / b;  // 除:3
a % b;  // 取模:1

2. 关系运算符

java 复制代码
>  <  >=  <=  ==  !=

3. 逻辑运算符

java 复制代码
&&  ||  !  // 与、或、非

4. 赋值运算符

java 复制代码
=  +=  -=  *=  /=  %=

5. 三元运算符

java 复制代码
int max = (a > b) ? a : b;

六、流程控制

1. 条件语句

java 复制代码
// if-else
if (score >= 90) {
    System.out.println("优秀");
} else if (score >= 60) {
    System.out.println("及格");
} else {
    System.out.println("不及格");
}

// switch
switch (day) {
    case 1: System.out.println("周一"); break;
    case 2: System.out.println("周二"); break;
    default: System.out.println("其他");
}

2. 循环语句

java 复制代码
// for 循环
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

// while 循环
int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}

// do-while 循环
int j = 0;
do {
    System.out.println(j);
    j++;
} while (j < 10);

// foreach 循环(用于数组和集合)
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    System.out.println(num);
}

3. 跳转语句

java 复制代码
break;     // 跳出循环
continue;  // 跳过本次循环
return;    // 返回方法结果

七、数组

java 复制代码
// 数组声明和初始化
int[] arr1 = new int[5];  // 声明长度为5的数组
int[] arr2 = {1, 2, 3, 4, 5};  // 直接初始化

// 多维数组
int[][] matrix = new int[3][3];
int[][] matrix2 = {{1,2}, {3,4}};

// 数组操作
int length = arr2.length;  // 获取数组长度
Arrays.sort(arr2);         // 排序(需导入 java.util.Arrays)

八、面向对象基础

1. 类和对象

java 复制代码
// 定义类
public class Person {
    // 属性(字段)
    private String name;
    private int age;
    
    // 构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // 方法
    public void introduce() {
        System.out.println("我叫" + name + ",今年" + age + "岁");
    }
    
    // Getter 和 Setter
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

// 创建对象
Person person = new Person("张三", 25);
person.introduce();

2. 封装、继承、多态

java 复制代码
// 继承
class Student extends Person {
    private String studentId;
    
    public Student(String name, int age, String studentId) {
        super(name, age);  // 调用父类构造方法
        this.studentId = studentId;
    }
    
    // 方法重写(多态)
    @Override
    public void introduce() {
        super.introduce();
        System.out.println("学号:" + studentId);
    }
}

九、常用类

1. String 类

java 复制代码
String str = "Hello";
str.length();           // 长度:5
str.charAt(0);          // 字符:'H'
str.substring(1, 3);    // 子串:"el"
str.indexOf("l");       // 索引:2
str.equals("Hello");    // 比较:true
str.split(",");         // 分割

2. 包装类

java 复制代码
Integer num = Integer.valueOf(100);  // 装箱
int value = num.intValue();          // 拆箱

// 自动装箱拆箱
Integer a = 100;    // 自动装箱
int b = a;          // 自动拆箱

十、异常处理

java 复制代码
try {
    // 可能抛出异常的代码
    int result = 10 / 0;
} catch (ArithmeticException e) {
    // 处理算术异常
    System.out.println("除数不能为零");
} catch (Exception e) {
    // 处理其他异常
    e.printStackTrace();
} finally {
    // 无论是否异常都会执行
    System.out.println("程序执行完毕");
}

// 抛出异常
public void checkAge(int age) throws Exception {
    if (age < 0) {
        throw new IllegalArgumentException("年龄不能为负数");
    }
}

学习建议

  1. 动手实践:多写代码,理解每个概念
  2. 理解面向对象思想:这是 Java 的核心
  3. 掌握常用 API:特别是集合框架、IO 流等
  4. 学习设计模式:提升代码质量
  5. 理解 JVM 原理:深入理解 Java 运行机制

这是 Java 的基础知识框架。每个部分都可以进一步深入,比如集合框架、IO流、多线程、网络编程等。你想深入了解哪个部分?

相关推荐
癫狂的兔子2 小时前
【BUG】【Python】eval()报错
python·bug
想做后端的小C2 小时前
Java:访问权限
java·开发语言
masterqwer2 小时前
day42打卡
python
不会飞的鲨鱼2 小时前
抖音验证码滑动轨迹原理(很难审核通过)
javascript·python
我命由我123452 小时前
Python 开发问题:No Python interpreter configured for the project
开发语言·后端·python·学习·pycharm·学习方法·python3.11
禾高网络2 小时前
互联网医院定制|互联网医院|禾高互联网医院搭建
java·大数据·人工智能·小程序
掘根2 小时前
【消息队列项目】消费者管理模块实现
java·开发语言
lzhdim2 小时前
C#应用程序取得当前目录和退出
开发语言·数据库·microsoft·c#
努力的小郑2 小时前
MyBatis 两个隐蔽深坑实录:Arrays.asList() 与数字 0 的“离奇失踪”
java·面试·mybatis