Why doesn't String.equals check if char [] is equal?

I looked at the source of java.lang.String and noticed that the equals method does not check if char[] supports every line is the same object. Wouldn't that improve the comparison time?

The alleged improvement contained in this rewritten version is:

 public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; /** Begin Optimization **/ if(v1==v2 && i==j){ return true; } /** End Optimization **/ while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } return true; } } return false; } 

I believe that this will improve performance if two strings were obtained using String.substring and possibly even interned strings.

Does anyone know if there is a reason why they decided not to implement it this way?

Update: For those who may not know much about string implementation , there are times when other than the String pool, where two String objects can have the same char [] value, int offset and int count.

Consider the following code:

 String x = "I am a String, yo!"; String y = x.split(" ")[3]; String z = x.substring(7,14); 

You will have this situation: Debugger expressions

It is also obvious that the Strings value-sharing function was eliminated in Java 7u6 to satisfy some benchmarks. Therefore, if you took the time to make your code work at a decent time (or in general) using String.substring () rather than String concatenation, you are SOL.

+6
source share
4 answers

Well, you need to check char[] , offset and count (string length). Since char[] is created only from the String class, the only way for all three of them to be equal is to create a doppelgรคnger to create it. You can force it to do this (for example, new String("why?") ), But this is not a normal use case.

<speculative> I'm not even sure if this will speed things up. The vast majority of the time, the test will not work, which means that it does the extra work without any benefit. This could be compensated for by branch prediction, but in this case, the test passes several times, this will invalidate the guesswork made by predicting this branch, which can actually slow down the work. In other words, if the JVM / CPU tries to optimize the general case, you usually wonโ€™t get anything, and you really hurt yourself in the rare case (this is what you are trying to optimize). If he does not try to optimize this general case, you will damage yourself in most comparisons for the sake of a rather rare set of comparisons. </speculative>

+1
source

In Java 7 ( see this article ), substring() no longer uses the same support array for returned String . you will still need to check each character. Basically, String support for char[] never shared, so you cannot

 this.value == other.value 
0
source

I do not understand this question.
char[] is an internal member of String . If 2 string references are the same (should be, since you should use static strings), char [] will be the same.
But for different cases, why do you expect char[] be the same link? Strings are immutable, and it is not possible for 2 different String objects to reference the same array. Also, it doesn't even make sense to use this conditional check even for a substring.
I did not know about the changes in Java 7 mentioned in one of the answers, but in this case it would be wrong to check the equality of the array. The String object is not only a base array, but also its current offset, length, etc.
Thus, 2 String objects as a result of a substring can be supported by the same char array, but can contain different (sub) strings as content - different offsets in the same char array

0
source

Performing such a check in an array of support characters is likely to be redundant and not required.

There are two instances in which an object of an array of support characters can be identical to objects (since another substring method always creates a new array of support characters).

String literal definition

 String a = "Hello"; a.equals("Hello"); // Backing array of "Hello" string literal // will be same as that of variable a 

In this case, the equals method determines that String is equal in the next line before checking the char array.

 if (this == anObject) { // From String.equals method return true; } 

Using the String constructor String to create another String object

Please note that the following block of code has no practical meaning and can never be executed in real code.

 String a = "Hello; String b = new String(a); a.equals(b); 

Therefore, instead of doing an additional check to determine if the character arrays are the same, we can safely assume that they will always be different if the String objects are different.

0
source

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


All Articles