Thursday, August 26, 2010

SCJP Exam Watch 8 - Obfuscate

The exam creators will tell you that they're forced to jam tons of code into little spaces "because of the exam engine." While that's partially true, they ALSO like to obfuscate.

The following code:

Animal a = new Dog();
Dog d = (Dog)a;
d.doDogStuff();

Can be replaced with this easy-to-read bit of fun:

Animal a = new Dog();
((Dog)a).doDogStuff();

In this case the compiler needs all of those parentheses, otherwise it thinks it's been handed an incomplete statement.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

No comments:

Post a Comment

Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.