Java 是否将 int 初始化为 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36854054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize int to 0 or not?
提问by Nouvel Travay
In the android source code I see they define four variables as
在 android 源代码中,我看到他们将四个变量定义为
protected int mPaddingRight = 0;
protected int mPaddingLeft = 0;
protected int mPaddingTop;
protected int mPaddingBottom;
In Java, what is the difference in initializing a variable to 0 or not? I don't understand that in some compilers I cannot do a comparison unless I initialize the field. But that is not the case here. Does this have to do with optimization? Or is this just inconsistent/bad coding practice?
在 Java 中,将变量初始化为 0 与否有什么区别?我不明白在某些编译器中,除非我初始化该字段,否则我无法进行比较。但这里的情况并非如此。这和优化有关系吗?或者这只是不一致/糟糕的编码实践?
采纳答案by F.Igor
According to Java primitive data types turorial, all primitive data types have a default value. So the initialization it's implicit. A good practice: initialize values before using to prevent unexpected behavior.
根据Java 原始数据类型 turorial,所有原始数据类型都有一个默认值。所以初始化是隐式的。一个好的做法:在使用之前初始化值以防止意外行为。
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false
回答by Evdzhan Mustafa
As noted in the comments, no there is no difference. They'll be initalized with 0 implicitly. However if you use good IDE or have other tools, it would be really easy for you to search and replace = 0;
with = SomeOtherValueHere;
.
正如评论中所指出的,没有没有区别。它们将被隐式初始化为 0。但是,如果您使用良好的 IDE 或有其他工具,则搜索和替换= 0;
为= SomeOtherValueHere;
.
Also I think it is a good practice to always initialzie your variables before you access them.
此外,我认为在访问变量之前始终对其进行初始化是一个很好的做法。
回答by Pritam Banerjee
It is a good coding practice to initialize variables.
初始化变量是一种很好的编码习惯。
From Oracle Docs:
来自 Oracle 文档:
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
声明字段时并不总是需要赋值。已声明但未初始化的字段将由编译器设置为合理的默认值。一般而言,此默认值将为零或空值,具体取决于数据类型。然而,依赖这样的默认值通常被认为是糟糕的编程风格。
The benefits of initializing the variablesare as following:
初始化变量的好处如下:
- Makes it easier to follow your code
- Makes life easier for static analysis tools.
- Most of the prevalent design patterns asks you to initialize variable to a default value, so that the programmer knows exactly to which value the variable is initialized.
- It is always good practice to initialize variables to prevent undefined behavior later in the program.
- Debugging becomes easier if you initialize the variables.
- 更容易遵循您的代码
- 让静态分析工具的工作更轻松。
- 大多数流行的设计模式都要求您将变量初始化为默认值,以便程序员确切地知道变量初始化为哪个值。
- 初始化变量以防止程序后期出现未定义的行为始终是一个好习惯。
- 如果初始化变量,调试会变得更容易。
回答by Pritam Banerjee
Others have pointed out that class properties are initialized to default values for you.
其他人指出,类属性已为您初始化为默认值。
So, semantically, there is no difference if you set them explicitly to 0 (or, for object properties, set those to null.)
因此,从语义上讲,如果将它们显式设置为 0(或者,对于对象属性,将它们设置为 null)没有区别。
However, there may in fact be a difference at the bytecode level. There is no guarantee that code that differs only in class properties that are implicitly vs. explicitly set to default values will have exactly the same bytecode.
但是,实际上可能在字节码级别存在差异。不能保证仅在隐式和显式设置为默认值的类属性中不同的代码将具有完全相同的字节码。
Older versions of the JDK used to generate larger and longer code for explicit initialization, which was easy to check with javap
. (This fact was occasionally used as the basis for interview questions.) I haven't checked recent versions of the JDK to see if this is still the case.
旧版本的 JDK 用于为显式初始化生成更大更长的代码,使用javap
. (这个事实偶尔被用作面试问题的基础。)我没有检查 JDK 的最新版本,看看是否仍然如此。