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

后端 未结 6 1994
醉梦人生
醉梦人生 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: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);
    

提交回复
热议问题