Why is the output show false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
21st Jun 2017, 5:00 PM
Nitin Singhal
Nitin Singhal - avatar
10 Answers
+ 5
A String is an Object. == compares reference in memory for objects. So when you created the new object the memory location is stored somewhere else on the heap. So == shows false because they are not located in the same memory location. To compare by value with Strings use the .equals() method. This will compare for equality character by character. On the other hand, when you write: String s = "test"; String a = "test"; if(s == a) This on the other hand will likely give true. This is because of a JVM optimization Java does with Strings where it will throw all Strings of the same value into the same memory location to avoid creating a new object. While, 'new' is forcing the creation of a new object. This is called the String Constant Pool.
21st Jun 2017, 5:08 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
Hey @Nitin Singhal try using string methods (i.e. equals()) to compare the contents of the two given strings. Remember: Equals() method is case sensitive beacause it compares by using ASCII values. public class Program {     public static void main(String[] args) {     String str1 = "hello";     String str2="Hello";     System.out.println(str1.equals(str2));//false     System.out.println("---Using equalsIgnoreCase ()method---"); //You can ignore case sensitivity of the equals method.   System.out.println(str1.equalsIgnoreCase(str2));          } } Hope it helps.. :)
21st Jun 2017, 6:22 PM
Nitin Kumar
Nitin Kumar - avatar
+ 3
Hey @Milad or @Serena Do you happen to know how reliable the SCP is? As in, could I just use == instead of .equals() as long as I don't create a new String object. Like if I were comparing Strings from different classes too, or Strings from objects of those classes using ==. I'm just asking since == must be a far faster comparison than .equals() so if the SCP is reliable I could just use == anyway.
21st Jun 2017, 5:32 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
In short, String S1 = new String("Hello"); This statement creates a String object in the Heap Memory. String S1 = "Hello"; This statement creates a String literal with value "Hello" in the String Pool. For more understanding, let's take 3 cases. Case 1. String Object and Literal String s1 = "Hello"; String s2 = new String("Hello"); Since s1 and s2 are reference variables, they'd be pointing at their respective memory locations right ? s1 points to String Pool's location and s2 points to Heap Memory location. Now testing if they're equal: if(s1 == s2) would return false, as the reference variables are checked. Where as s2.equals(s1) will return true as equals function checks the individual characters in both the reference variables. An Interesting Fact: "String Literal Pool" is a memory area that contains all the string literals used in the program. Whenever a new literal is encountered, the JRE checks if it exists in the String pool or not. It does not contain duplicate literals. If you create two reference variables with same value "Hello", they'd both point to the same location. Case 2: Two String Literals: Eg: String s1 = "Hello"; String s2 ="Hello"; if(s1==s2) will be true. Case 3: One string object and anonymous literal String s1 = new String("Hello"); if(s1 == "Hello") will be false. In this case , "Hello" creates a literal in the pool which has a different memory location than S1's heap memory. Hope it helps.
21st Jun 2017, 5:13 PM
MiladDev
MiladDev - avatar
+ 2
@Restoring faith Oh gosh ! I thought its the basic that applies in every language , I am deleting that answer :-|
21st Jun 2017, 5:45 PM
Utkαrsh
Utkαrsh - avatar
+ 1
brother first of all, in String object creation, two approaches. one is: String s1="sololearn"; 2nd is: String s2=new String("Java"); now, if we create object as String s="sololearn"; then this type object creation in SCP memory. and if you create object as String s=new String("jaav"); then it's create in HEAP memory. more information, you can see on Java by ratan on YouTube..
21st Jun 2017, 5:10 PM
meherDev
meherDev - avatar
+ 1
😂 Everyone's beaten me to it.
21st Jun 2017, 5:24 PM
Limitless
Limitless - avatar
0
That is very interesting. Will do a bit research
21st Jun 2017, 5:09 PM
Limitless
Limitless - avatar
0
its ok. since you use "new", first string only looks like second. but these values are different by relation.
22nd Jun 2017, 4:10 PM
Elad Kipiani
Elad Kipiani - avatar
- 2
public class Test { public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; System.out.println("s1 == s2 is:" + s1 == s2); } }
31st Jul 2020, 8:54 AM
Sakthi Sudharsini
Sakthi Sudharsini - avatar