Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Dan Leifker
02-29-08 02:59 AM


Re: 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

Report this thread to moderator Post Follow-up to this message
Old Post
Joshua Cranmer
02-29-08 02:59 AM



Catherine 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

Report this thread to moderator Post Follow-up to this message
Old Post
Simogati
03-02-08 10:41 PM


Re: Interesting Java interview question
Joshua 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>" ?


Report this thread to moderator Post Follow-up to this message
Old Post
Jack
03-14-08 02:59 AM


Re: Interesting Java interview question
Jack 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

Report this thread to moderator Post Follow-up to this message
Old Post
Joshua Cranmer
03-14-08 02:59 AM


Re: 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 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).
>

Report this thread to moderator Post Follow-up to this message
Old Post
Doug Dunn
03-20-08 12:59 PM


Re: 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.[/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

Report this thread to moderator Post Follow-up to this message
Old Post
Joshua Cranmer
03-20-08 11:59 PM


Re: 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

Report this thread to moderator Post Follow-up to this message
Old Post
Maarten Bodewes
03-23-08 11:59 PM


Re: Interesting Java interview question
Maarten 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?


Report this thread to moderator Post Follow-up to this message
Old Post
Ian Shef
04-02-08 12:02 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 01:11 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.