+ 2

About basic concept of String.

Hi, I have a question :: I know if we create String Literal, it will store in String Pool Object. Scanner input = new Scanner(System.in) String myName = "Martinus"; String userInput = input.next(); // assume user input is Martinus System.out.print(myName == userInput); // it will return false, why? Are they different Object and not store to String Pool? Can you explain to me? and what about this String name = input.next(); String name2 = input.next(); // assume user input is the same value, return false

14th Jun 2017, 7:00 AM
Martinus Lukas
Martinus Lukas - avatar
1 Antwort
+ 3
instead of myName == userInput use myName.equals (userInput) as == performs reference comparison and return true if both objects point to same memory location. Whereas, the equals() function compares the values in the objects.
14th Jun 2017, 7:35 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar