int 和 Integer 的区别与作用

int是JAVA八大基本数据类型(byte,shor,int,long,char,boolean,float,double)之一。

JAVA语言为八大基本数据提供了包装类,Integer对应是int类型的包装类,就是把int类型包装成Object对象。

Java有2种不同的类型:引用数据类型和基本数据类型。Int是java的基本数据类型,Integer是java的为int提供的封装类。Java为每个基本数据类型提供了封装类。


int 是基本数据类型

Integer 是int的一个包装类,他是类不是基本数据类型,他的内部其实包含一个int型的数据

对象封装有很多好处,可以把属性也就是数据跟处理这些数据的方法结合在一起,比如Integer就有parseInt()等方法来专门处理int型相关的数据,另一个非常重要的原因就是在Java中绝大部分方法或类都是用来处理类类型对象的,如ArrayList集合类就只能以类作为他的存储对象,而这时如果想把一个int型的数据存入list是不可能的,必须把它包装成类,也就是Integer才能被List所接受。所以Integer的存在是很必要的。

始数据类型                 引用数据类型
boolean Boolean
char Character
byte Byte
short Short
int nteger
long Long
float Float

double Double

引用类型和基本类型的行为完全不同,并且它们具有不同的语义。引用类型和基本类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以那种类型的数据结构存储,当引用类型和原始类型用作某个类的实例时所指定的缺省值。对象引用实例变量的缺省值为null,而原始类型实例变量的缺省值与它们的类型有关。

Integer类的使用方法 

1、属性。 

static int MAX_VALUE:返回最大的整型数;

static int MIN_VALUE:返回最小的整型数; 

static Class TYPE :返回当前类型。

例子代码: 

System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE ); 

结果为:Integer.MAX_VALUE: 2147483647

2、构造函数。 

Integer(int value) :通过一个int的类型构造对象; 

Integer(String s) :通过一个String的类型构造对象;

例子代码: 

Integer i = new Integer("1234"); 

生成了一个值为1234的Integer对象。

3、方法。 

说明: 

1. 所有方法均为public; 

2. 书写格式:[修饰符] <返回类型> <方法名([参数列表])> 

如: 

static int parseInt(String s) 表示:此方法(parseInt)为类方法(static),返回类型为(int),方法所需参数为String类型。 

1. byteValue():取得用byte类型表示的整数; 

2. int compareTo(Integer anotherInteger) :比较两个整数。相等时返回0;小于时返回负数;大于时返回正数。

例子代码: 

Integer i = new Integer(1234); 

System.out.println("i.compareTo: " + i.compareTo(new Integer(123)) ); 

结果为:i.compareTo: 1 

3. int compareTo(Object o) :将该整数与其他类进行比较。如果o也为Integer类,进行方法2的操作;否则,抛出ClassCastException异常。 

4. static Integer decode(String nm) :将字符串转换为整数。 

5. double doubleValue() :取得该整数的双精度表示。 

6. boolean equals(Object obj) :比较两个对象。

7. float floatValue() :取得该整数的浮点数表示。 

8. static Integer getInteger(String nm) :根据指定名确定系统特征值。 

9. static Integer getInteger(String nm, int val) :上面的重载。 

10. static Integer getInteger(String nm, Integer val) :上面的重载。 

11. int hashCode() :返回该整数类型的哈希表码。 

12. int intValue() : 返回该整型数所表示的整数。 

13. long longValue() :返回该整型数所表示的长整数。 

14. static int parseInt(String s) :将字符串转换成整数。s必须是时进制数组成,否则抛出NumberFormatException异常。 

15. static int parseInt(String s, int radix) :以radix为基数radix返回s的十进制数。所谓的基数就是“几进制”。

例子代码: 

String s1 = new String("1010"); 

System.out.println("Integer.parseInt(String s, int radix): " + Integer.parseInt(s1,2) ); 

结果为:Integer.parseInt(String s, int radix): 10 

即s1在此例中被看成是二进制,返回十进制就是10。

16. short shortValue() :返回该整型数所表示的短整数。 

17. static String toBinaryString(int i) :将整数转为二进制数的字符串。 

18. static String toHexString(int i) :将整数转为十六进制数的字符串。 

19. static String toOctalString(int i) :将整数转为八进制数的字符串。 

20. String toString() :将该整数类型转换为字符串。 

21. static String toString(int i) :将该整数类型转换为字符串。不同的是,此为类方法。 

22. static String toString(int i, int radix) :将整数i以基数radix的形式转换成字符串。

例子代码: 

int i1 = 54321; 

System.out.println("Integer.toString(int i, int radix): " + Integer.toString(i1,16) ); 

结果为:Integer.toString(int i, int radix): d431 

23. static Integer valueOf(String s) :将字符串转换成整数类型。 

24. static Integer valueOf(String s, int radix) :将字符串以基数radix的要求转换成整数类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值