'. '

IdentityCheck

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 7: Line 7:
<source lang="java">
<source lang="java">
public static void sayHello() {
public static void sayHello() {
-
return "Hello" + " World!";
+
String s1 = "Hello";
 +
String s2 = " World!";
 +
return s1 + s2;
}
}
</source>
</source>

Revision as of 13:14, 26 March 2009

Is there an externally observable difference between following programs?

public static String sayHello() {
  return "Hello World!";
}
public static void sayHello() {
  String s1 = "Hello";
  String s2 = " World!";
  return s1 + s2;
}

Of course, there is! Just call the method twice:

assert sayHello() == sayHello();

The first version of the method passes the IdentityCheck test. The second does not. There are also other differences in MemoryAllocations as well as StringsUsedInConstantPool.

Personal tools
buy