Why this condition returns false? | SoloLearn: Learn to code for FREE!

+2

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 AM

Mind To Machine 💻🕆

11 Answers

New Answer

+1

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()`.

+12

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.

+12

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ć

+6

This post can help you Mind To Machine 🤖😎 https://www.sololearn.com/Discuss/647021/?ref=app

+4

Lambda_Driver I have nothing to add. Maybe only a keyword "String pool", can be useful on interview 😆

+3

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));

+1

== tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically "equal") Try a.equlas("pizza")

+1

Igor Kostrikin but String a = "pizza"; String b = "pizza"; System.out.println(a == b); also prints true

+1

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 😉

0

Diego Acero do you know the logical difference behind using the "new" keyword vs just assigning the strings the same value ?

-2

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