Files
javaSE260715/day07/src/com/inmind/arraylist03/Demo10.java
T

40 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.inmind.arraylist03;
import java.util.ArrayList;
/*
基本类型 基本类型包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
常用类-ArrayList-基本类型的存储方式_转换为包装类
自动装箱---int型的值,装箱成了引用数据类型Integer
自动拆箱---Integer类型对象,自动拆箱为基本数据类型int
基本数据类型4类8种,都有对应的包装类型,特殊记忆intInteger charCharacter
*/
public class Demo10 {
public static void main(String[] args) {
//定义一个存放int型数据的集合
//ArrayList<int> list1 = new ArrayList<int>(); 错误
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);//自动装箱:int型 1 ---->Integer 类型的对象
list1.add(2);
list1.add(3);
System.out.println(list1);
//获取第二个值
Integer i = list1.get(1);
System.out.println(i);//此时i是引用数据类型,保存的是地址,但是底层源码修改了toString的效果
//包装类在数学运算时就会自动拆箱
int sum = i +10;//自动拆箱:i是Integer对象,自动拆箱为int型的2的整数
System.out.println(sum);//12
}
}