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?
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...
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);
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){
......
}
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.
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.
Source: Stackoverflow.com