zoukankan      html  css  js  c++  java
  • Java学习笔记24(Integer类)

    这里介绍基本数据类型包装类,Integer是int的包装类,

    其他的基本数据类型的包装类的方法和Integer的方法几乎一致,会一种即可全会

    基本数据类型包装类的特点:用于在基本数据类型和字符串之间进行转换

    这些类属于java的核心类,不需要import

    Integer类的方法:

    parseInt方法

    示例:

    将字符串变成基本类型

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
            function2();
        }
    
        public static void function1() {
            int i = Integer.parseInt("-12");
            // 可以把一个字符串变成int型
            System.out.println(i / 2);// -6
        }
    
        public static void function2() {
            int i = Integer.parseInt("1010", 2);
            // 将一个二进制数的字符串转成十进制int型
            System.out.println(i);// 10
        }
    }

    同样可以将基本类型变成字符串:

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
            function2();
            function3();
        }
    
        public static void function1() {
            int i = 3;
            String string = i + "";
            System.out.println(string + 1);
            // 这里输出字符串31
        }
    
        public static void function2() {
            int i = 3;
            // 这里的toString方法不是重写父类的方法
            String string = Integer.toString(i);
            System.out.println(string + 1);
            // 输出字符串31
        }
    
        public static void function3() {
            int i = 5;
            String string = Integer.toString(i, 2);
            System.out.println(string);
            // 转成二进制数,输出字符串101
        }
    }

    Integerl类的构造方法:

    示例:

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
        }
    
        public static void function1() {
            Integer integer = new Integer("100");
            int i = integer.intValue();
            // 这里顺便复习下i++和++i的区别
            // System.out.println(i++);//100
            System.out.println(++i);// 101
        }
    }

    其他方法:

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
            function2();
        }
    
        public static void function1() {
            // Integer类的静态成员变量
            System.out.println(Integer.MAX_VALUE);
            System.out.println(Integer.MIN_VALUE);
            System.out.println(Integer.SIZE);
            // 输出 2147483647 -2147483648 32
        }
    
        public static void function2() {
            int i = 666;
            System.out.println(Integer.toBinaryString(i));// 二进制字符串的1010011010
            System.out.println(Integer.toOctalString(i));// 八进制字符串的1232
            System.out.println(Integer.toHexString(i));// 十六进制的29a
    
        }
    }

    JDK1.5以后出现的特性:自动装箱,自动拆箱

    自动装箱:基本数据类型,直接变成对象

    自动拆箱:对象中的数据变回基本数据类型

    示例:

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
        }
    
        public static void function1() {
            Integer integer = 1;
            //这样写是合适的,自动装箱
            //本质上:Integer in = new Integer(1)
            integer = integer + 1;
            //自动拆箱,把引用类型拆成基本类型再做运算
            //本质上:integer+1 <==> integer.intValue()+1 = 2
            //再赋值给integer时候,自动装箱
            System.out.println(integer);
            //打印对象,但不是对象地址,而是1
        }
    }

    自动装箱和拆箱的好处:

    方便操作,简化代码,使基本类型和引用类型之间可以直接计算

    弊端:例如Integer in = null;  in = in + 1;这里就会出现异常,必须加入相应的处理方法

    关于自动装箱和拆箱的注意事项:

    这里有一个在Java面试中坑了很多人的地方,

    package demo;
    
    public class IntegerDemo {
        public static void main(String[] args) {
            function1();
            function2();
            function3();
        }
    
        public static void function1() {
            Integer i = new Integer(1);
            Integer j = new Integer(1);
            System.out.println(i==j);//false
            //这里比较的是两个对象的地址,当然不同
            System.out.println(i.equals(j));//true
            //这里是比较对象的数据,不比较地址
        }
        
        public static void function2(){
            Integer a = 500;
            Integer b = 500;
            System.out.println(a==b);//false
            System.out.println(a.equals(b));//true
        }
        
        public static void function3(){
            Integer a = 127;
            Integer b = 127;
            System.out.println(a==b);//true
            //这里注意,大于128就是false
            //当数据在bytes范围内,JVM为了节约内存不会创建新对象
            //这里Integer b = 127 <==> Integer b = a
            System.out.println(a.equals(b));//true
        }
    }
  • 相关阅读:
    java 三目运算符
    增强for 可以用于ArrayList
    shell SORT
    BASH if/while/until loop
    设置临时环境变量
    2015年创业中遇到的技术问题:121-130
    2015年创业中遇到的技术问题:121-130
    JDK8新特性之Lambda表达式
    使用Html5开发Android和iOS应用:HBuilder、Html5Plus、MUI
    使用Html5开发Android和iOS应用:HBuilder、Html5Plus、MUI
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8269091.html
Copyright © 2011-2022 走看看