java包装类

基本数据类型 包装类类型
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
基本数据 => 包装类
  1. JDK 5.0以下

    1. 通过构造器:

      Integer t = new Integer(11)
      Float t = new Float("11.1F")
  2. JDK 5.0以上

    1. 自动装箱

      int i = 10; Integer inI = i;
包装类 => 基本数据
  1. JDK 5.0以下

    1. 基础类型Value(包装类)

      intValue(xxx)
  2. JDK 5.0以上

    1. 自动拆箱

      Integer inI = new Integer(10); int i = inI;
包装类 => String类型
  1. 包装类型.toString(需要转的包装类)

    Integer.toString(new Integer(10))
String类型 => 包装类

1.new 包装类(String)

    Integer in = new Integer("10");
基本数据 => String类型

1.基本数据 + ""

`10.1f + "" = "10.1"`

2.String.valueOf(基本数据)

`String.valueOf(10.1f) = "10.1"`
字符串 => 基本数据
  1. 基础数据.parse基础数据(字符串)
`String s = "123"; Byte.parseByte(s) == (byte声明的)123; // true`

`String s = "123"; Short.parseShort(s) == (short)123 // true`

`String s = "123"; Integer.parseInt(s) == (int)123; // true`

`String s = "123"; Long.parseLong(s) == 123L; // true`

`String s = "123F"; Float.parseFloat(s) == (float)123.0; // true`

`String s = "127.0"; Double.parseDouble(s) == (double)127.0 // true`

`String s = "a"; s.charAt(0) == 'a' // true`

`String s = "tRue"; Boolean.parseBoolean(s) == true // true `

注意点
① 字符串转char使用charAt(index)
② 字符串转布尔 不区分大小写

追忆惘然
7 声望0 粉丝

站在巨人的肩膀上