Advanced Java

So you just took the AP Java test, or you’re a programmer switching to Java, or you just love programming in Java, and you’re looking for some interesting nuances, syntax, built-in libraries, that you can easily take advantage of, and allow you to do amazing things. You’ve come to the right place. Here are some easy to learn and easy to use features of the Java language that you might not have known about.

The Ternary Operator

You might already have heard of this, but it isn’t generally taught in Computer Science curriculums, so I thought I’d put it here just in case.

For more information, check the wiki out!

Labeled break Statements

Did you ever want to leave two levels of loops, without a it looking convoluted and ugly?

For another example, and an example with a labeled continue statement, visit this website.

The finally Block

The finally block after a try block is always called (except with a System.exit(...);).

For more information on the try-catch-finally block, check out the documentation.

enums

You have probably heard about the enum datatype, but did you know that they can have their own pseudo-classes?

For another cool example with planets, check this link out!

Custom Annotations

You probably already know of the @Override annotation, but did you know you could also make (and use) your own?

For more examples, see this or this. To learn how to actually process annotations and their values using reflection, see this article.

The Robot Class

This class allows you to interact with the computer to a whole new level. You can see the screen, control the mouse, press keys, and much more!

For a neat lesson about the Robot Class, check out this tutorial.

Multithreading

You might have already known this, but Java runs on its own virtual machine, and has a pretty cool threading system. It’s pretty self-explanatory, so I’ll whip up a quick example to add up the numbers from 1 to n using multithreading.

If that didn’t make sense, feel free to contact me, or look at this article—albeit in Allman style formatting.

Lambda Functions

Ever since Java 8, Java has had lambda functions implemented, using the -> operator.

If you think that’s pretty cool (and useful), be sure to check out another example or a more in-depth explanation.

Reflection

I already touched on this before in the custom annotations section, but reflection allows your program to parse methods, classes, and other members of programs. Oracle’s tutorial covers this very well, so I’ll just include a quick snippet into what’s possible.

Dynamic Typing

Last but not least, I can’t make a post about cool Java features without including dynamic typing. It’s possible to parse JavaScript natively using Java. This is so bizarre, and I can’t think of any practical use for this, but enjoy nonetheless.

You can read all about this scripting API here.

The End

Well, that’s the show folks, I hope you learned about at least one new feature of the Java language! If you have any lurking questions about these features, or want any more features to be covered, contact me—either by email or in the comment section below!