Interesting Java interview question  
Author Message
Dan Leifker





PostPosted: 2008-2-29 10:35:00 Top

java-programmer, Interesting Java interview question A friend of mine likes to ask this question when interviewing Java
developers. Thought I'd pass it along because it's sort of interesting.


Why does the following compile, and what does the output look like?

System.out.println(new Object(){{{}}}.toString());


He says he has asked this question of several dozen people, and almost
everyone just stares at it in shock. Only one has answered it
correctly. I wish I had his resume...

dleifker
 
Joshua Cranmer





PostPosted: 2008-2-29 11:07:00 Top

java-programmer >> Interesting Java interview question Dan Leifker wrote:
> Why does the following compile, and what does the output look like?
>
> System.out.println(new Object(){{{}}}.toString());

I would say that the output depends on some unprovided code. For
example, if the following file were used:

public class InterviewQuestion {
public static void main(String... args) {
System.out.println(new Object(){{{}}}.toString());
}
}

it should print out `InterviewQuestion$1@<hex value>` where hex value
will be the hash code of said object.

What the confusing part is doing is this:
new Object() { // Create a new anonymous class subclassing Object
{ // This is the constructor for the anonymous class
{ // And this is an empty arbitrarily-scoped block
}
}
}

I would say that each pair of braces in that portion of the code is in
decreasing order of banality. Many people know how to create these
anonymous classes, the constructor for said classes was brought up in
c.l.j.p a while back in some other contexts (it can really be some
beautiful code, IMHO), and the arbitrary scoping is little-used AFAICT.
Though the anonymous class constructor is also little-used.

> He says he has asked this question of several dozen people, and almost
> everyone just stares at it in shock. Only one has answered it
> correctly. I wish I had his resume...

Or you could just read the JLS in your spare time. It's not that bad of
a spec, although the discussion of method resolution with respect to
generics has a bit of an eye-glazing ability. I've read far worse...

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Jack





PostPosted: 2008-3-14 5:00:00 Top

java-programmer >> Interesting Java interview question Joshua Cranmer a 閏rit :

>> Why does the following compile, and what does the output look like?
>>
>> System.out.println(new Object(){{{}}}.toString());
>
> I would say that the output depends on some unprovided code. For
> example, if the following file were used:
>
> public class InterviewQuestion {
> public static void main(String... args) {
> System.out.println(new Object(){{{}}}.toString());
> }
> }
>
> it should print out `InterviewQuestion$1@<hex value>` where hex value
> will be the hash code of said object.
>

Why not "Object@<hex value>" ?

 
 
Joshua Cranmer





PostPosted: 2008-3-14 5:01:00 Top

java-programmer >> Interesting Java interview question Jack wrote:
> Joshua Cranmer a 閏rit :
>
>>> Why does the following compile, and what does the output look like?
>>>
>>> System.out.println(new Object(){{{}}}.toString());
>>
>> I would say that the output depends on some unprovided code. For
>> example, if the following file were used:
>>
>> public class InterviewQuestion {
>> public static void main(String... args) {
>> System.out.println(new Object(){{{}}}.toString());
>> }
>> }
>>
>> it should print out `InterviewQuestion$1@<hex value>` where hex value
>> will be the hash code of said object.
>>
>
> Why not "Object@<hex value>" ?


Because you're actually defining a new class with the new Object() {}
syntax (look up anonymous inner classes).

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
 
Doug Dunn





PostPosted: 2008-3-20 18:52:00 Top

java-programmer >> Interesting Java interview question I hate the way this news group has gone to hay. It used to be a good,
active news group. C# advocates are punk for bothering other programmers
in this group. I have prepared www.javaspec.org/wiki/Is_Java_dying%3F in
response to their continual nonsense. Please get a life and leave this
newsgroup alone.

Meanwhile, this appears to me to just be to sets of arbitrary blocks--
the innnermost {{}}-- inside of an anonymous class body. The only
question here is this:

Anonymous classes implicitly subclass the class in which they are declared.

The question is seeking to find out if the reader knows this because in
isolation it is not possible to know what will print.




Joshua Cranmer wrote:
> Jack wrote:
>> Joshua Cranmer a 閏rit :
>>
>>>> Why does the following compile, and what does the output look like?
>>>>
>>>> System.out.println(new Object(){{{}}}.toString());
>>>
>>> I would say that the output depends on some unprovided code. For
>>> example, if the following file were used:
>>>
>>> public class InterviewQuestion {
>>> public static void main(String... args) {
>>> System.out.println(new Object(){{{}}}.toString());
>>> }
>>> }
>>>
>>> it should print out `InterviewQuestion$1@<hex value>` where hex value
>>> will be the hash code of said object.
>>>
>>
>> Why not "Object@<hex value>" ?
>
>
> Because you're actually defining a new class with the new Object() {}
> syntax (look up anonymous inner classes).
>
 
 
Joshua Cranmer





PostPosted: 2008-3-20 21:39:00 Top

java-programmer >> Interesting Java interview question Doug Dunn wrote:
> I hate the way this news group has gone to hay. It used to be a good,
> active news group. C# advocates are punk for bothering other programmers
> in this group. I have prepared www.javaspec.org/wiki/Is_Java_dying%3F in
> response to their continual nonsense. Please get a life and leave this
> newsgroup alone.

Have you looked at the numerous subgroups of c.l.java, e.g.,
c.l.j.programmer? I get ~ 80 - 100 messages/day on that newsgroup, far
from dead.

> Meanwhile, this appears to me to just be to sets of arbitrary blocks--
> the innnermost {{}}-- inside of an anonymous class body. The only
> question here is this:

Not quite.
new Object() { // Anonymous class declaration
{ // Anonymous class constructor declaration
{ // Arbitrary scope creation
}
}
}

You've declared a constructor as well (albeit one identical to the
default constructor).

> Anonymous classes implicitly subclass the class in which they are declared.

Where's the question mark, if it's a question?

This is also an incorrect or at best misleading statement. The anonymous
class that is declared is a subclass of Object, nothing more, nothing
less. Certainly, the output of this snippet does change depending on the
class in which it was declared, but the anonymous class does not extend it.

> The question is seeking to find out if the reader knows this because in
> isolation it is not possible to know what will print.

The question, I think, is trying to identify the following aspects of
Java knowledge:
1. Anonymous classes, esp.
1a. Constructors of said classes
1b. Name of said class
2. Arbitrary scope creation
3. What Object's toString method does

of which 1a and 3 are probably what the interviewer is most interested
in knowing.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
 
Maarten Bodewes





PostPosted: 2008-3-24 0:28:00 Top

java-programmer >> Interesting Java interview question Dan Leifker wrote:
> A friend of mine likes to ask this question when interviewing Java
> developers. Thought I'd pass it along because it's sort of interesting.
>
>
> Why does the following compile, and what does the output look like?
>
> System.out.println(new Object(){{{}}}.toString());
>
>
> He says he has asked this question of several dozen people, and almost
> everyone just stares at it in shock. Only one has answered it
> correctly. I wish I had his resume...
>
Well, that one was pretty simple, and Eclipse already stated that I
should document my empty code block :)

Of course, I make more frequent use of

System.out.println(new Object(){static{{}}}.toString());

since most initialization can be done within a constructor.

Maarten