20

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

String email1 = "[email protected]";
String email2 = "[email protected]";
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'm asked to declare isMatch as false when on the next line i am comparing the email addresses and assigning the value to isMatch.
I've tried the following code which seems to work just the same:

String email1 = "[email protected]";
String email2 = "[email protected]";
Boolean isMatch;

isMatch = email1.equals (email2);

if (isMatch == true){
    System.out.println("Emails match");
}
else{
    System.out.println("Emails don't match");
}

On the course it doesn't explain why I'm declaring isMatch as false first. Is there a reason why I must declare isMatch as false before comparing the email addresses?

6 Answers 6

21

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Thats the first time i've seen the keyword "final". I'll probably get to that later in the course. I'm still on some really basic stuff which might be whyi'm asked to write it in that way, but being new to Java i have no way of knowing right from wrong at this stage. Thanks for the suggestion though.
Introducing "final" is a helpful reminder to someone reading the code that the specific item isn't going to change as execution continues. I also liked replacing if(isMatch == true) with if(isMatch) as its then far more obvious that its a true/ false branch in the code
10

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.

1 Comment

Thank you Levon for explaining so i can easily understand. I plan on learning other languages eventually so i'll follow your advice and initialize my variables so i get used to doing it.
2

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.

Comments

1

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);

3 Comments

Thanks i didn't think of doing that. I have done that with int and String so i probably should have thought of it.
@user1762031. Yeah. If you take a look at Effective Java - Item#49. It clearly states to use primitive rather than boxed primitive.
Thanks Rohit. I'll have a look.
0

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.

1 Comment

Thanks for the link, that will come in handy i'm sure.
0

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){
        ......
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.