我刚刚开始学习Java。在我正在学习的在线课程中,我被要求尝试以下代码:
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");
}
我不明白为什么在下一行比较电子邮件地址并将值赋给isMatch
时,要求我将其声明为false。
我尝试了以下代码,它似乎也能正常工作:
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");
}
在这门课程中,它没有解释为什么我要先将isMatch
声明为false。我在比较电子邮件地址之前必须声明isMatch
为false的原因是什么?