• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trick or Treat?

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

special question for today (Java 5):

What happens if you try to compile and run the following code today?


today: see when posted




A output: Trick
B output: Treat
C compiles but does not give an output
D throws an Exception
E does not compile









Yours,
Bu.

---
Don't serialize cats tonight.
 
Gareth Western
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trick?
 
Sreekanth Chinthalapalli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints Trick. The main Idea of the question is to check Throwable is instanceof Serializable interface or not.
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will print Nothing.Because Calender.MONTH value is 2 always and not equal to Calender.OCTOBER which is 9,So the method isHalloween(Calendar cal) will always return false ,So the result.

If I am wrong suggest me.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,

indeed you're wrong
the line of code was:
if (cal.get(Calendar.MONTH) != Calendar.OCTOBER)
and the method called, get has the signature

int java.util.Calendar( int field)

and if you don't want to look up in the Calendar API what integer now actually stands for the field "month", use a constant instead.
The method does not return the number of the constant, but what is written in the field "month".

And I compared it also not with an integer (would be nine for october, as the months start with zero. Also the Maya used zero based dates by the way).

And as I don't want to confuse somebody, I'd rather use the constant Calendar.OCTOBER than the integer 9 directly. Sooner or later somebody will think, Halloween is in September then.


To all the others:
At least nobody stepped into the trap:"Hey! He's instantianting Throwable! Just by the name ending in "-able" you can guess it is an interface!"

Output yesterday: Trick
today, no output.

class Throwable is Serializable. So you can serialize a RuntimeException, store it to disk and send it by mail to everyone who doesn't have one.
;-)

Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic