Why this condition returns false?
String a = new Scanner(System.in).nextLine(); String b = "pizza"; System.out.println(a == b); why in java this condition is false(given you also input "pizza" for the scanner), but if you assign 'a' a literal "pizza" the condition becomes true?
12/18/2018 1:03:19 AMMind To Machine 💻🕆
11 AnswersNew Answer
The compiler will turn any literals with the same value into the same object, so each string has to be kept only once. This is called string interning. If you force the creation of new objects (with `new`), then the comparison will return false as others have pointed out. You can force non-literal strings to be interned with `my_string.intern()`.
Mind To Machine 🤖😎 When you use the "new" keyword, you are creating a new object. This creates a new reference in memory. That's why in your code it returns false since you have 2 different objects. The equals() method on the other hand compares the characters of both strings one by one. With regards to string literals where you assign strings the same value, java checks if the string already exists somewhere in memory. If it finds one, it returns a reference to that string. Therefore, two string literals assigned the value of "pizza" refer to a single reference (memory location) that is why it returns true.
Therefore: String a = "pizza"; //mem1 String b = "pizza"; //mem1 String c = new String("pizza"); //mem3 a == b - true because same memory location a == c - false because different memory locations a.equals(b) - true because same chars a.equals(C) - true because same chars Shoutout to any java experts out there. Please correct me if I'm wrong. Michal Danijel Ivanović
This post can help you Mind To Machine 🤖😎 https://www.sololearn.com/Discuss/647021/?ref=app
Lambda_Driver I have nothing to add. Maybe only a keyword "String pool", can be useful on interview 😆
The case is with the new statement. It allocates memory in the heap. Therefore any equality checking on such object would return false, except it done with equals() method. If you want to get true then you should compare values using equals() So your code would look like this. System.out.println(a.equals(b));
== tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically "equal") Try a.equlas("pizza")
Igor Kostrikin but String a = "pizza"; String b = "pizza"; System.out.println(a == b); also prints true
Michael there is no comparison between String and Scanner objects, if i tried to initialize String 'a' with a Scanner a compile time exception will be thrown. and you wouldn't even see the output as the code will not be compiled 😉
Diego Acero do you know the logical difference behind using the "new" keyword vs just assigning the strings the same value ?
Mind To Machine 🤖😎 You’re forgetting to include the “new” keyword. Assigning “a” a literal “pizza” in the following code prints false. String a = new String("pizza"); String b = new String("pizza"); System.out.println(a == b); // prints false