What is the correct way to declare a boolean variable in Java?

后端 未结 6 1912
醉梦人生
醉梦人生 2021-02-06 01:09

I have just started learning Java. In the online course I am following, I am asked to try the following code:

String email1 = \"meme@me.coh\";
String email2 = \"         


        
相关标签:
6条回答
  • 2021-02-06 01:44

    As stated by Levon, this is not mandatory as stated in the docs: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    This is probably either an habit from other languages that don't guarantee primitive data types default values.

    0 讨论(0)
  • 2021-02-06 01:49

    First of all, you should use none of them. You are using wrapper type, which should rarely be used in case you have a primitive type. So, you should use boolean rather.

    Further, we initialize the boolean variable to false to hold an initial default value which is false. In case you have declared it as instance variable, it will automatically be initialized to false.

    But, its completely upto you, whether you assign a default value or not. I rather prefer to initialize them at the time of declaration.

    But if you are immediately assigning to your variable, then you can directly assign a value to it, without having to define a default value.

    So, in your case I would use it like this: -

    boolean isMatch = email1.equals (email2);
    
    0 讨论(0)
  • 2021-02-06 01:53

    In your example, You don't need to. As a standard programming practice, all variables being referred to inside some code block, say for example try{} catch(){}, and being referred to outside the block as well, you need to declare the variables outside the try block first e.g.

    This is helpful when your equals method call throws some exception e.g. NullPointerException;

         boolean isMatch = false;
    
         try{
             isMatch = email1.equals (email2);
          }catch(NullPointerException npe){
             .....
          }
          System.out.print("Match=="+isMatch);
          if(isMatch){
            ......
          }
    
    0 讨论(0)
  • 2021-02-06 02:03

    There is no reason to do that. In fact, I would choose to combine declaration and initialization as in

    final Boolean isMatch = email1.equals (email2);
    

    using the final keyword so you can't change it (accidentally) afterwards anymore either.

    0 讨论(0)
  • 2021-02-06 02:06

    Not only there is no need to declare it as false first, I would add few other improvements:

    • use boolean instead of Boolean (which can also be null for no reason)

    • assign during declaration:

      boolean isMatch = email1.equals(email2);
      
    • ...and use final keyword if you can:

      final boolean isMatch = email1.equals(email2);
      

    Last but not least:

    if (isMatch == true)
    

    can be expressed as:

    if (isMatch)
    

    which renders the isMatch flag not that useful, inlining it might not hurt readability. I suggest looking for some better courses/tutorials out there...

    0 讨论(0)
  • 2021-02-06 02:09

    You don't have to, but some people like to explicitly initialize all variables (I do too). Especially those who program in a variety of languages, it's just easier to have the rule of always initializing your variables rather than deciding case-by-case/language-by-language.

    For instance Java has default values for Boolean, int etc .. C on the other hand doesn't automatically give initial values, whatever happens to be in memory is what you end up with unless you assign a value explicitly yourself.

    In your case above, as you discovered, the code works just as well without the initialization, esp since the variable is set in the next line which makes it appear particularly redundant. Sometimes you can combine both of those lines (declaration and initialization - as shown in some of the other posts) and get the best of both approaches, i.e., initialize the your variable with the result of the email1.equals (email2); operation.

    0 讨论(0)
提交回复
热议问题