Java数据结构——第一章Java基础回顾

一、基本数据类型与类型转换

(一)8 种基本数据类型

|---------|--------|------|-------------------|
| 类型 | 说明 | 占用空间 | 取值范围 |
| byte | 字节型 | 1 字节 | -128 ~ 127 |
| short | 短整型 | 2 字节 | -32768 ~ 32767 |
| int | 整型 | 4 字节 | -2^31 ~ 2^31-1 |
| long | 长整型 | 8 字节 | -2^63 ~ 2^63-1 |
| float | 单精度浮点型 | 4 字节 | 约 ±3.4×10^38 |
| double | 双精度浮点型 | 8 字节 | 约 ±1.8×10^308 |
| char | 字符型 | 2 字节 | 0 ~ 65535 |
| boolean | 布尔型 | 1 字节 | true/false |

(二)类型转换

  1. 自动转换:小范围类型向大范围类型转换,无需强制类型转换
    • 转换顺序:byte→short→int→long→float→double
    • 示例:int num = 100; double d = num;(int自动转换为double)
  1. 强制转换:大范围类型向小范围类型转换,需使用(目标类型)
    • 示例:double d = 3.14; int num = (int)d;(结果为 3)

二、引用类型与对象操作

(一)值类型与引用类型区别

  • 值类型:变量直接存储数据值,存储在栈内存(如int a = 5;)
  • 引用类型:变量存储对象地址,对象实例存储在堆内存(如String str = "hello";)

(二)类与对象创建

  1. 类定义
复制代码

public class Student {

private String name;

private int age;

// 构造方法

public Student(String name, int age) {

this.name = name;

this.age = age;

}

// 成员方法

public void showInfo() {

System.out.println("姓名:" + name + ",年龄:" + age);

}

}

  1. 对象创建与使用
复制代码

Student student = new Student("张三", 20); // 实例化对象

student.showInfo(); // 调用方法

(三)static 成员(类变量 / 类方法)

  • 类变量:用static修饰,所有对象共享,通过类名.变量名访问
  • 类方法:用static修饰,只能访问static成员,通过类名.方法名调用
复制代码

public class MathUtil {

public static final double PI = 3.14159; // 静态常量

public static int sum(int a, int b) { // 静态方法

return a + b;

}

}

// 使用

double circleArea = MathUtil.PI * r * r;

int result = MathUtil.sum(5, 3);

三、参数传递机制

(一)基本数据类型传递

  • 值传递:实参值拷贝给形参,形参修改不影响实参
复制代码

public static void changeValue(int num) {

num = 100; // 形参修改

}

int a = 50;

changeValue(a);

System.out.println(a); // 输出50,实参未改变

(二)引用数据类型传递

  • 地址传递:实参地址拷贝给形参,形参通过地址修改对象会影响实参
复制代码

public class Person {

public String name;

public Person(String name) {

this.name = name;

}

}

public static void changeName(Person p) {

p.name = "李四"; // 修改对象属性

}

Person person = new Person("张三");

changeName(person);

System.out.println(person.name); // 输出"李四"

四、泛型编程

(一)泛型类定义与使用

  • 作用:实现类型安全的容器,避免强制类型转换
复制代码

// 泛型类定义

class GenericList<T> {

private T[] data;

private int size;

public GenericList(int capacity) {

data = (T[]) new Object[capacity];

size = 0;

}

public void add(T element) {

data[size++] = element;

}

public T get(int index) {

return data[index];

}

}

// 使用泛型类

GenericList<Integer> intList = new GenericList<>(5);

intList.add(10);

intList.add(20);

int num = intList.get(0); // 无需强制类型转换

(二)泛型方法

复制代码

public static <T> void printArray(T[] array) {

for (T element : array) {

System.out.print(element + " ");

}

System.out.println();

}

Integer[] nums = {1, 2, 3};

String[] strs = {"a", "b", "c"};

printArray(nums); // 输出:1 2 3

printArray(strs); // 输出:a b c

五、继承与多态

(一)继承机制

  • 单继承:Java 类只能继承一个父类,使用extends关键字
复制代码

// 父类

