Java记录 -30- 包装类

来源:互联网 发布:网络抓包软件 编辑:程序博客网 时间:2024/05/21 06:12

包装类(Wrapper Class),针对于原生数据类型的包装。

1. Java提供了8种原生数据类型。但在使用过程中,许多地方都需要使用对象类型,而原生数据类型不是对象,这时就需要将原生数据类型包装成对象类型来使用。

2. Java8种原生数据类型都提供了包装类。包装类的对象中承载了具体的原生数据类型的值。

3. 我们可以像用对象类型的操作来操作原生数据类型。所有的包装类(8种)都位于java.lang包下。

4. Java 中八个包装类分别是:ByteShortIntegerLongFloatDoubleCharacterBoolean。他们的使用方式都是一样的,可以实现原生数据类型和包装类型的双向转换。


使用实例:

public static void main(String[] args){

int i = 3;

Integer integer = new Integer(i);

int ii = integer.intValue();

System.out.println(i == ii);

}


Class Integer

java.lang.Object

java.lang.Number

java.lang.Integer

All Implemented Interfaces:

SerializableComparable<Integer>
public final class Integer extends Number implements Comparable<Integer>

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int. 

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.



原创粉丝点击