3 import java.util.scanner; 4 5 public class MainProgram public static void main
ID: 3909881 • Letter: 3
Question
3 import java.util.scanner; 4 5 public class MainProgram public static void main(String[] args) ( 7 8 Scanner input = new Scanner(System.in); System.out.print("Enter Word A "); String strA-input.nextLine(); System.out.print("Enter Word B:"); string strB = input.next Line(); 10 12 13 14 15 16 17 (strA strB)); " + -- System . out . println("Result: 2 Console 3 terminated> MainProgram [lava Application] CProgram FilesJavaljdk1.8.0.131binjavaw.exe Oun 20, 2018, 743:09 PM) Enter Word A: Java Enter Word B: Java Result: false The Java program above reads two equivalent words (Java) from the console and compares them to see if they're equal. Explain why the result is "false"Explanation / Answer
As you see, we are trying to compare two strings which are entered by the user using '=='.
Let me tell you that we can use '==' for primitive data type comparisions like int, long etc. or for object references.
When you are trying to compare two strings you can use strA.equals(strB).This would return true.
Here basically '==' checks if both objects point to the same memory location. In your case they both are different and are not pointing to the same memory location. As a reason it returns false.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.