• Post Reply Bookmark Topic Watch Topic
  • New Topic

Got good Java eyes?  RSS feed

 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

True .. our minds are biased..

Cheers!!
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow very nice ..
Took 3 minutes ( approx ) to figure out ..
[ November 08, 2005: Message edited by: Srinivasa Raghavan ]
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i found it in a german java book "java ist auch eine insel" ("java is an island, too")...

:-)
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice. I passed it around and somebody else got it in a minute or so.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COuld u please explain why it worked ??
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question existed well before Java Puzzlers did.
http://jqa.tmorris.net/GetQAndA.action?qids=78&showAnswers=true
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!