Java SE 8/11 Programmer II Exam Series: Java Collections Framework

Introduction The collection interfaces are divided into two groups: the most basic interface java.util.Collection, and other collection interfaces based on java.util.Map. In this section, we will cover four main interfaces in the Java Collections Framework: List, Set, Map, and Queue. List You usually use a list when you want an ordered collection that can contain duplicate entries. All […]

Java SE 8/11 Programmer II Exam Series: Generics

Introduction Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods. It enables you to re-use the same code. In this section, we will cover Generic Classes, Generic Interfaces, Generic Methods, and Bounded Type Parameters. Type Parameter Naming Conventions By convention, type parameter names are single, uppercase letters. The most […]

Java SE 8/11 Programmer II: Design Patterns and Principles

Introduction This section covers the OCP Java SE 8 Programmer II exam objectives: design an interface, define functional interfaces, implement polymorphism, create and use singleton classes and immutable classes. Designing an Interface Let’s revise the interface rules in Java 8. An interface may extend another interface, and in doing so it inherits all of the […]

Java SE 8/11 Programmer II: Advanced Class Design

Introduction In this post, we discover the basics of advanced class design for the Java SE 8 Programmer II Exam. We cover the instanceof operator; implement equals, hashCode, and toString; create enumerations and nested classes. Virtual methods Virtual functions are an important part of the run-time polymorphism in object-oriented programming. They are just non-static methods. […]

Item 43: Prefer method references to lambdas

Introduction Method references are more concise than lambdas. If method references are shorter and clearer, use them; otherwise, utilize lambdas. Method references reduce the boilerplate code and thus improves readability. There are four kinds of method references, which are summarized below: Kind Example Reference to a static method ContainingClass::staticMethodName Reference to an instance method of […]

Item 44: Favor the use of standard functional interfaces

If one of the standard functional interfaces does the job, you should generally use it in preference to a purpose-built functional interface This will make your API easier to learn, by reducing its conceptual surface area, and will provide significant interoperability benefits Always annotate your functional interfaces with the @FunctionalInterface annotation @FunctionalInterface annotation is used […]

Item 42: Prefer lambdas to anonymous classes

Anonymous classes were adequate for the classic objected-oriented design patterns requiring function objects. Here’s a code snippet using an anonymous class. // Anonymous class instance as a function object – obsolete! Collections.sort(words, new Comparator<String>() { public int compare(String s1, String s2) { return, s2.length()); } }); Interfaces with a single abstract method are now […]

Item 61: Prefer primitive types to boxed primitives

Applying the == operator to boxed primitives is almost always wrong. The two boxed primitives can have same value and different identities, whereas the primitives have only values. Check the code below: When you mix primitives and boxed primitives in an operation, the boxed primitive is auto-unboxed. If a null object reference is auto-unboxed, you […]

Item 59: Know and use the libraries

The takeaway from this section is “Don’t reinvent the wheel!”. The author explains the pitfalls of most developers fall into by giving random number generation example. Check the library before you try an ad hoc solution. By using a standard library, you take advantage of the knowledge of the experts who wrote it and the […]

Item 58: Prefer for-each loops to traditional for loops

Traditional for loop distracts the programmer because of index variables. It gives you many chances to use the wrong variable. for-each loop hides the iterator or index variable. There are some cases that you do need to use an ordinary for loop. For instance, you want to replace the value of a list while you iterate a […]