Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageDan 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
Post Follow-up to this messageCatherine Z. Jones and Angelina Jolie Go Lesbian! http://www.CheapVideoBlog.com/PlayM...peg?clip=726648 Mariah Carey and Paris Hilton , Satisfying Her Lesbian Girlfriend! http://www.CheapVideoBlog.com/Player?vid=726648 Mariah Carey and Olsen Twins , Weird Lesbian Sex Act! http://www.CheapVideoBlog.com/player.asp?watch=726648 Angelina Jolie and Sarah M. Gellar Licking Pussy! http://www.CheapVideoBlog.com/Player?q=726648 Mariah Carey and Sarah M. Gellar , Movies Of Crucified Lesbian Slave! http://www.CheapVideoBlog.com/Player.php?q=726648
Post Follow-up to this messageJoshua Cranmer a écrit :
>
> 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>" ?
Post Follow-up to this messageJack wrote:
> Joshua Cranmer a écrit :
>
>
> 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
Post Follow-up to this messageI 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 sing 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: > > > Because you're actually defining a new class with the new Object() {} > syntax (look up anonymous inner classes). >
Post Follow-up to this messageDoug 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.[/color ] 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 sing 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
Post Follow-up to this messageDan 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
Post Follow-up to this messageMaarten Bodewes <maarten.bodewes@xs4all.nl> wrote in news:47e68525$0$14346
$e4fe514c@news.xs4all.nl:
<snip>
> Of course, I make more frequent use of
>
> System.out.println(new Object(){static{{}}}.toString());
>
> since most initialization can be done within a constructor.
<snip>
Eclipse told me:
Cannot define static initializer in inner type new Object(){}
for this code:
package testpack1;
public class InnerStaticInit {
public static void main(String[] args) {
System.out.println(new Object(){static{{}}}.toString());
}
}
According to The Java Language Specification (third edition), paragraph
8.1.3, "Inner classes may not declare static initializers..."
Did I miss the joke?
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.