int和Integer的区别&java的三大特性

int和Integer的区别
首先基本数据类型有包装类的原因有
1.为了方便数据转化
2.为了函数方便传参,比如你想传入一个object的类型参数 但你这会是int类型通过integer那么就可以顺利传参了
3.为了将基本数据类型当成对象操作

下表为原始数据类型和包装类型

原始类型 包装类型
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

此处包含自动装箱和自动拆箱
自动装箱是会自动将基本数据类型转化为包装类型,自动拆箱为将包装类型转化为基本数据类型。

        double a=2.01;
        int b=(int)a;
        System.out.println(b);
        Integer s1 = 127;
        Integer s2 =127;
        System.out.println(System.identityHashCode(s1));
        System.out.println(System.identityHashCode(s2));
        Integer s3 = new Integer(127);
        Integer s4 =new Integer(127);
        System.out.println(System.identityHashCode(s3));
        System.out.println(System.identityHashCode(s4));

运行结果:
在这里插入图片描述
在这里需要知道的是integer的常量池会封装(-128到127)超出这个范围就会在堆内存存储,那么在比较就会不一样。两个Integer对象是肯定不一样的。

java的三大特性:
封装:就是属性私有化,仅对外提供公共的访问方式get、set方法。这样可以增加安全性降低耦合
继承:就是将多个相同的属性和方法提取出来,新建一个父类。目的是代码的复用
多态有
(编译时/设计时)多态:指的是重载,允许方法名相同而参数不同,(返回值可相同可不同)
运行时多态:重写和向上转型(子类向父类转型)

猜你喜欢

转载自blog.csdn.net/peopleware1/article/details/105819577