Comparing strings with a logical operator in Java

When comparing two lines, I was taught that we should not use the logical operator (==). We should use String.equals (String) for comparison. However, I see that the following code matches and prints " Hello Friend " with the latest JDK (1.6_23). I tried searching and did not find any link. Why is this happening?

 public class StringComp{ public static void main(String args[]){ String s = "hello"; if(s=="hello"){ System.out.println("Hello Friend"); }else{ System.out.println("No Hello"); } } } 
+3
source share
7 answers

You should not use == because it does something else than you think.

In this case, "hello" is saved ("Reading at the boarding line"), so "matching" means that it is the same as your "insanity."

== checks if two things are EXACTLY the same thing, and not if they have the same content. This is really a big difference, and some random (albeit explicable) “false possibilities” are not grounds for using this method.

Just use equals to compare strings.

An example is provided on this site: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

 String a = new String ("a"); String b = new String ("a"); System.out.println (a == b); 

It returns false, and the following code returns true.

 String a = new String ("a"); String b = new String ("a"); System.out.println (a.equals(b)); 
+10
source

Magic is called internment.

Java interns String literals, and therefore there is a high probability that it will be true:

 String a = "hello"; // "hello" is assigned *and* interned String b = "hello"; // b gets a reference to the interned literal if (a == b) System.out.println("internd."); String c = "hello" + Math.abs(1.0); // result String is not interned String d = "hello" + Math.abs(1.0); // result String is not interned System.out.println(c==d); // prints "false" c = c.intern(); System.out.println(c==d); // prints "false" d = d.intern(); System.out.println(c==d); // prints "true" 
+4
source

The == operator is used to check whether both references are to the same String object, and .equals compares the values.

+2
source

It works in your case (and AFAIK it worked the same way as in earlier JVMs), because s refers to the string literal "hello" . References to strings initialized by literals refer to a literal (which is a global object in the background), rather than a separately created new object. The term for this, like the others mentioned above, is internment. So s and "hello" in your example refer to the same physical object. Consider

 String s1 = "hello"; String s2 = "hello"; String s3 = "hello"; 

All of these lines refer to the same physical object, so comparing "==" between any of them or between any of them and "hello" returns true .

However

 String s4 = new String ("hello"); 

creates a new object, so s4 == "hello" gives false .

+2
source

"The Java virtual machine maintains an internal reference list for interned strings (a pool of unique strings) to avoid duplicating String objects in heap memory. Whenever the JVM loads a string literal from a class file and executes it, it checks to see if this string exists in the internal list or not. If it already exists in the list, it does not create a new String and uses a link to an existing String object. The JVM does this type of check internally for a string literal, but not for a String object that it creates through a key with it is "new." You can force the JVM to perform this type of validation on String objects that are created using the "new" keyword using the String.intern () method. This forces the JVM to check the internal list and use the existing String object if it is already is present.

So, the conclusion is that the JVM supports unique String objects for string literals inside. Programmers do not need to worry about string literals, but they need to worry about String objects that are created using the "new" keyword, and they must use the intern () method to avoid duplicating String objects in heap memory, which in turn improves Java performance. See the next section for more information. "

Link: PreciseJava.com - Optimization Methods in Strings and StringBuffer (How the JVM Works with Strings)

0
source

== compares object references

You can understand this by observing the output in the following code:

 public class StringComp{ public static void main(String args[]){ String s = new String("hello"); if(s=="hello"){ System.out.println("Hello Friend"); }else{ System.out.println("No Hello"); } } } 

This program will print No hello

Since in this code the variable s refers to a new String object, which, in turn, refers to the string literal "hello" from the string pool.

0
source

== compares object references; equals () compares string values.

In your case, you select two String objects with the value "hello", and then compare them with ==. The compiler may decide to optimize and use the same object reference, giving a true result like the one you have. However, this is not a guaranteed behavior - it is much safer to use equals () to compare values.

-1
source

Source: https://habr.com/ru/post/898066/


All Articles