FileDocCategorySizeDatePackage
TwoStrings.javaAPI DocExample834Mon Apr 09 09:25:12 BST 2001None

TwoStrings

public class TwoStrings extends Object
See if Strings are shared in Java. The thing is, it depends on whether you say "string" or new String("mystring");

Fields Summary
Constructors Summary
Methods Summary
public static voidcompare(java.lang.String one, java.lang.String two)

		System.out.println("Comparing...");
		if (one == two) {
			System.out.println("Strings are shared: " +
				one.hashCode() + ", " + two.hashCode());
		} else if (one.equals(two)) {
			System.out.println("At least the strings are equal: " +
				one.hashCode() + ", " + two.hashCode());
			System.out.println((Object)one);
			System.out.println(two);
		} else System.out.println("This is rather distressing, sir.");
		System.out.println();
	
public static voidmain(java.lang.String[] argv)

		String one = "A String";
		String two = "A String";
		String three = new String(one);
		compare(one, two);
		compare(two, three);