Integer(包装类)和int(原始数据类型)Integer和int之间的主要区别在于Integer是包装类,而int是原始数据类型。
一个int是一种数据类型,它存储32位带符号的二进制补码整数,而一个Integer是一个将基本类型int包装在一个对象中的类。
一个整数可以被用作一个参数的方法,需要一个对象,而INT可被用作一个参数,需要一个整数值的方法,其可用于算术表达式。
int数据类型有助于将整数值存储在内存中,而Integer则有助于将int转换为对象并将对象转换为int。
除非标记为final并且Integer类包含一个int值并且是不可变的,否则int类型的变量是可变的。
例1public class PrimitiveDataTypeTest {
public static void main(String []args) {
//声明int-
int a = 20;
int b = 40;
int result = a+b;
System.out.println("Result is: " + result);
}
}
输出结果Result is: 60
例2public class WrapperClassTest {
public static void main(String []args) {
int a = 20;
Integer b = Integer.valueOf(a);
System.out.println("Converted Value of b is: " + b);
Integer c = new Integer(30);
int d = c.intValue();
System.out.println("Converted Value of d is: " + d);
}
}
输出结果Converted Value of b is: 20
Converted Value of d is: 30