/*
- 包装类(Wrapper Class)
- 1,针对八种基本数据类型定义相应的引用类型—包装类(封装类),有了类的特点,就可以调用类中的方法,Java才是真正的面向对象
- byte–Byte,short–Short,int–Integer,long–Long,float–Float,double–Double,这些类的直接父类都是Number。
- boolean–Boolean,char–Character.
- 2,基本数据类型,包装类与String类之间的转换
*/
package object_chapter2;
import org.junit.jupiter.api.Test;
import java.util.*;
public class Object_WrapperCLass {
@Test
public void wrapperTest() {
//基本数据类型转换到包装类,使用包装类构造器或自动装箱
byte by = 1;
short s = 2;
char ch = 'b';
int i = 3;
long l = 12;
Integer in = new Integer((int)1.23);//构造器形参可以传入byte,short,char类型数据,会进行自动类型提升。float,double无法直接传入,需要使用强制类型转换
Integer a = new Integer(10);//使用Integer类的构造器
Integer a1 = new Integer('A');//char型数据自动转换成对应ASCII码的数值进行赋值
Integer a2 = new Integer("6");//构造器中使用字符串参数,字符串转换成数值进行赋值
// Integer a2 = new Integer("6abcd"); //会报错,String类型参数必须是数字。
Integer a3 = 33; //JDK1.5之后的自动装箱方式
System.out.println(a.toString() + " " + a1.toString() + " " + a2.toString() + " " + a3.toString() );
Boolean b = new Boolean(true);
b = new Boolean("TRue");//查看方法源码,规定字符串忽略大小写。
Boolean b1 = new Boolean("false");
b1 = new Boolean("true1234");//查看源码,字符串只要不等于true都会返回false
Boolean b2 = false;
System.out.println(b.toString() + " " + b1.toString() + " " + b2.toString());
Character c = new Character('A');
// Character c1 = new Character("A");//Character类没有String参数的构造器。
Character c2 = 'a';
System.out.println(c.toString() + " " + c2.toString());
}
@Test
public void wrapperTest1() {
//包装类转换为基本数据类型,使用包装类的xxxValue()方法,或自动拆箱。
Integer in = new Integer("128");
int i = in.intValue();
System.out.println(i);
int j = in.byteValue();//Integer类的对象可以使用byteValue方法强转到byte型,
System.out.println(j);
int k = in;//自动拆箱
System.out.println(k);
//自动装箱与拆箱
System.out.println(in + 2);
Boolean b = true;//b自动装箱
System.out.println(!b);//b自动拆箱
}
@Test
public void wrapperTest2() {
//基本数据类型,包装类和String类型之间的转换
//基本数据类型转换为String
Integer in = 10;
int i = in;
System.out.println(i + "");//使用数值 + ""的方式做自动类型提升
String s = String.valueOf(i);//使用String类中重载的valueOf()方法
System.out.println(s);
s = String.valueOf(true);
System.out.println(s);
//包装类转换为String
String s1 = in.toString();
System.out.println(s1);//使用包装类对象的toString()方法
String s2 = Integer.toString(22);
System.out.println(s2);//使用包装类的toString(形参)方法
//String转换为包装类
Boolean b = new Boolean("False");//t通过包装类构造器传入String类型参数转换
System.out.println(b);
//String转换为基本数据类型
float f = new Float("2.34F");//通过相应包装类的构造器转换
float f2 = Float.parseFloat("3.1415f");//通过相应包装类的静态方法parseFloat();转换
System.out.println(f + " " + f2);
boolean b1 = new Boolean("true1");//不规范的true会转换为false,字符串中的true可以忽略大小写
boolean b2 = Boolean.parseBoolean("true123");//不规范的true会转换为false,字符串中的true可以忽略大小写
System.out.println(b1 + "" + b2);
}
@Test
public void wrapperTest3() {
//练习1
Object o1 = true ? new Integer(1) : new Double(2.0);//输出1.0,因为三元运算符也是运算符,int型与double型数据运算返回double型数据
System.out.println(o1);
Object o2;
if (true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2);//输出1
//练习2
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false,地址值不同
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true,
Integer x = 128;
Integer y = 128;
System.out.println(x == y);//false,
/*Integer中内部类规定导致结果不同
* -128至127范围内的数值储存在一个Integer类型的数组中,
* 如果使用自动装箱的方式,给Integer赋值在范围内可直接使用数组元素
* ,不用再创建新的内存结构,调用的时候指向同一地址以提高效率
* new方式创建的对象和超出此范围的数值会新建内存结构,地址值不同
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
*/
}
@Test
public void VectorTest() {
Scanner scan = new Scanner(System.in);
Vector v = new Vector();
//创建Vector数组
for (; ; ) {
System.out.println("请输入学生成绩,输入负数时结束");
// int i = scan.nextInt();
// Integer in = new Integer(i);//构造器创建Integer类的对象in
Integer in = scan.nextInt();//使用自动装箱方式
if(in < 0) { //运算时自动拆箱
break;
}
if(in > 100) {
System.out.println("数据非法,请重新输入");
continue;
}
v.addElement(in); //形参为Object类,Integer类为子类,使用多态方式传入参数。
}
//输出成绩列表
System.out.println("一共输入了" + v.size() + "个成绩");
for (int i = 0; i < v.size(); i++) {
//System.out.print(v.get(i) + " ");//效果相同
System.out.print(v.elementAt(i) + " ");
}
System.out.println();
//找到最高分
int maxObj = 0;
for (int i = 0; i < v.size(); i++) {
if(maxObj <= (Integer)v.elementAt(i)) {//v.elementAt(i)是Object类,需要向下转型成Integer类后自动拆箱参与运算。
maxObj = (Integer)v.elementAt(i);
}
}
System.out.println("最高分是" + maxObj);
//成绩转化为等级
for (int i = 0; i < v.size(); i++) {
Object obj = v.elementAt(i); //读取Object类对象
Integer in = (Integer)obj; //转换到Integer类
int j = (int)obj;//自动拆箱
j = in.intValue();//使用方法转换
j = in;//自动拆箱
if(maxObj - (Integer)v.elementAt(i) <= 10) {
System.out.println("成绩为 " + v.elementAt(i) + " 等级为A");
}else if(maxObj - (Integer)v.elementAt(i) <= 20) {
System.out.println("成绩为 " + v.elementAt(i) + " 等级为B");
}else if(maxObj - (Integer)v.elementAt(i) <= 30) {
System.out.println("成绩为 " + v.elementAt(i) + " 等级为C");
}else {
System.out.println("成绩为 " + v.elementAt(i) + " 等级为D");
}
}
}
}