Skip to content

Instantly share code, notes, and snippets.

@willh
Created March 18, 2014 22:13
Show Gist options
  • Save willh/9630923 to your computer and use it in GitHub Desktop.
Save willh/9630923 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import static org.junit.Assert.fail;
public class StringComparisonTest {
@Test
public void testCompileTimeVariable() {
String testCompileTimeString = "compileTime";
if ("compileTime" == testCompileTimeString) {
System.out.print("equal");
} else {
fail("unequal");
}
}
@Test
public void testLiteralValue() {
if ("literal" == "literal") {
System.out.print("equal");
} else {
fail("unequal");
}
}
@Test
public void testStatementVariable() {
// defined as "statement" at compile time
String resultOfConcatenation = "state" + "ment";
if ("statement" == resultOfConcatenation) {
System.out.print("equal");
} else {
fail("unequal");
}
}
@Test
public void testArrayItemVariable() {
String[] stringArray = {"hello", "world"};
if ("hello" == stringArray[0]) {
System.out.print("equal");
} else {
fail("unequal");
}
}
@Test
public void testNewStringAtRuntime() {
String newString = new String("newString");
if ("newString" == newString) {
System.out.print("equal");
} else {
fail("unequal");
}
}
@Test
public void testCharArrayToStringAtRuntimeVariable() {
char[] charArray = {'h', 'e', 'l', 'l', 'o'};
String charSequence = "";
for (int i = 0; i < charArray.length; i++) {
// only becomes "hello" at runtime
charSequence += charArray[i];
}
if ("hello" == charSequence) {
System.out.print("equal");
} else {
fail("unequal");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment