zoukankan      html  css  js  c++  java
  • Java基本类型包装类

    基本类型包装类概述

    将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
    常用的操作是用于基本数据类型和字符串之间的转换。
    基本类型与包装类的定义如下:
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

    public class test {
        public static void main(String[] args) {
            
            System.out.println(Integer.toBinaryString(60));  //转化为二进制字符串
            System.out.println(Integer.toOctalString(60));   //转换成八进制字符串
            System.out.println(Integer.toHexString(60));  //转换成十六进制字符串
        
        }
        
    }
    输出:
    111100
    74
    3c
    

    integer类

    Integer类概述

    Integer类在对象中包装了一个基本类型int值,该类提供了多种方法,能在int类型和String类型之间相互转换,还提供了处理int类型时非常有用的其他一些常量和方法。

    Integer类的构造方法

    public Integer(int value)
    public Integer(String s)
    可以使用构造方法创建对象:

    public static void main(String[] args) {
            
            System.out.println(Integer.MAX_VALUE);
            System.out.println(Integer.MIN_VALUE);
            
            Integer i1=new Integer(100);
            System.out.println(i1);
            
            Integer i2=new Integer("100");
            System.out.println(i2);
            
            Integer i3=new Integer("abc");  // java.lang.NumberFormatException异常
                                                 因为abc不是数字字符串,所以转换会报错
            System.out.println(i3);
                    
        
        }
    输出:
    2147483647
    -2147483648
    100
    100
    Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.<init>(Unknown Source)
        at com.heima.collection.test.main(test.java:18)
    
    
    String和int类型的相互转换

    1.int类型转换为String类型
    和“”进行拼接
    public static String valueOf(int i)
    int--Integer--String(Integer类的toString()方法)
    public static String toString(int i) (Integer类的静态方法)
    2.String类型转换为int类型
    String--Integer--int
    public static int parseInt(String s)

    public class test {
        public static void main(String[] args) {
            //int--String
            int i=200;
            String s1=i+"";
            String s2=String.valueOf(i);
            
            Integer i2=new Integer(i);
            String s3=i2.toString();
            
            String s4=Integer.toString(i);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
            
            //String--int
            String s="200";
            Integer i3=new Integer(s);
            int i4=i3.intValue();
            
            int i5=Integer.parseInt(s);
            
            System.out.println(i4);
            System.out.println(i5);
        }
        
    }
    输出:
    200
    200
    200
    200
    200
    200
    
    parseXxx()方法

    基本数据类型包装类有八种,其中除了char的包装类Character中没有parseXxx()方法外,其它七种都有parseXxx()方法,可以将这七种的字符串表现形式转换成基本数据类型。
    char的包装类Character中没有parseXxx()方法,字符串到字符的转换通过toCharArray()就可以将字符串转换为字符数组。

    public class test {
        public static void main(String[] args) {
            
            String s="true";
            boolean b=Boolean.parseBoolean(s);
            System.out.println(b);
            
            char[] c=s.toCharArray();
            for(int i=0;i<c.length;i++)
                System.out.println(c[i]);
            
        }
        
    }
    输出:
    true
    t
    r
    u
    e
    
    JDK5的新特性

    自动装箱:把基本类型转换为包装类类型
    自动拆箱:把包装类类型转换为基本类型

    public class test {
        public static void main(String[] args) {
            
            int x=100;
            Integer i1=new Integer(x);  //将基本数据类型包装成对象,装箱
            int y=i1.intValue();     //将对象转换为基本数据类型,拆箱
            System.out.println(y);
            
            Integer i2=100;        //自动装箱,把基本数据类型转换成对象
            int z =i2+200;         //自动拆箱,把对象转换成基本数据类型
            System.out.println(z);
                    
            //Integer i3=null;
            //int a=i3+100;     //底层i3调用intValue()方法,但是i3是null,null调用方法就会出现空指针异常java.lang.NullPointerException
            //System.out.println(a);
        }
        
    }
    输出:
    100
    300
    
    面试题
    public class test {
        public static void main(String[] args) {
            
            Integer i1=new Integer(97);
            Integer i2=new Integer(97);
            System.out.println(i1==i2);
            System.out.println(i1.equals(i2));
            System.out.println("------------");
            
            Integer i3=new Integer(197);
            Integer i4=new Integer(197);
            System.out.println(i3==i4);
            System.out.println(i3.equals(i4));
            System.out.println("------------");
            
            Integer i5=97;
            Integer i6=97;
            System.out.println(i5==i6);
            System.out.println(i5.equals(i6));
            System.out.println("------------");
            
            Integer i7=197;
            Integer i8=197;
            System.out.println(i7==i8);   //原因在于-128到127是byte的取值范围,如果在这个范围内,自动装箱就不会新创建对象,而是从常量池中获取,如果超过了byte取值范围就会再重新创建对象
            System.out.println(i7.equals(i8));
            
        }
        
    }
    输出:
    false
    true
    ------------
    false
    true
    ------------
    true
    true
    ------------
    false
    true
    
    
  • 相关阅读:
    提取多层嵌套Json数据
    Jquery 使用Ajax获取后台返回的Json数据后,页面处理
    Jquery购物车jsorder改进版,支持后台处理程序直接转换成DataTable处理
    长连接和短连接
    JAVA8新特性
    线程、进程与程序+并行与并发
    订单号生成规则
    散列表解决冲突的方式
    125.Valid Palindrome
    128.Longest Consecutive Sequence
  • 原文地址:https://www.cnblogs.com/bbn0111/p/7499271.html
Copyright © 2011-2022 走看看