zoukankan      html  css  js  c++  java
  • java-Integer类

    1、为什么会有基本类型包装类
      * 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。


    2、常用操作
      * 常用的操作之一:用于基本数据类型与字符串之间的转换。

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


    3、基本类型和包装类的对应
      byte - Byte
      short - Short
      int - Integer
      long - Long
      float - Float
      double - Double
      char - Character
      boolean - Boolean

    4、构造方法
      * public Integer(int value)
      * public Integer(String s)

    5、int -- String
      * a:和""进行拼接
      * b:public static String valueOf(int i)
      * c:int -- Integer -- String (Integer类的toString方法())
      * d:public static String toString(int i) (Integer类的静态方法)


    6、String -- int
      * a:String -- Integer -- int
      * public static int parseInt(String s)
      * 基本数据类型包装类有八种,其中七种都有parseXxx的方法,可以将这七种的字符串表现形式转换成基本数据类型
      * char的包装类Character中没有pareseXxx的方法,字符串到字符的转换通过toCharArray()就可以把字符串转换为字符数组。

    5、6例:

     1 public class Demo {
     2 
     3     public static void main(String[] args) {
     4         
     5         //demo1();
     6         String s1 = "true";
     7         boolean b = Boolean.parseBoolean(s1);
     8         System.out.println(b);
     9         
    10         //String s2 = "abc";
    11         //char c = Character.p        //char的包装类Character中没有pareseXxx的方法
    12                                       //字符串到字符的转换通过toCharArray()就可以把字符串转换为字符数组
    13     }
    14 
    15     private static void demo1() {
    16         //int ----> String int转换成String
    17         int i = 100;
    18         String s1 = i + "";                        //推荐用
    19         String s2 = String.valueOf(i);             //推荐用
    20         
    21         Integer i2 = new Integer(i);
    22         String s3 = i2.toString();
    23         
    24         String s4 = Integer.toString(i);
    25         System.out.println(s1);
    26         
    27         //String----> int String 转换int
    28         String s = "200";
    29         Integer i3 = new Integer(s);
    30         int i4 = i3.intValue();                    //将Integer转换成了int数
    31         
    32         int i5 = Integer.parseInt(s);              //将String转换为int,推荐用这种
    33     }
    34 
    35 }

    7、JDK5的新特性
      * 自动装箱:把基本类型转换为包装类类型
      * 自动拆箱:把包装类类型转换为基本类型
      * 注意事项
        * 在使用时,Integer x = null;代码就会出现NullPointerException。
        * 建议先判断是否为null,然后再使用。

    7例:

     1 public class Demo {
     2 
     3     public static void main(String[] args) {
     4 //        int x = 100;
     5 //        Integer i1 = new Integer(x);           //将基本数据类型包装成对象,装箱
     6 //        
     7 //        int y = i1.intValue();                 //将对象转换为基本数据类型,拆箱
     8         
     9         Integer i2 = 100;                        //自动装箱,把基本数据类型转换成对象
    10         int z = i2 + 200;                        //自动拆箱,把对象转换为基本数据类型
    11         System.out.println(z);
    12         
    13         Integer i3 = null;
    14         int a = i3 + 100;                        //底层用i3调用intValue,但是i3是null,null调用方法就会出现
    15         System.out.println(a);                   //空指针异常java.lang.NullPointerException
    16     }
    17 
    18 }

    8、面试题:

     1 public class Demo {
     2 
     3     public static void main(String[] args) {
     4         Integer i1 = new Integer(97);
     5         Integer i2 = new Integer(97);
     6         System.out.println(i1 == i2);                 //false
     7         System.out.println(i1.equals(i2));            //true
     8         System.out.println("-----------");
     9     
    10         Integer i3 = new Integer(197);
    11         Integer i4 = new Integer(197);
    12         System.out.println(i3 == i4);                 //false
    13         System.out.println(i3.equals(i4));            //true
    14         System.out.println("-----------");
    15     
    16         Integer i5 = 127;
    17         Integer i6 = 127;
    18         System.out.println(i5 == i6);                 //true
    19         System.out.println(i5.equals(i6));            //true
    20         System.out.println("-----------");
    21     
    22         Integer i7 = 128;
    23         Integer i8 = 128;
    24         System.out.println(i7 == i8);                 //false
    25         System.out.println(i7.equals(i8));            //true
    26         
    27         /*
    28          * -128到127是byte的取值范围,如果在这个取值范围内,自动装箱就不会新创建对象,而是从常量池中获取
    29          * 如果超过了byte取值范围就会再新创建对象
    30          * 
    31          * public static Integer valueOf(int i) {
    32                 assert IntegerCache.high >= 127;
    33                 if (i >= IntegerCache.low && i <= IntegerCache.high)            //i>= -128 && i <= 127
    34                     return IntegerCache.cache[i + (-IntegerCache.low)];
    35                 return new Integer(i);
    36             }
    37          */
    38     }
    39 
    40 }
  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/hfumin/p/10187868.html
Copyright © 2011-2022 走看看