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

I just started learning Java. In the online course that I am following, I will be asked to enter the following code:

String email1 = "meme@me.coh"; String email2 = "meme@me.com"; Boolean isMatch = false; isMatch = email1.equals (email2); if (isMatch == true){ System.out.println("Emails match"); } else{ System.out.println("Emails don't match"); } 

I donโ€™t understand why I am asked to declare isMatch as false when on the next line I am comparing email addresses and assigning isMatch .
I tried the following code, which seems to work the same way:

 String email1 = "meme@me.coh"; String email2 = "meme@me.com"; Boolean isMatch; isMatch = email1.equals (email2); if (isMatch == true){ System.out.println("Emails match"); } else{ System.out.println("Emails don't match"); } 

In the course, this does not explain why I first declare isMatch false. Is there a reason I should declare isMatch false, before comparing email addresses?

+8
java variables boolean
source share
6 answers

You don't have to, but some people like to explicitly initialize all the variables (me too). Especially those who program in different languages, you just need to have a rule to always initialize your variables, and not decide for each case / in language.

For example, Java has default values โ€‹โ€‹for Boolean, int, etc. C, on the other hand, does not automatically give initial values, all that happens in memory is what you end up using if you yourself did not assign a value.

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

+7
source share

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

  • use boolean instead of boolean (which may also be null no reason)

  • appoint at the time of announcement:

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

     final boolean isMatch = email1.equals(email2); 

And last but not least:

 if (isMatch == true) 

may be expressed as:

 if (isMatch) 

which displays the isMatch flag isMatch not so useful, nesting it may not affect readability. I suggest finding some of the best courses / tutorials there ...

+10
source share

There is no reason for this. In fact, I would decide to combine declaration and initialization, as in

 final Boolean isMatch = email1.equals (email2); 

using the final keyword so that you cannot change it later (by accident).

+1
source share

First of all, you should not use any of them. You are using a wrapper type, which should rarely be used if you have a primitive type. Therefore, you should use boolean sooner.

In addition, we initialize the boolean variable to false to preserve the original default value, which is false. If you declare it as an instance variable, it will be automatically initialized to false .

But it is entirely up to you whether you assign a default value or not. I prefer to initialize them during the declaration.

But if you assign your variable right away, 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); 
+1
source share

In your example, you do not need. . As a standard programming practice, all variables are referenced inside some block of code, for example, try{} catch(){} , and referenced to an external block, you must first declare variables outside the try block, for example,

This is useful when calling the equals method throws some kind of exception, for example. NullPointerException ;

  boolean isMatch = false; try{ isMatch = email1.equals (email2); }catch(NullPointerException npe){ ..... } System.out.print("Match=="+isMatch); if(isMatch){ ...... } 
+1
source share

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

This is probably either a habit of other languages โ€‹โ€‹that do not guarantee primitive data types by default.

0
source share

All Articles