Why this condition returns false? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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?

18th Dec 2018, 1:03 AM
Mind To Machine šŸ’»šŸ•†
Mind To Machine šŸ’»šŸ•† - avatar
10 Respostas
+ 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()`.
18th Dec 2018, 4:30 AM
Schindlabua
Schindlabua - avatar
+ 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.
18th Dec 2018, 3:15 AM
Lambda_Driver
Lambda_Driver - avatar
+ 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ć
18th Dec 2018, 3:28 AM
Lambda_Driver
Lambda_Driver - avatar
+ 4
Lambda_Driver I have nothing to add. Maybe only a keyword "String pool", can be useful on interview šŸ˜†
18th Dec 2018, 7:52 AM
Michal
Michal - avatar
+ 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));
18th Dec 2018, 1:19 PM
Seniru
Seniru - avatar
+ 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")
18th Dec 2018, 1:26 AM
Igor Kostrikin
Igor Kostrikin - avatar
+ 1
Igor Kostrikin but String a = "pizza"; String b = "pizza"; System.out.println(a == b); also prints true
18th Dec 2018, 1:29 AM
Mind To Machine šŸ’»šŸ•†
Mind To Machine šŸ’»šŸ•† - avatar
+ 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 šŸ˜‰
18th Dec 2018, 2:24 PM
Mind To Machine šŸ’»šŸ•†
Mind To Machine šŸ’»šŸ•† - avatar
0
Diego Acero do you know the logical difference behind using the "new" keyword vs just assigning the strings the same value ?
18th Dec 2018, 2:43 AM
Mind To Machine šŸ’»šŸ•†
Mind To Machine šŸ’»šŸ•† - avatar
- 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
18th Dec 2018, 2:00 AM
Diego
Diego - avatar