Wednesday, December 28, 2016

Equal strings aren't equal (==) in Java?







Hi guys, why is this Java code not working correctly (the if statement is always being read as False, invariably giving an output of 4 for any input, including Anish & "Anish")?



import java.util.Scanner;  
public class lkjlj {
public static void main(String args []) {
Scanner Pillai = new Scanner(System.in);
System.out.println("Enter Your name");
String nabeel = Pillai.nextLine();
System.out.println(nabeel);
if (nabeel == "Anish") {
System.out.println("Your Age is 6");
} else {
System.out.println("Your age is 4");
}
}
}

No comments:

Post a Comment