【蜕变之路】第11天 基本类与包装类 (2019年3月1日)

发布时间:2020-04-07 13:21:28 作者:wuningfei
来源:网络 阅读:240

    Hello,大家好!我是程序员阿飞!今天是我在新公司第一天上班,心情很激动。很感谢新公司领导对我的认可与信赖,我会在以后的工作中认真负责,为公司创造更多的利润。好了,今天主要学习的是:基本类与包装类。

    1、什么是包装类型?什么是基本类型?什么是自动拆装箱?

    基本类型:最简单的类型。如:八大基本数据类型。

    包装类型:在进行类型转换的范畴内,有一种特殊的转换,需要将int这样的基本数据类型转换为对象;所有的基本类型都一个与之对应的类,即包装类。也就是基本包装类。

    八大基本数据类型与之对应的包装类如下:

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

    自动装箱:将原始数据类型转换成对应的对象

    public void toInteger(){

       int i = 1;//基本数据类型

       Integer j = new Integer(i);//第一种方式

       Integer k = Integer.valueOf(i)//第二种方式

       Integer h = i;//第三种方式

    }

    自动拆箱:将对象转换成原始数据类型的过程

    public void toInt(){

      Integer i = new Integer(5);

      int j = i.intValue(i);

    }


    2、int与Integer的区别

    Integer默认值是null,而int的默认值是0。

     声明Integer的变量需要实例化,而声明int的变量不需要实例化

     Integer是对象,用一个引用指向这个对象,而int是基本类型,直接储存数值。

     Int类型是放在栈空间的,而Integer对象是放在堆空间的。


    3、String与int之间的转换

    将字符串String转换成整型int

    public void strToInt(){

       int i = Integer.parseInt("123");

       //直接使用静态方法,不会产生多余的对象,但会抛出异常

    }

    public void strToInt(){

       String s = "123";

       Integer q = Integer.valueOf(s).intValue();

       //Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

    }

    将int整型转换成String字符串

    public void intToStr(){

       int i = 1;

       String j = i+"";

       //会产生两个String对象

    }

    public void intToStr(){

       int i = 1;

       String j = String.valueOf(i);

       //直接使用String类的静态方法,只产生一个对象

    }

    public void intToStr(){

       int i = 1;

       String j = i.toString();

       //待定

    }


    4、Integer类的缓存机制

    public void test(){

       Integer a1 = 100;

       Integer a2 = 100;

       Integer b1 = 200;

       Integer b2 = 200;

       System.out.printIn(a1 == a2);

       System.out.printIn(b1 == b2);

    }

    //输出的结果:

    true

    false

    /**

         * Returns an {@code Integer} instance representing the specified

         * {@code int} value.  If a new {@code Integer} instance is not

         * required, this method should generally be used in preference to

         * the constructor {@link #Integer(int)}, as this method is likely

         * to yield significantly better space and time performance by

         * caching frequently requested values.

         *

         * This method will always cache values in the range -128 to 127,

         * inclusive, and may cache other values outside of this range.

         *

         * @param  i an {@code int} value.

         * @return an {@code Integer} instance representing {@code i}.

         * @since  1.5

         */

        public static Integer valueOf(int i) {

            if (i >= IntegerCache.low && i <= IntegerCache.high)

                return IntegerCache.cache[i + (-IntegerCache.low)];

            return new Integer(i);

        }

    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;

            }

    

            private IntegerCache() {}

        }

    上面是一个Integer类中的内部类,是处理Integer的缓存,cache的大小为256,static块中是对cache赋值从-128(包含)~127(包含),所以上面的例子可以得出100在范围以内不会产生新的对象,直接返回缓存中的对象,200不在范围内会产生新的对象,注意Integer的缓存数据是不会被垃圾回收。

推荐阅读:
  1. mybatis数据库中的date无法在 java中完全映射如何解决
  2. 怎么在java中自定义注解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 基本类型 包装类型

上一篇:KVM 克隆虚拟机

下一篇:20180524早课记录17-Hadoop

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》