Equality Flashcards

1
Q

What does == mean?

A

It compares primitives and compares if two references point to the same object.

It should not be used to compare Strings but it is legal. Be careful. Pay attention to objects in string pool vs separate objects.

String x = “Hello World”;
String y = “Hello World”;
System.out.println(x == y); // true because they are in pool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does .equals() do and when can it be used?

A

equals() is logical equality IN STRINGS and ARRAY LISTS. It compares the values inside two Strings. For other objects it compares reference equality just like ==. The method can be used to compare any two objects of the same type only String compares inside.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does == do with two Strings?

A

Calling == on String objects will check whether they point to the same object in the
pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does == do with two StringBuilder references?

A

Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does .equals() do with two Strings?

A

Calling equals() on String objects will check whether the sequence of characters is the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does .equals() do with two StringBuilder references?

A
Calling equals() on StringBuilder objects will check
whether they are pointing to the same object rather than looking at the values inside.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is this result?
short s =9;
Integer k = 9;
System.out.print(s==k);

A

True. Autounboxing occurs then the primitive values are compared. One will be promoted if necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is this valid?

System.out.println(Double.parseDouble(“3.0”).equals(Integer.parseInt(“3”)));

A

No, when the operations are performed they produce primitives. The .equals method cannot be used on primitives. This will not compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What's the difference?
if (b2 = !b1 = b2){
if (b2 = !b1){   
if (b2 != b1 = b2){
//if (b2 = b1 != b2){
A

if (b2 = !b1 = b2){//does not compile because of !b1=b2.
!b1 is not a variable that can receive an assignment. It’s a value.

if (b2 = !b1){ // Ok, just assignment and it evaulates to a boolean

if (b2 != b1 = b2){ // compiler error because the first two create a boolean result then it tries to assign a value tp that result.

//if (b2 = b1 != b2){ //ok, assignment then comparison

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Is this valid?
System.out.println(new Double(3.0).equals(new Double(3.0)));
A

Yes because this is comparing two objects. It does not result in the comparison of primitives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is this legal?

static int num = 1;
static int num2 = 1;

System.out.println(num.equals(num2));

A

No, can’t do .equals() on primitives

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What is the result?
System.out.println(new Double(3.0).equals(new Float(3.0)));
A

False. .equals looks at object type and inside

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What is the result?
System.out.println(new Double(3.0).equals(new Double(3.0)));
A

True .equals looks looks at object type and inside

How well did you know this?
1
Not at all
2
3
4
5
Perfectly