【发布时间】:2012-10-20 19:43:31
【问题描述】:
我刚刚开始学习 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 时,我被要求将isMatch 声明为假。
我已经尝试了以下代码,它似乎工作起来是一样的:
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 为假。在比较电子邮件地址之前,我必须将 isMatch 声明为 false 有什么原因吗?
【问题讨论】: