C# 在Java和C中,int和Integer有什么区别?

C# 在Java和C中,int和Integer有什么区别?,c#,java,integer,int,C#,Java,Integer,Int,当我在阅读时,偶然发现有一种特殊类型的程序员知道Java/C(面向对象编程语言)中int和Integer之间的区别 那么,有什么区别呢?在Java中,“int”类型是一个基元,而“Integer”类型是一个对象 在C#中,int类型与System.Int32相同,是(即更像java的int)。整数(与任何其他值类型一样)可以(“包装”)到对象中 对象和原语之间的差异在某种程度上超出了本问题的范围,但概括起来: 对象为多态性提供了便利,通过引用传递(或者更准确地说,通过值传递引用),并从中分配。

当我在阅读时,偶然发现有一种特殊类型的程序员知道Java/C(面向对象编程语言)中
int
Integer
之间的区别


那么,有什么区别呢?

在Java中,“int”类型是一个基元,而“Integer”类型是一个对象

在C#中,int类型与
System.Int32相同,是(即更像java的int)。整数(与任何其他值类型一样)可以(“包装”)到对象中


对象和原语之间的差异在某种程度上超出了本问题的范围,但概括起来:


对象为多态性提供了便利,通过引用传递(或者更准确地说,通过值传递引用),并从中分配。相反地,原语是不可变的类型,通过值传递,并且通常从中分配。

在Java中,int是原语,而Integer是对象。也就是说,如果您创建了一个新的整数:

Integer i = new Integer(6);
您可以在i上调用某些方法:

String s = i.toString();//sets s the string representation of i
鉴于使用int:

int i = 6;
您不能对它调用任何方法,因为它只是一个原语。因此:

String s = i.toString();//will not work!!!
将产生错误,因为int不是对象

int是Java中为数不多的原语之一(以及char和其他一些原语)。我不是100%确定,但我认为Integer对象或多或少只是有一个int属性和一大堆与该属性交互的方法(例如toString()方法)。所以整数是一种处理int的奇特方式(就像字符串是处理一组字符的奇特方式一样)

我知道Java不是C语言,但由于我从未用C语言编程,这是我能找到的最接近答案。希望这有帮助

在C#中,int只是
System.Int32
的一个别名,string代表
System.string
,double代表
System.double


我个人更喜欢int、string、double等,因为它们不需要
使用系统语句:)一个愚蠢的原因,我知道…

我将补充上面给出的优秀答案,并讨论装箱和拆箱,以及这如何应用于Java(尽管C也有)。我将只使用Java术语,因为我更熟悉它

正如前面提到的答案,
int
只是一个数字(称为未装箱类型),而
Integer
是一个对象(它包含数字,因此是装箱类型)。在Java术语中,这意味着(除了不能调用
int
上的方法外),您不能在集合中存储
int
或其他非对象类型(
List
Map
,等等)。为了存储它们,您必须首先将它们装箱到相应的装箱类型中

Java5以后的版本有一种称为自动装箱和自动取消装箱的功能,它允许在后台进行装箱/取消装箱。比较和对比:Java 5版本:

Deque<Integer> queue;

void add(int n) {
    queue.add(n);
}

int remove() {
    return queue.remove();
}
必须注意的是,尽管Java5版本很简洁,但两个版本都生成相同的字节码。因此,尽管自动装箱和自动取消装箱非常方便,因为您编写的代码较少,但这些操作确实发生在幕后,运行时成本相同,因此您仍然必须知道它们的存在


希望这有帮助

我将在这里发布,因为其他一些帖子与C#有点不准确

正确:
int
系统的别名。Int32

错误:
float
不是
System.float
的别名,而是
System.Single的别名

基本上,int是C#编程语言中的保留关键字,是
System.Int32
值类型的别名

但是,float和float不相同,因为“
float
”的正确系统类型为system.Single。有些类型的保留关键字似乎与类型名不直接匹配

在C#中,“
int
”和“
System.Int32
”,或任何其他对或关键字/系统类型之间没有区别,定义枚举时除外。使用枚举可以指定要使用的存储大小,在这种情况下,只能使用保留关键字,而不能使用系统运行时类型名称

int中的值是存储在堆栈上、内存中还是作为引用的heap对象,取决于上下文和使用方式

此方法中的声明:

