aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Question Doubt, 61:69 from K&B CD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Question Doubt, 61:69 from K&B CD " Watch "Mock Exam Question Doubt, 61:69 from K&B CD " New topic
Author

Mock Exam Question Doubt, 61:69 from K&B CD

Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
ok this question's answer is compilation fails, which is fine but it doesn't mention the second problem with the code:

class Fizz {
int x = 5;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1, f2);
if (f1==f3) {
f1=f2;
}
System.out.println(f2==f3+" "+(f2.x==f3.x));//NAUGHTY NAUGHTY!
}

static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;
}

}

Yes the line marked Naughty Naughty is the second hidden error.
f2==f3 resolves to a boolean except it needs to be in brackets!
(f2==f3). Just in case you see a modded version of this in the exam.

so... Naughty Naughty Smackey Bottom!
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
I believe the underlying problem is in string concatenation!!!

let me just try some in eclipse!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Robert,

Great tip, thanks!

However, the actual cause of the compile-time error is perhaps more subtle than what it first seems to be. Notice that this similar code fragment does compile perfectly:

whereas this does not:

Can you figure out why?


Edit: Ah, the post order got messed up when I accidentally deleted my post. Just for the record, this was supposed to appear before Robert's above reply. Apologies for any confusion caused.
[ November 05, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
ahh I was just going to say, how did I manage to respond before you asked the question LOL!

Ok this problem is interesting, if I try a new Integer, Boolean etc.., it works ok ,but any of my own made objects dont!

I dont know if they are overriding something in the API or something!

It might be related to how the "equation"/"operation" evaluates when you try to concatenate it. and how the compiler checks it? in which case other compilers may let it though?

very interested to find out, but off home now, will check again in an hour or so!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Actually, Integer and Boolean will not work either... at least not as the type of f2:

Here's a clue to the real answer: it does compile when f2 is a CharSequence reference:

Aside from CharSequence, the only other types for f2 that will work are Object (as already mentioned), Comparable, and java.io.Serializable.

More food for thought: for the code fragments that compile successfully, what are the two operands of the first == operator in the println statement? (Ponder this question deeply, and you'll find enlightenment in due course. )
[ November 05, 2007: Message edited by: Kelvin Lim ]
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
hmmm at work new Boolean(false) and new Integer(5) worked ok but i cant repeat it here... well that seems to make more sense I'll look into it when I have a spare min!!!
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
String Works
CharSequence (Inferface) works which is implemented by a string
Object works


Is it because Print takes a String, or an Object? though that doesn't explain Serializable...

the Two Operands are just Fizzes that extend object. Although if you enclose the comparison in brackets then it resolves to a false which will fit in a String?

your gonna need to shed some light on this as I am running out of ideas....!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Robert Elbourn:
the Two Operands are just Fizzes that extend object.

Are you sure about this? Think about the precedence of the == operator compared to the + operator.

What are the two operands of == in this expression? "8==3+2+(1+2)"
tarik el berrak
Greenhorn

Joined: Oct 06, 2006
Posts: 29
the other compile error is
if (f1==f3) {
f1=f2;
}


SCJP 1.5<br />New Target (SCWCD 5)
tarik el berrak
Greenhorn

Joined: Oct 06, 2006
Posts: 29
the other compile error is
if (f1==f3) {
f1=f2;
}
since f1 is final, it can't be re-assigned another object.
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
yup was aware of the f1=f2 issue however was looking at the 2nd unreported issue
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
Originally posted by Kelvin Lim:
What are the two operands of == in this expression? "8==3+2+(1+2)"[/QB]
ahhhhhh

in this case the right hand side is the result of 3+2+(1+2)
which is the result of 3+2+(3)
which is the result of 3+5
which is the result of 8
where 8==3+2+(1+2) returns true

HENCE


System.out.println(f2==f3+" "+(5+9)); is the same as:

Fizz == Fizz + 14

Fizz + 14 is an invalid assignment hence the quotes to change the order of assignment which goes from right to left (not the other way around)
I had another question I failed on from the web today a bit like that and it still didn't click until now!!


that must be right, if so then
treasure discovered is greater than treasure hidden, thanks!!!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Robert Elbourn:
Fizz + 14 is an invalid assignment hence the quotes to change the order of assignment which goes from right to left (not the other way around)
I had another question I failed on from the web today a bit like that and it still didn't click until now!!

Good, you're getting very close! But this still doesn't explain why "Object f2 = new Object()" works and "Fizz f2 = new Fizz()" does not (as per my original post in this thread).

Notice that in the expression 'f2==f3+" "+(5+9)', the bolded + operators here are actually the string concatenation operators instead of arithmetic addition. Why? Because both of them have a String as one of their operands. There's no problem with applying the string concatenation operator to any mixture of primitives and/or objects; when applied to a non-String object, it will use the return value of the object's toString() method. All Java objects have a toString() method, because this is a method of the Object class.

So f3+" "+(5+9) will actually compile fine for any Java object f3. Why, then, does 'f2==f3+" "+(5+9)' fail to compile when f2 is a Fizz...?
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

The two scenarios boil down to this

<a Fizz> == <a String>

and

< an Object> == <a String>


In one case you are trying to compare a string with an object ... a String 'is a' object so they might be equivalent.

In the second case you are trying to compare an object derived from object with a String, they can't possibly refer to
the same thing and the compiler knows this and tries to help you out .

Java lang spec ....

"
A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion ...
"


i.e you can cast between String and Object as they may be related (i.e. the object could a String in disguise ;-) ) but a Fizz and String obviously you can't i.e. the compiler can see this can never work.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
thanks! forgot to answer that bit of the question!!!
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Yup, Chris's explanation is exactly right. This is an interesting example, because while Robert's initial post essentially pointed out the logical error in the program (the == comparison was probably not doing what the programmer intended), the syntax error was caused by a rather different issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Exam Question Doubt, 61:69 from K&B CD
 
Similar Threads
Final
explain this code
help me in understanding this program
chapter 3 self test question 5, K&B
This code fragment is quite confusing