Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// This program seems to contradict itself! // Things that appear to be equal ar

ID: 3635987 • Letter: #

Question

// This program seems to contradict itself!
// Things that appear to be equal are not.
// can you explain WHY this program
// outputs true or false?

public class IntegerQuiz {

public static void main(String[] args) {
int k = 100;
Integer int1 = k;
Integer int2 = k;
System.out.println("a. " + (int1 == int2));

k = 200;
Integer int3 = k;
Integer int4 = k;
System.out.println("b. " + (int3 == int4));

char c = 'A';
Character char1 = c;
Character char2 = c;
System.out.println("c. " + (char1 == char2));

c = '€';
Character char3 = c;
Character char4 = c;
System.out.println("d. " + (char3 == char4));

String s1="Priyam";
String s3="yam";
String s2="Pri" + s3;
System.out.println("e. s1==s2: " + ("Priyam"=="Pri" + "yam"));
System.out.println("e. s1 equals s2: " + s1.equals(s2));
System.out.println("e. s1==s2: " + (s1==s2));
}
}

Explanation / Answer

Integer is not an variable, but a pointer. The comparing == for small reference values tells you that they are the same, but for larger values tells you they are different. You have to use int3.equal(int4) to get the true comparison. because 100 or 'A' is small enough it gives true, but because the value of 300 or '