当前位置:   article > 正文

java包装类 及其缓存

java包装类 及其缓存

Java 包装类(Wrapper Class)是将基本数据类型转换为对象的方式,每个基本数据类型在 java.lang 包中都有一个相应的包装类:

  1. Boolean 对应基本类型 boolean

  2. Character 对应基本类型 char

  3. Integer 对应基本类型 int

  4. Float 对应基本类型 float

  5. Double 对应基本类型 double

  6. Byte 对应基本类型 byte

  7. Short 对应基本类型 short

  8. Long 对应基本类型 long

 

包装类 自动拆箱 自动装箱

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. ArrayList<Integer> list = new ArrayList<Integer>();
  4. list.add(Integer.valueOf(10));
  5. list.add(20);//自动装箱
  6. list.add(30);
  7. Integer firstElement= list.get(0);
  8. int firstPrimitive=list.get(0);//自动拆箱
  9. System.out.println("First element as an Integer: " + firstElement);
  10. System.out.println("First element as an int: "+ firstPrimitive);
  11. for (Integer integer : list) {
  12. System.out.println(integer);
  13. }
  14. }

返回

  1. First element as an Integer: 10
  2. First element as an int: 10
  3. 10
  4. 20
  5. 30

包装类缓存

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. Integer a=100;
  4. Integer b=100;
  5. System.out.println("a == b is "+ (a == b));//true 在缓存范围内 -128 -127
  6. System.out.println("a.equals(b) is "+ a.equals(b));//
  7. Integer c=200;
  8. Integer d=200;
  9. System.out.println("c == d is "+ (c == d));//false
  10. System.out.println("c.euals(d) is "+ c.equals(d));//
  11. }

返回

  1. a == b is true
  2. a.equals(b) is true
  3. c == d is false
  4. c.euals(d) is true

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/861640
推荐阅读
相关标签
  

闽ICP备14008679号