JAVASE学习总结(9)

一。单元测试方法的使用

一个文件夹称为一个工程(src上一级)

c语言中status的定义为ypedef int status,status为int的一个同义词。 

println()里面如果是char[]数组输出的是内容,其他类型数组就是地址值(引用数据类型)

 二。包装类

1.包装类的理解

1)定义:包装类(封装类)是针对八种基本数据类型定义相应的引用类型

2)why:将基本数据类型转成类,才真正体现了面向对象,而且可以调用类中的方法

有的方法需要传入引用类型,有时候有需要对数据进行运算

3)how

byte---Byte
short---Short
int---Integer
long---Long
float---Float
double---Double
boolean---Boolean
char---Character

2.基本数据类型转换为包装类

        1)通过构造器

int num1 = 10;
Integer in1 = new Integer(num1);
Sysout(in1.toString()/in1);

        2)通过字符串参数

Float f1 = new Float(12.3f);
Sysout(f1);

Float f2 = new Float("12.3");
Sysout(f2);

        3)自动装箱

3.包装类转换为基本数据类型

        1)调用包装类的方法:xxxValue()

Float f1 = new Float(12.3);
float f2 = f1.floatValue();
Sysout(f2);

        2)自动拆箱

4.自动拆箱与自动装箱

public void test3(){
    //实例
    int num1 = 10;
    method(num1);//基本数据类型--->包装类的对象//public void method(Object obj){}


    //自动装箱 基本数据类型--->包装类的对象
        int mun2 = 10;
        Integer in1 =num2;


        boolean b1 =true;
        Boolean b2 = b1;
    
    //自动拆箱 包装类-->基本数据类型
        Sysout(in1.toString());
        int num3 = in1;


}

注意:

boolean类型默认为false

Boolean类型默认为NULL

5.基本数据类型包装类与String类型的转换

        1)基本数据类型,包装类--->String类型:调用String重载的valueof(Xxx xxx)        

public void test(){

    int num1 = 10;
    //方式1:连接运算
    String str1 = num1+"";
    //方式2:调用String的valueOf(Xxx xxx)
    float f1 =12.3f;
    String str2 = String.valueof(f1);//12.3


    Double d1 = new Double(12.4);
    String str3 = String.valueOf(d1);//12.4
}

        2)String类型--->基本数据类型,包装类:调用包装类的parseXxx(String s)

        

int num1 = (int)str1//强转一定得有子父类的关系


public void Test(){
    String str1 = "123";
//String str = "123a"; 可能会报NumberFormatException的异常
    int num = Integer.parseInt(str1);
    String str2 = "true1";
    boolean b1 = Boolean.parseBoolean(str2);//false



}

总结:基本数据类型与包装类通过自动装箱拆箱转换,他俩与String类型用parseXxx(String)和valueOf()类型转换

 第一个比较地址值,第二个拆箱比较数值,第三个超出了Integer缓存的范围数字

第三个超出了,new了,比较的是地址值

 

拆箱的   三目运算符在使用时会判断A B是否为相应的数据类型,所以这里1变成了1.0

三。static关键字

 对于一个关键字,我们要考虑在那用,做什么

猜你喜欢

转载自blog.csdn.net/qq_61551764/article/details/121663215
今日推荐