Below is the code:
package com.myprograms.test;
import java.util.Scanner;
public class MyProgram {
public static void main(String[] args) {
Scanner sIn = new Scanner(System.in);
String name = "";
System.out.print("Enter the name:");
name = sIn.next();
if (name.equals("somename")) {
System.out.println("Success - Case 1");
} else {
System.out.println("Failed - Case 1");
}
if (name == "somename") {
System.out.println("Success - Case 2");
} else {
System.out.println("Failed - Case 2");
}
sIn.close();
}
}
Below is the output:
Enter the name: somename
Success - Case 1
Failed - Case 2
Here is the question:
Why one input behaves differently for the same condition in Java? Is it Java error?