Forums Register Login

Got good Java eyes?

+Pie Number of slices to send: Send
okay, not a real question, but cool stuff i found in a java book.

the following code compiles without errors:



but why?

oviously it's lame to try it out in your IDE. furthermore it's lame to post the solution in the thread, instead you can post the time you needed...

almost everyone i showed this code needed a couple of minutes, even our senior java architects. guess our brain is kind of biased...

:-)

enjoy,
jan
+Pie Number of slices to send: Send
Hi,

Nice and smart stuff..took me about 90 secs to find out the trick..

True .. our minds are biased..

Cheers!!
+Pie Number of slices to send: Send
Wow very nice ..
Took 3 minutes ( approx ) to figure out ..
[ November 08, 2005: Message edited by: Srinivasa Raghavan ]
+Pie Number of slices to send: Send
[Jan]: cool stuff i found in a java book.

Credit where it's due: I believe the book referred to here is Java Puzzlers by Joshua Bloch and Neal Gafter. Highly recommended. Sadly the authors did not actually use the Big Moose Saloon URL in their example, instead choosing to reference their current employer. But we can't have everything I suppose.
+Pie Number of slices to send: Send
actually i found it in a german java book "java ist auch eine insel" ("java is an island, too")...

:-)
+Pie Number of slices to send: Send
 

Originally posted by Jan Groth:
actually i found it in a german java book "java ist auch eine insel" ("java is an island, too")...

:-)



Yeah, Germans are good at combining several tens of words while writing German and then splitting them apart in their mind while reading.



English-speakers don't do that for a full-time job.

+Pie Number of slices to send: Send
 

ooviously it's lame to try it out in your IDE. furthermore it's lame to post the solution in the thread, instead you can post the time you needed...


About 15 seconds. It may be a bit easier for those who spend a lot of time dinking around in the SCJP forum, where the discussion revolves around esoteric constructs like this.

Nice puzzle!
+Pie Number of slices to send: Send
Nice. I passed it around and somebody else got it in a minute or so.
+Pie Number of slices to send: Send
COuld u please explain why it worked ??
+Pie Number of slices to send: Send
 

COuld u please explain why it worked ??



Don't scroll down if you don't want to know the answer quite yet....























































It's a label followed by a comment.

[ November 08, 2005: Message edited by: Bear Bibeault ]
[ November 08, 2005: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
The question existed well before Java Puzzlers did.
http://jqa.tmorris.net/GetQAndA.action?qids=78&showAnswers=true
+Pie Number of slices to send: Send
OK, here's a question for the old-timers:

I could swear that at one time, only loops could be labeled in Java. Was this ever true, proposed to be true, reputed to be true, true of a certain implementation, or otherwise non-false in any situation?
+Pie Number of slices to send: Send
Hi
A nice one infact....Didnt strike at the first glance but when u said its lame to send the solution ...got that in nearly 15 secs...
Good one,
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
>I could swear that at one time, only loops could be labeled in Java.

Any statement can be labelled.

You can use break with any label
You can only use continue with a label on a loop statement.
That may be where the confusion comes from.

There is an example here:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.15
+Pie Number of slices to send: Send
 

Originally posted by Stefan Evans:
>I could swear that at one time, only loops could be labeled in Java.

Any statement can be labelled.

You can use break with any label
You can only use continue with a label on a loop statement.
That may be where the confusion comes from.

There is an example here:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.15



"at one time"

nice tricky thing, and it kinda shames me (SCJP 1.5) couldn't get the answer that easy cheers anyway
( there's always a beginning )
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1299 times.
Similar Threads
errors in java program
Passed the Exam with 71%
Head First EJB Errata Link
Memory Leaks
recommend me a book to learn enterprice edition
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:22:57.