Re: Interesting Java interview question
- From: Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx>
- Date: Thu, 13 Mar 2008 21:01:10 GMT
Jack wrote:
Joshua Cranmer a écrit :
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
.
- Follow-Ups:
- Re: Interesting Java interview question
- From: Doug Dunn
- Re: Interesting Java interview question
- References:
- Re: Interesting Java interview question
- From: Jack
- Re: Interesting Java interview question
- Prev by Date: Re: Interesting Java interview question
- Next by Date: Controlling PDF file through the Web
- Previous by thread: Re: Interesting Java interview question
- Next by thread: Re: Interesting Java interview question
- Index(es):