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

后端 未结 6 1906
醉梦人生
醉梦人生 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 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...

提交回复
热议问题