Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
SCJP 1.4, SCWCD in process
42
Originally posted by Mike Gershman:
Second, I find it very hard to believe that a working Java programmer would write code that depends on interned strings being equal under ==.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Gershman
SCJP 1.4, SCWCD in process
Originally posted by Mike Gershman:
This would require either that multi-server multi-site applications somehow synchronize their constant pools or that the programmers be be very aware of which classes share a jvm. The latter would in turn make redistributing an application across servers trickier.
What do we gain by writing code that relies on implementation?
In most cases, the strings will be different when the first 4 or 8 byte word is compared by equals(). This is not much different from comparing String references, which are not really simple addresses.
I wonder how often they use == with strings.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
42
It's this part that I don't agree with and snicerely hope is not true. The tech tip specifically statesOriginally posted by Steven Bell:
I'm fairly certain that the JVM spec requires that all Strings be interned so that any two Strings that are equals() are also ==
I infer from this that not all Strings are always intered. If you read a String from a File or over a Socket, will it intern it? If it does do this with every String that gets created, you'll end up with serious performance issues.String literals and string-valued constant expressions are always interned
Sorry, only the first part of my post was replying to you (I wasn't clear either ). The closest I think anyone came to recommending the use of == was Ilja withOriginally posted by Steven Bell:
I would never recommend relying on the == comparison for Strings.
which was only pointing out when you can depend on it, not really a recommendation in my book, but seeing as this is the beginners' forum, I wanted to be specific.Originally posted by Ilja Preuss
Unless he had read the official tech tip on the topic
While it doesn't compare hash codes, it could since it caches the hash code value by inserting this if test:I don't know how often that would help versus hurt, though. If you tend to compare a lot of large unequal Strings that have been put into a hash-based collection at some point, it would pay off. Otherwise it's probably just busy work given that the next comparison is against the length of the two Strings which is faster than comparing the hash codes and probably just as likely to return false for different Strings.Back to Steven's post
The equals method in the String class does not check the hashCode. ... I'm not sure why it would be faster to check the hashCode before doing a comparison as both involve a traversal over the characters in the String.
Originally posted by David Harkness:
If you read a String from a File or over a Socket, will it intern it?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I'm confused - how do you compare strings that are not in the same VM???
What do you mean by "relies on implementation"? It's something that is guaranteed to work by the *specification*, after all.
Mike Gershman
SCJP 1.4, SCWCD in process
Originally posted by Mike Gershman:
Using most kinds of remote access. For instance, accessor methods using RMI.
You could say that you wouldn't use == under those conditions, but what happens when your small-scale application is really successful and needs to be ramped up big time? Do I have to hunt down every dependency on single-jvm residency?
I didn't mean Sun's implementation, I meant not unecessarily relying on the particular hardware/software configuration in your organization. I think that's how we preserve true platform independence.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.