+ 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
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.