• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the underlying problem is in string concatenation!!!

let me just try some in eclipse!
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Robert Elbourn
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)"
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the other compile error is
if (f1==f3) {
f1=f2;
}
 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup was aware of the f1=f2 issue however was looking at the 2nd unreported issue
 
Robert Elbourn
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...?
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Robert Elbourn
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! forgot to answer that bit of the question!!!
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic