Java code that uses traditional enumerated types is problematic. Java 5 gave us a better alternative in the form of typesafe enums. In this article, I introduce you to enumerated types and typesafe enums, show you how to declare a typesafe enum and …
Java 5 brought generics to the Java language. In this article, I introduce you to generics and discuss generic types, generic methods, generics and type inference, generics controversy, and generics and heap pollution. download
Get the code
Downloa…
Writing programs that work correctly at runtime can be challenging. This is because our assumptions about how our code will behave when executed are often wrong. Using Java’s assertions feature is one way to verify that your programming logic is sou…
You’ve probably encountered situations where you need to associate metadata (data that describes other data) with classes, methods, and/or other application elements. For example, your programming team might need to identify unfinished classes in a …
In my previous Java 101 tutorial, you learned how to better organize your code by declaring reference types (also known as classes and interfaces) as members of other reference types and blocks. I also showed you how to use nesting to avoid name con…
Nested classes are classes that are declared as members of other classes or scopes. Nesting classes is one way to better organize your code. For example, say you have a non-nested class (also known as a top-level class) that stores objects in a resi…
Along with lambdas, Java SE 8 brought method references to the Java language. This tutorial offers a brief overview of method references in Java, then gets you started using them with Java code examples. By the end of the tutorial you will know how …
Before Java SE 8, anonymous classes were typically used to pass functionality to a method. This practice obfuscated source code, making it harder to understand. Java 8 eliminated this problem by introducing lambdas. This tutorial first introduces th…
JDK 1.0 introduced a framework of language features and library types for dealing with exceptions, which are divergences from expected program behavior. The first half of this tutorial covered Java’s basic exception handling capabilities. This secon…
Java exceptions are library types and language features used to represent and deal with program failure. If you’ve wanted to understand how failure is represented in source code, you’ve come to the right place. In addition to an overview of Java exc…