在Java中,int是原始数据类型,而Integer是Wrapper类。
- int,作为原始数据类型具有较小的灵活性,int只能在其中存储整数的二进制值。
- 由于Integer是int数据类型的包装器类,因此它在存储,转换和操作int数据方面为我们提供了更大的灵活性。
- Integer是一个类,因此可以调用该类中定义的各种内置方法。 Integer类型的变量存储对Integer对象的引用,就像存储任何其他引用(对象)类型一样。
示例代码:
// 有效
int n = 20;
// 有效
Integer n = 45;
// 有效
Integer.parseInt("10");
// 无效
int.parseInt("10");
主要一些区别:
强制转换为字符串变量:不能直接或什至不能强制将字符串值(仅包含整数)分配给int变量。 但是,可以使用
Integer(String)
构造函数将String分配给Integer类型的对象。甚至可以使用parseInt(String)
将String文字转换为int
值。
示例代码: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类型的变量中这是不可能的。
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 + "
" + oct + "
" + hex);
}
}
运行结果如下:
1111011
173
7b
- 对数据执行操作:Integer类还允许我们反转数字或分别使用reverse(),rotateLeft()和rotateRight()左右旋转数字。。需要定义自己的逻辑来对int变量执行这些操作,因为它不是内置类。
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
+ "
Right rotate : " + rR + "
Reverse : " + rev);
}
}
运行结果如下:
Left Rotate : 48
Right rotate : 3
Reverse : 805306368
- 灵活性:整数包装器类为提供了对现有int数据类型的更大灵活性。 除了预定义的运算符,还可以对int值执行许多操作。 在需要将int变量视为对象的地方使用Integer类。 由于Wrapper类继承了Object类,因此可以在具有Object引用或泛型的集合中使用它们。 因此,我们将可空性的属性添加到现有的int数据类型中。
从Java 5开始,有了自动装箱的概念,其中原始数据类型自动转换为包装器类,反之亦然。 因此,可以在任何原始数据类型和任何Wrapper类之间执行任何算术或逻辑运算。
// 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));
}
}
运行结果如下:
Sum of 2 Integer objects :25
Sum of an Integer object and int value :14
Sum of an Integer object and double value :15.1
Sum of an Integer object and Double object :24.1
Java Integer和int的区别
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果。
转载请注明:文章转载自 有区别网 [http://www.vsdiffer.com]
本文标题:Java Integer和int的区别
本文链接:https://www.vsdiffer.com/vs/integer-vs-int.html
免责声明:以上内容仅代表 个人看法、理解、学习笔记、总结和研究收藏。不保证其正确性,因使用而带来的风险与本站无关!如本网站内容冒犯了您的权益,请联系站长,邮箱: ,我们核实并会尽快处理。