/* Check the String class The difference between two comparisons: == and equals() method */ import java.util.Scanner; public class Compare { public static void main( String[] args ) { String myName="Chu"; String yourName; Scanner input = new Scanner(System.in); System.out.print("My last name is Chu.\nEnter your last name: "); yourName = input.next(); System.out.println("\nCompare with == operation: "); if(myName == yourName) System.out.println("same"); else System.out.println("Different"); System.out.println("\nCompare with a method, equals: "); if(myName.equals(yourName)) System.out.println("Same"); else System.out.println("Different");
if(myName == "Chu") //It is true for this case. System.out.println("same"); else System.out.println("Different");
} }