在Java中,int是原始数据类型,而Integer是Wrapper类。
- int,作为原始数据类型具有较小的灵活性。我们只能在其中存储整数的二进制值。
- 由于Integer是int数据类型的包装器类,因此它在存储,转换和操作int数据方面为我们提供了更大的灵活性。
- Integer是一个类,因此可以调用该类中定义的各种内置方法。与其他任何引用(对象)类型一样,Integer类型的变量存储对Integer对象的引用。
public class IntegerAndint {
public static void main(String[] args) {
Integer a = new Integer(1);
Integer b = new Integer(1);
int c = 1;
int d = 1;
System.out.println(a == b);// false
System.out.println(a == c);// true
System.out.println(c == d);// true
System.out.println(a.equals(b));// true
}
}
输出:
false
true
true
true
例子:
//有效
整数n = 20;
//有效
整数n = 45;
//有效
Integer.parseInt(“ 10”);
// 无效
int.parseInt(“ 10”);
差异要点:
- 强制转换为字符串变量:我们不能直接或什至通过强制转换将字符串值(仅包含整数)分配给int变量。但是,我们可以使用Integer(String)构造函数将String分配给Integer类型的对象。我们甚至可以使用parseInt(String)将String文字转换为int值。
filter_none
编辑
play_arrow
亮度_4
// Java program to illustrate
// difference between
// int and Integer
public
class
Main {
public
static
void
main(String args[])
{
Integer a =
new
Integer(
"123"
);
// Casting not possible
// int a = (int)"123";
// Casting not possible
// int c="123";
// Casting possible using methods
// from Integer Wrapper class
int
b = Integer.parseInt(
"123"
);
System.out.print(a +
new
Float(
"10.1"
));
}
}
输出:133.1
- 将值直接转换为其他基数:我们可以分别使用toBinaryString(),toOctalString()或toHexString()将存储在Integer类中的整数值直接转换为Binary,Octal或Hexadecimal格式。在int类型的变量中这是不可能的。
filter_none
编辑
play_arrow
亮度_4
// Java program to illustrate
// difference between
// int and Integer
public
class
Main {
public
static
void
main(String args[])
{
String bin = Integer.toBinaryString(
123
);
String oct = Integer.toOctalString(
123
);
String hex = Integer.toHexString(
123
);
System.out.print(bin +
"\n"
+ oct +
"\n"
+ hex);
}
}
输出:1111011 173 7b
- 对数据执行操作: Integer类还允许我们反转数字或分别使用reverse(),rotateLeft()和rotateRight()左右旋转数字。我们需要定义自己的逻辑来对int变量执行这些操作,因为它不是内置类。
filter_none
编辑
play_arrow
亮度_4
// Java program to illustrate
// difference between
// int and Integer
public
class
Main {
public
static
void
main(String args[])
{
// mainethods convert integer to its binary form,
// apply the desired operation
// and then returns the decimal form
// of the newly formed binary number
// (12)10->(1100)2 ->
// rotate left by 2 units -> (110000)2->(48)10
int
rL = Integer.rotateLeft(
12
,
2
);
// (12)10->(1100)2 ->
// rotate right by 2 units -> (0011)2->(3)10
int
rR = Integer.rotateRight(
12
,
2
);
//(12)10 -> (00000000000000000000000000001100)2
// -> reverse ->(00110000000000000000000000000000)2
// -> (805306368)10
// int is of 32 bits
int
rev = Integer.reverse(
12
);
System.out.print(
"Left Rotate : "
+ rL
+
"\nRight rotate : "
+ rR +
"\nReverse : "
+ rev);
}
}
输出:左旋:48 右旋转:3 反向:805306368
- 灵活性:整数包装器类为我们提供了对现有int数据类型的更大灵活性。除了预定义的运算符,我们还可以对int值执行许多操作。在需要将int变量像对象一样对待的地方使用Integer类。由于Wrapper类继承了Object类,因此可以在具有Object引用或泛型的集合中使用它们。因此,我们将可空性的属性添加到现有的int数据类型中。
从Java 5开始,我们有了自动装箱的概念,其中原始数据类型自动转换为包装器类,反之亦然。因此,我们可以在任何原始数据类型和任何Wrapper类之间执行任何算术或逻辑运算。filter_none编辑
play_arrow
亮度_4
// Java program to illustrate
// auto-boxing
import
java.util.function.Function;
import
java.util.function.Function;
public
class
Main {
public
static
void
main(String args[])
{
Integer a =
new
Integer(
"12"
);
Integer d =
new
Integer(
"13"
);
int
b =
2
;
double
c =
3.1
;
Double f =
new
Double(
"12.1"
);
int
d2 = a + d;
System.out.println(
"Sum of 2 Integer objects :"
+ (a + d));
System.out.println("Sum of an Integer object
and
int
value :" + (a + b));
System.out.println("Sum of an Integer object
and
double
value :" + (a + c));
System.out.println("Sum of an Integer object
and Double object :" + (a + f));
}
}
输出:2个整数对象的总和:25 一个整数对象和int值的总和:14 整数对象和double值的总和:15.1 整数对象和双精度对象的总和:24.1
除了Integer,我们在Java中还有更多与数据类型相对应的包装器类。给出如下:
Java中基本类型的等效包装器类