import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Equals { private String s1; private String s2; Equals (String s1, String s2){ this.s1 = s1; this.s2 = s2; } public void testEqualsEquals (String a, String b) { System.out.println("Testing '" +a + "' == '" + b + "' is " + (a == b)); } public void testDotEquals (String a, String b) { System.out.println("Testing '" +a + "' .equals() '" + b + "' is " + (a.equals(b))); } public void testTwo(String msg, String a, String b) { System.out.println("Testing: " + msg); testEqualsEquals(a, b); testDotEquals(a, b); System.out.println(); } public void testEquals(){ int Five1 = 5; int Five2 = 4+1; double doubleFive = 5.0; // primitives if (Five1==Five2) System.out.println("primitive comparison: " + Five1 + "==" + Five2 +"\n"); else System.out.println("primitive comparison: " + Five1 + "!=" + Five2 +"\n"); // surprised? if (Five1==doubleFive) System.out.println("primitive comparison: " + Five1 + "==" + doubleFive +"\n"); else System.out.println("primitive comparison: " + Five1 + "!=" + doubleFive +"\n"); testTwo("Strings from command line parameters" , s1, s2); String h = "hello "; String w = "world"; String hw1 = "hello world"; String hw2 = h+w; testTwo("String literal concatenation", hw1, hw2); // Look at different result here, java has only one copy of literals String s3 = "string"; String s4 = "string"; testTwo("two identical String literals", s3, s4); } /** * @param args: two Strings * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { // intermezzo: where does your input file go inEclipse? /* Scanner scan = new Scanner(new File("wrongPlace")); //Scanner scan = new Scanner(new File("inEq")); int v; for (int i = 0; i<4; i++){ v = scan.nextInt(); System.out.println("v: " +v); } */ // goal of this code: == vs .equals Equals E = new Equals (args[0], args[1]); E.testEquals(); } }