Re: Java compatibility issues (WAS: MF having issues?)
- From: "Oliver Wong" <owong@xxxxxxxxxxxxxx>
- Date: Tue, 07 Mar 2006 19:40:23 GMT
[post re-ordered]
"Sergey Kashyrin" <ska@xxxxxxxxxxx> wrote in message news:i0lPf.78$zF4.184390@xxxxxxxxxxxxxxxxx
String a = "123";
String b = "123";
if (a == b) {
/*You SHOULD reach this point*/
}
I believe (but am not sure), that the JLS (Java Language Specification) REQUIRES that this optimization be done (i.e. if you write a compiler which does NOT perform this optimization, then you are not legally allowed to call it a "Java compiler").
It's just implementation.
I'm not sure about optimization requiremens and as I can recall in java 1.1 I saw different results on a different platforms.
Might one of those platforms been the MS JVM versus Sun's JVM? Microsoft was illegally using the "Java" trademark. Sun permits people to use this trademark AS LONG AS your product conforms to their specifications and standards document where applicable. MS JVM's didn't follow the Sun's standards for JVMs, and so they weren't legally allow to call it a "JVM", but they did anyway.
I looked it up, and found the entry I was thinking of. Section 3.10.5 of the 3rd edition of the JLS:
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5
<quote>
Thus, the test program consisting of the compilation unit (§7.3):
package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }
and the compilation unit:
package other;
public class Other { static String hello = "Hello"; }
produces the output:
true true true true false true
This example illustrates six points:
* Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
* Literal strings within different classes in the same package represent references to the same String object.
* Literal strings within different classes in different packages likewise represent references to the same String object.
* Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
* Strings computed by concatenation at run time are newly created and therefore distinct.
</quote>
So this optimization is indeed a requirement before you can call your platform a "Java platform".
- Oliver
.
- Follow-Ups:
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: Java compatibility issues (WAS: MF having issues?)
- References:
- Re: MF having issues?
- From: Jeff York
- Re: MF having issues?
- From: Frank Swarbrick
- Re: MF having issues?
- From: Jeff York
- Re: MF having issues?
- From: Sergey Kashyrin
- Re: MF having issues?
- From: James J. Gavan
- OT: Java compatibility issues (WAS: MF having issues?)
- From: Oliver Wong
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: Java compatibility issues (WAS: MF having issues?)
- From: James J. Gavan
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: Java compatibility issues (WAS: MF having issues?)
- From: James J. Gavan
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Oliver Wong
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Oliver Wong
- Re: Java compatibility issues (WAS: MF having issues?)
- From: Sergey Kashyrin
- Re: MF having issues?
- Prev by Date: Re: Java compatibility issues (WAS: MF having issues?)
- Next by Date: Re: Java compatibility issues (WAS: MF having issues?)
- Previous by thread: Re: Java compatibility issues (WAS: MF having issues?)
- Next by thread: Re: Java compatibility issues (WAS: MF having issues?)
- Index(es):
Relevant Pages
|