hi all,

I came through this question and was confused as to what and why the ans should be??

String str = "10";
int a = new Interger (10);

which of the following is legal: - choose any 3.

a) str += a;
b) boolean b = str == a;
c) boolean b = str == a.toString();
d) boolean b = str == a + "";

i am confused with a and b.

Recommended Answers

If you have to choose three, it's an invalid question. Only two of those are legal operations.

(And one of those two is only valid for testing object reference equality)

Jump to Post

All 4 Replies

If you have to choose three, it's an invalid question. Only two of those are legal operations.

(And one of those two is only valid for testing object reference equality)

If you have to choose three, it's an invalid question. Only two of those are legal operations.

(And one of those two is only valid for testing object reference equality)

well the question is which of them are LEGAL STATEMENTS. According to a engine i use the answer says option b is not valid. i basically want to know why option a is valid and option b is not valid.

a is creating a new String object, with value "1010"
in b you are using == to compare two Objects. you can not compare objects on equality using the '==' comparator. this will determine whether the two objects you've given have the same reference, but this can only be done with objects of the same type (or they would have to be related. for instance, you can use '==' between a primitive int and it's Wrapper class Integer).

since in the other two checks you're always looking for the same reference between two String objects, they are legal statements, but in b, you are looking for the same reference for a String object and an Integer object, which the compiler knows is impossible.

I can read just fine kesh1000. The "engine" you use should have also noted that 'c' is not a legal statement. An int is not an object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.