The default value of a logical instance is true or false.

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

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

+4
source share
5 answers

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

The default value false. ( JLS 4.12.5 )

ex. true, false ?

, , , .

: . :

  • , :

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

    ( , ... , , ... .)

  • , . , .

+7

JLS 4.12.5

  • , (§15.9, §15.10):

    • byte , (byte)0.

    • short , (short)0.

    • int , 0.

    • long , 0L.

    • float - , 0.0f.

    • double - , 0.0d.

    • char , '\u0000'.

    • boolean false.

    • (§4.3) null.

+4

, true false, ?

, false, id 0, / null

ex. true, false ?

, .

+3

, true false ?

, false. , null

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

The value true or false depends on the context of your class. No problem with him at all.

For example: you create a client object and have an instance of memeber isActive. If your design allows all clients to be active by default, then yo are correct.

+1
source
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

+1
source

Source: https://habr.com/ru/post/1528244/


All Articles