class Animal {

protected String name;

public Animal(String name) {

this.name = name;

}

public void move() {

System.out.println(name + "在移动");

}

}

// 子类

class Dog extends Animal {

public Dog(String name) {

super(name); // 调用父类构造方法

}

@Override // 方法重写

public void move() {

System.out.println(name + "在跑");

}

public void bark() {

System.out.println(name + "在叫");

}

}

// 多态应用

Animal animal = new Dog("旺财");

animal.move(); // 输出:旺财在跑(调用子类重写方法)

// animal.bark(); 错误,父类无此方法

(二)接口实现

  • 多实现:类可实现多个接口,使用implements关键字
复制代码

// 接口定义

interface Swimmable {

void swim();

}

interface Runable {

void run();

}

// 类实现接口

class Duck extends Animal implements Swimmable, Runable {

public Duck(String name) {

super(name);

}

@Override

public void swim() {

System.out.println(name + "在游泳");

}

@Override

public void run() {

System.out.println(name + "在快跑");

}

}

// 使用接口

Duck duck = new Duck("唐老鸭");

Swimmable swimmer = duck;

swimmer.swim(); // 输出:唐老鸭在游泳

六、集合与迭代器

(一)Collection 接口与常用实现类

  • ArrayList:动态数组,查询快,增删慢
  • LinkedList:链表,增删快,查询慢
  • HashSet:无序去重集合
  • TreeSet:有序去重集合

(二)迭代器遍历集合

复制代码

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class IteratorDemo {

public static void main(String[] args) {

List<String> fruits = new ArrayList<>();

fruits.add("苹果");

fruits.add("香蕉");

fruits.add("橘子");

// 使用迭代器遍历

Iterator<String> iterator = fruits.iterator();

while (iterator.hasNext()) {

String fruit = iterator.next();

System.out.println(fruit);

}

// 增强for循环(内部使用迭代器)

for (String fruit : fruits) {

System.out.println(fruit);

}

}

}

七、输入输出操作

(一)Scanner 输入

复制代码

import java.util.Scanner;

public class InputDemo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入姓名:");

String name = scanner.next();

System.out.print("请输入年龄:");

int age = scanner.nextInt();

System.out.println("姓名:" + name + ",年龄:" + age);

scanner.close();

}

}

(二)System.out 输出

复制代码

int num = 10;

double pi = 3.14159;

String str = "Hello";

// 换行输出

System.out.println("整数:" + num);

// 不换行输出

System.out.print("浮点数:" + pi);

// 格式化输出

System.out.printf("字符串:%s,整数:%d\n", str, num);

八、关键特性总结

  1. 封装:通过private修饰符隐藏内部实现,提供public方法访问
  1. 继承:通过extends实现代码复用,通过super调用父类成员
  1. 多态:父类引用指向子类对象,调用方法时表现不同行为
  1. 泛型:通过类型参数<T>实现类型安全的通用代码
  1. 接口:定义行为规范,实现类必须实现所有抽象方法
  1. 迭代器:统一遍历集合元素的方式,分离遍历逻辑与集合实现
相关推荐
Mryan20053 分钟前
Android 应用多语言与系统语言偏好设置指南
android·java·国际化·android-studio·多语言
iCxhust37 分钟前
PC16550 UART接收中断处理完整示例代码
c语言·开发语言·stm32·单片机·嵌入式硬件
wt_cs41 分钟前
C#财政票查验接口集成-医疗发票查验-非税收入票据查验接口
开发语言·科技·安全·金融·ocr
鲁Q同志1 小时前
若依导出模板时设置动态excel下拉框(表连接的)
java·excel
LXL_241 小时前
超子说物联网-MQTT_笔记1---通过EMQX搭建MQTT服务器
服务器·笔记·物联网
汇匠源2 小时前
Java 零工市场小程序 | 灵活就业平台 | 智能匹配 | 日结薪系统 | 用工一站式解决方案
java·小程序
why1512 小时前
java IO流
java
BillKu2 小时前
Vue3 + TypeScript 中 hook 优化记录
开发语言·javascript·typescript
小馒头君君2 小时前
近期GitHub热榜推荐
开发语言·windows·python·学习·github