Wednesday, 22 March 2006

Java Quiz

Bonus points to anyone who knows what the results of this will be without running it or looking at the documentation. ;-)

public void testTrue() {
String str = "true";
boolean expected = true;

assertEquals("not what you expected?", expected, Boolean
.getBoolean(str));
}

public void testFalse() {
String str = "false";
boolean expected = false;

assertEquals("not what you expected?", expected, Boolean
.getBoolean(str));
}
For anyone interested, here's the documentation.

3 comments:

  1. isn't reading the documentation cheating? After reading the documentation does anybody else think this function is in the wrong spot?

    ReplyDelete
  2. I agree... and I think that the name sucks wrt what it actually does... It would be good if they just made another method that does that same thing and depreciate getBoolean().

    ReplyDelete
  3. You could probably just deprecate this function, and use Boolean.parseBoolean(System.getProperty(str)).
    I don't really see that you'd need to use this function all that often, and even if you did, then the solution I mentioned would be much more clear to other programmers as to what you were actually trying to accomplish.

    ReplyDelete