Java 中的默认布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21509150/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:04:44  来源:igfitidea点击:

Default Boolean value in Java

javaboolean

提问by Ernestas Gruodis

I just want to know if there is a difference in Java between:

我只想知道 Java 之间是否存在差异:

private boolean someValue;

private boolean someValue = false;

The second line maybe is just a time wasting?

第二行也许只是浪费时间?

EDIT (SUMMARY):

编辑(摘要):

From the answers I found that there is almost no difference, but:

从答案中我发现几乎没有区别,但是:

"Relying on such default values, however, is generally considered bad programming style."

“然而,依赖这样的默认值通常被认为是糟糕的编程风格。”

But there are some strong arguments not to do so - see accepted answer below.

但是有一些强有力的论据不这样做 - 请参阅下面的已接受答案。

EDIT 2

编辑 2

I found that in some cases booleanvalue must beinitialized, otherwise the code will not compile:

我发现在某些情况下必须初始化booleanvalue ,否则代码将无法编译

boolean someValue;
if (someValue) { // Error here
     // Do something
}

In my NetBeans IDE I got the error - "variable someValue might not have been initialized".

在我的 NetBeans IDE 中,我收到错误 - “变量 someValue 可能尚未初始化”

It's getting interesting.. :)

越来越有趣了..:)

采纳答案by assylias

All instance and class variables in Java are initialised with a default value:

Java 中的所有实例和类变量都使用默认值初始化:

For type boolean, the default value is false.

对于类型boolean,默认值为false

So your two statements are functionally equivalent in a single-threaded application.

因此,您的两个语句在单线程应用程序中在功能上是等效的。

Note however that boolean b = false;will lead to two write operations: bwill first be assigned its default value falsethen it will be assigned its initial value (which happens to be falseas well). This may have an importance in a multi-threaded context. See this exampleof how explicitly setting the default value can introduce a data race.

但是请注意,这boolean b = false;将导致两个写入操作:b首先分配其默认值,false然后分配其初始值(恰好false也是)。这在多线程上下文中可能很重要。请参阅此示例,了解显式设置默认值如何引入数据竞争。

Relying on such default values, however, is generally considered bad programming style.

然而,依赖这样的默认值通常被认为是糟糕的编程风格。

I would argue the opposite: explicitly setting default values is bad practice:

我会反驳:明确设置默认值是不好的做法:

  • it introduces unnecessary clutter
  • it may introduce subtle concurrency issues
  • 它引入了不必要的混乱
  • 它可能会引入微妙的并发问题

回答by lakshman

If you didn't initialize it, it will be false. So there is no difference between them.

如果您没有初始化它,它将是false. 所以它们之间没有区别。

回答by Kick

The default value of booleandata type is falseso we can say that there is no difference.

boolean数据类型的默认值是false所以我们可以说没有区别。

回答by marcinj

There is no difference, from:

没有区别,来自:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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.

声明字段时并不总是需要赋值。已声明但未初始化的字段将由编译器设置为合理的默认值。一般而言,此默认值将为零或空值,具体取决于数据类型。然而,依赖这样的默认值通常被认为是糟糕的编程风格。

回答by StarsSky

If you declare as a primitive i.e. boolean. The value will be falseby default if it's an instance variable (or class variable).

如果您声明为原始 ie boolean。如果它是一个实例变量(或类变量),则默认情况下该值将为false

If it's an instance variable Booleanit will be null.

如果它是一个实例变量Boolean,它将为null

If it's declared within a method you will have to initialize it, or there will be a compiler error.

如果它是在方法中声明的,则必须对其进行初始化,否则会出现编译器错误。