Java核心类库——java中的包装类

java中的包装类
 1)包装类可以把基本类型包装为对象类型
 2)有八种包装类
  int   Integer
  long    Long
  byte  Byte
  short Short
  float  Float
  double Double
  boolean Boolean
  char  Character
 3) 包装类提供了  对应数据类型的 工具方法的
  Integer.toHexString()  //变为十六进制
  Integer.toString(int)  //转为10进制
  Integer.toBinaryString() //变为二进制
  Integer.parseInt(String) //把字符串换成int
  Integer,parseInt(String,int)
  Double.parseDouble(String str)

 4)自动包装(auto boxing/unboxing)
  Java 5以后可以
例子
  

public class WorpClassDemo {
 public static void main(String[] args) {
  Integer i = new Integer(5);
  Object obj = 1;
  //自动包装
  Object o = 5;//Object o = new Integer(5);
  System.out.println(o instanceof Integer);//ture
  
  //System.out.println(0 instanceof int)//编译错误
  int x = i+8;//Integer + int
  //x = i.intValue() + 8;//自动拆包
  i = i + 8;//先自动拆包,运算,运算以后再自动包装,性能比int a=1;a =a+1;差千百倍
 }

}


注意:
 1 包装类是final的类
 2包装类对象是不变的,与字符串类似(不变模式)

原文地址:https://www.cnblogs.com/hqr9313/p/2452824.html