int i;
定义
System.Int32
类型的变量
i
,该变量位于寄存器或堆栈中,具体取决于优化。类型(结构或类)中的同一声明定义了成员字段。方法参数列表中的同一声明定义了一个参数,其存储选项与局部变量相同。(请注意,如果您开始将迭代器方法拉入混合中,则本段无效,因为它们完全不同)

要获取堆对象,可以使用装箱:

object o = i;

这将在堆上创建
i
内容的盒装副本。在IL中,您可以直接访问heap对象上的方法,但在C#中,您需要将其转换回int,这将创建另一个副本。因此,在C#中,如果不创建新int值的新装箱副本,堆上的对象就很难更改。(呃,这段话读起来并不那么容易。)

这已经为Java找到了答案,下面是C的答案:

“Integer”在C#中不是有效的类型名,“int”只是System.Int32的别名。此外,与Java(或C++)不同,C#中没有任何特殊的基元类型,C#中类型(包括int)的每个实例都是一个对象。下面是一些演示代码:

void DoStuff()
{
    System.Console.WriteLine( SomeMethod((int)5) );
    System.Console.WriteLine( GetTypeName<int>() );
}

string SomeMethod(object someParameter)
{
    return string.Format("Some text {0}", someParameter.ToString());
}

string GetTypeName<T>()
{
    return (typeof (T)).FullName;
}
void DoStuff()
{
System.Console.WriteLine(SomeMethod((int)5));
系统C
void DoStuff()
{
    System.Console.WriteLine( SomeMethod((int)5) );
    System.Console.WriteLine( GetTypeName<int>() );
}

string SomeMethod(object someParameter)
{
    return string.Format("Some text {0}", someParameter.ToString());
}

string GetTypeName<T>()
{
    return (typeof (T)).FullName;
}
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2); //  true
Integer i1 = new Integer(128);
Integer i2 = new Integer(128);
System.out.println(i1 == i2); //  false
System.out.println(i1.equals(i2)); //  true
int aNumber = 4;
int anotherNum = aNumber;
aNumber += 6;
System.out.println(anotherNum); // Prints 4
Integer aNumber = Integer.valueOf(4);
Integer anotherNumber = aNumber; // anotherNumber references the 
                                 // same object as aNumber
public int add(int a, int b) {
    return a + b;
}
final int two = 2;
int sum = add(1, two);
public int add(Integer a, Integer b) {
    return a.intValue() + b.intValue();
}
final Integer two = Integer.valueOf(2);
int sum = add(Integer.valueOf(1), two);
public void increment(int x) {
  x = x + 1;
}
int a = 1;
increment(a);
// a is now 2
public void increment(Integer x) {
  x = Integer.valueOf(x.intValue() + 1);
}
Integer a = Integer.valueOf(1);
increment(a);
// a is now 2
e.g. int i=10;
Integer a = new Integer();
int x;
Integer y; 
Integer.toString(x);
int number = 7;
Integer number = new Integer(5);
double doubleValue = 156.5d;
Double doubleObject  = new Double(doubleValue);
Byte myByteValue = doubleObject.byteValue ();
String myStringValue = doubleObject.toString();
Integer(int num)
Integer(String str) throws NumberFormatException
Double(double num)
Double(String str) throws NumberFormatException
class ManualBoxing {
        public static void main(String args[]) {
        Integer objInt = new Integer(20);  // Manually box the value 20.
        int i = objInt.intValue();  // Manually unbox the value 20
        System.out.println(i + " " + iOb); // displays 20 20
    }
}
class AutoBoxing {
    public static void main(String args[]) {
        Integer objInt = 40; // autobox an int
        int i = objInt ; // auto-unbox
        System.out.println(i + " " + iOb); // displays 40 40
    }
}
int a;
//assuming a value you are getting from data base which is null
if(a ==null) // this is wrong - cannot compare primitive to null
{
do something...}

Instead you will use,
Integer a;
//assuming a value you are getting from data base which is null
if(a ==null) // this is correct/legal
{ do something...}
Integer value1 = null; //OK

int value2 = null      //Error
List<Integer> element = new ArrayList<>();
int valueInt = 10;
Integer  valueInteger = new Integer(value);
element.add(valueInteger);
List<Integer> element = new ArrayList<>();
element.add(5);