Java 和 C# 中的 int 和 Integer 有什么区别? [英] What is the difference between an int and an Integer in Java and C#?

查看:36
本文介绍了Java 和 C# 中的 int 和 Integer 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读更多关于软件的乔尔 当我遇到 Joel Spolsky 说一些关于特定类型的程序员知道的事情时Java/C#(面向对象编程语言)中 intInteger 之间的区别.

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int and an Integer in Java/C# (Object-Oriented Programming Languages).

那么,有什么区别?

推荐答案

在 Java 中,'int' 类型是一个原始类型,而 'Integer' 类型是一个对象.

In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object.

在 C# 中, 'int' 类型与 System.Int32 相同,并且是 一个值类型(即更像 java 'int').整数(就像任何其他值类型一样)可以是 装箱(包装")到一个对象中.

In C#, the 'int' type is the same as System.Int32 and is a value type (ie more like the java 'int'). An integer (just like any other value types) can be boxed ("wrapped") into an object.

对象和原语之间的差异有点超出了这个问题的范围,但总结一下:

The differences between objects and primitives are somewhat beyond the scope of this question, but to summarize:

对象为多态性提供便利,通过引用传递(或更准确地说是通过值传递引用),并从.相反,原语是按值传递的不可变类型,通常从 堆栈.

Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap. Conversely, primitives are immutable types that are passed by value and are often allocated from the stack.

这篇关于Java 和 C# 中的 int 和 Integer 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