Java 布尔型实例变量默认值是 true 还是 false

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

Is a boolean instance variable default value true or false

javainstance-variables

提问by Jessica M.

  1. if you create an instance variable in a class, is the default value true or false until otherwise changed?

  2. Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class? Or is that something you should conceptually avoid in terms of using instance variables?

  1. 如果您在类中创建实例变量,默认值是真还是假,除非另有更改?

  2. 将实例变量作为 ex. true 然后将值更改为 false 并在整个类中使用该变量?或者,在使用实例变量方面,您应该在概念上避免这种情况吗?

采纳答案by Stephen C

If you create an instance variable in a class, is the default value true or false until otherwise changed?

如果你在一个类中创建一个实例变量,默认值是真还是假,直到另外改变?

The default value is false. (JLS 4.12.5)

默认值为false。( JLS 4.12.5)

Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class?

将实例变量作为 ex. true 然后将值更改为 false 并在整个类中使用该变量?

I assume you mean, is it good practice to define your boolean instance variables so that you can rely on default initialization.

我假设您的意思是,定义布尔实例变量以便您可以依赖默认初始化是一种好习惯。

The answer is: No. It is not good practice:

答案是:不。这不是一个好习惯:

  • It is good practiceto define the instance variables so that they make sense to the reader of the code:

        // Good (probably)
        private boolean isValid = true;
    
        // Bad (probably)
        private boolean isNotValid;  // so that I can rely on default init
    

    (Now, it maymake your code easier to understand if the variable is negated ... but the point is that you should decide based on what makes the code easy to understand... not on based exploiting default initialization.)

  • It is bad practiceto spend time worrying about performance issues at this level of granularity. The chances are that performance benefit of avoiding an explicit initialization is insignificant.

  • 定义实例变量是一种很好的做法,以便它们对代码的读者有意义:

        // Good (probably)
        private boolean isValid = true;
    
        // Bad (probably)
        private boolean isNotValid;  // so that I can rely on default init
    

    (现在,如果变量被否定,它可能会让你的代码更容易理解......但关键是你应该根据使代码易于理解的原因来决定......而不是基于利用默认初始化。)

  • 在这种粒度级别上花时间担心性能问题是不好的做法。避免显式初始化的性能优势很可能是微不足道的。

回答by Abimaran Kugathasan

if you create an instance variable in a class, is the default value true or false until otherwise changed?

如果您在类中创建实例变量,默认值是真还是假,除非另有更改?

If the variable is type of boolean, then default value is false, primitives variables default value id 0, object variable/reference variable default value is null

如果变量是布尔类型,则默认值为 false,基元变量默认值为 id 0,对象变量/引用变量默认值为null

Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class?

将实例变量作为 ex. true 然后将值更改为 false 并在整个类中使用该变量?

You can do this, it's depends on your requirement.

你可以这样做,这取决于你的要求。

回答by Suresh Atta

if you create an instance variable in a class, is the default value true or false until otherwise changed?

如果您在类中创建实例变量,默认值是真还是假,除非另有更改?

If the member is primitive , then false. If it is a wrapper, then null

如果成员是原始成员,则false. 如果它是一个包装器,那么null

Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class? Or is that something you should conceptually avoid in terms of using instance variables?

将实例变量作为 ex. true 然后将值更改为 false 并在整个类中使用该变量?或者,在使用实例变量方面,您应该在概念上避免这种情况吗?

Setting true or false will be depends on your class context. No issues with it at all.

设置 true 或 false 将取决于您的类上下文。完全没有问题。

For ex: You are creating an customer Object and having an instance memeber isActive. If your design allows that all customers by default active then yo are right.

例如:您正在创建一个客户对象并拥有一个实例成员 isActive。如果您的设计允许默认情况下所有客户都处于活动状态,那么您是对的。

回答by trutheality

JLS 4.12.5

JLS 4.12.5

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

    • For type byte, the default value is zero, that is, the value of (byte)0.

    • For type short, the default value is zero, that is, the value of (short)0.

    • For type int, the default value is zero, that is, 0.

    • For type long, the default value is zero, that is, 0L.

    • For type float, the default value is positive zero, that is, 0.0f.

    • For type double, the default value is positive zero, that is, 0.0d.

    • For type char, the default value is the null character, that is, '\u0000'.

    • For type boolean, the default value is false.

    • For all reference types (§4.3), the default value is null.

  • 每个类变量、实例变量或数组组件在创建时都使用默认值进行初始化(第 15.9 节、第 15.10 节):

    • 对于 type byte,默认值为零,即 的值(byte)0

    • 对于 type short,默认值为零,即 的值(short)0

    • 对于 type int,默认值为零,即0

    • 对于 type long,默认值为零,即0L

    • 对于 type float,默认值为正零,即0.0f

    • 对于 type double,默认值为正零,即0.0d

    • 对于 type char,默认值为空字符,即'\u0000'.

    • 对于类型boolean,默认值为false

    • 对于所有引用类型(第 4.3 节),默认值为null

回答by Benjamin

when you crate a variable of boolean   
public class check  {
static boolean  b;
    public static void main(String args[]) {
            System.out.print("The Default Value of Boolean is="+b);
     }
   }

enter image description here

在此处输入图片说明