zoukankan      html  css  js  c++  java
  • 常用类一一基本数据类型的包装类(WrapperClass)一一Byte Short nteger Long Float Double Character Boolean

    为什么需要包装类?

    JAVA是支持跨平台的。可以在服务器 也可以在手机上运行

    基本数据类型 在栈中  效率更高

    包装类 将数据类型转换成对象 在 堆中  利于操作

     1 package cn.bjsxt.wrapperclass;
     2 /**
     3  * wrapperclass包装类
     4  * 包装类位置lang包 不需要导入
     5  * 
     6  * @author Administrator
     7  *
     8  */
     9 public class Test01 {
    10     public static void main(String[] args) {
    11         //将1000转成对象
    12         Integer i  = new Integer(1000);
    13          //整数的最大值
    14         System.out.println(Integer.MAX_VALUE);
    15         //用16进制表示
    16         System.out.println(Integer.toHexString(i));
    17         // 将字符串转成整数 赋值给i2
    18         Integer i2 = Integer.parseInt("123");
    19         System.out.println(i2);
    20         //+10
    21         System.out.println(i2+10);
    22         Integer i3 = new Integer("233");
    23         System.out.println(i3);
    24         Integer i4 = Integer.parseInt("55");
    25         System.out.println(i4);
    26         //数字变成字符串 直接拼接
    27         String str = 125+"";
    28         System.out.println(str);
    29     }
    30 }

     自动装箱和自动拆箱

     auto-boxing and unboxing

    自动装箱

    基本类型就自动封装到与他相同类类型的包装类中,如:

    Integer  i =1000;

    本质是 编译器为 我们添加了

     Inteager i = new Integer(1000);

    自动拆箱

    包装类对象自动转成基本数据类型。如:

    int a = new Inteager(1000);

    本质是, 编译器为我们添加了

    int a = new Integer(1000).intValue();

     1 public class test02 {
     2     public static void main(String[] args) {
     3         //自动装箱
     4         //Integer i = new Integer(1000);
     5         Integer i = 1000;//JDK1.5之后 编译器帮我们写 Integer i = new Inteager(1000);
     6         Integer b = 2000;
     7         //自动拆箱
     8         int c = new Integer(1000);//编译器帮我写了 new Integer(1000).intValue();
     9         int d = b;//相当于 int d = b.intValue();
    10         
    11         System.out.println(d);
    12         
    13         Integer a3 =1234;
    14         Integer a4 =1234;
    15         System.out.println(a3==a4);//false 因为是不同对象 地址不同
    16         System.out.println(a3.equals(a4)); //true 值相等
    17         
    18         System.out.println("#######################");
    19         //[-128,127]之间的数 仍然按照基本数据类型来处理 为了提高效率 基本类型比对象快 
    20         //他也封装了 不过在运行时 又变回基本数据类型 为了效率
    21         Integer a =127;
    22         Integer a2 =127;
    23         System.out.println(a==a2);//ture 为什么?上面是false 这里是ture 说好的不同对象呢 
    24         System.out.println(a.equals(a2));
    25         
    26         Integer a5=-128;
    27         Integer a6 =-128;
    28         System.out.println(a5==a6);//ture 为什么?上面是false 这里是ture 说好的不同对象呢 
    29         System.out.println(a5.equals(a6));
    30         
    31         System.out.println("########################");
    32         
    33         Integer a7=128;
    34         Integer a8 =128;
    35         System.out.println(a7==a8);//false 
    36         System.out.println(a7.equals(a8));
    37         
    38         Integer a9=-129;
    39         Integer a10 =-129;
    40         System.out.println(a9==a10);//false 
    41         System.out.println(a9.equals(a10));
    42         
    43     }
    44 }
  • 相关阅读:
    第1章 CLR的执行模型
    第19章 可空值类型
    Servlet 3.0 特性
    java代码逆序输出再连篇
    java代码逆序输出数字
    java代码实现从键盘输入编号,输出价格,并且不再编号内的,无效输入!!!!
    java代码,实现输入编号,输出对应水果的单价~~~~
    java.输入水果的编号,求它对应的单价
    java.控制次数,每一组数都要计算。所以有个嵌套
    java练习,,,从键盘输入次数,输出最大值,和
  • 原文地址:https://www.cnblogs.com/PoeticalJustice/p/7630110.html
Copyright © 2011-2022 走看看