July 1st, 2018
Reflection in Java
Reflection in Java was always a technique clouded in mystery for me. There aren't many practical applications for reflection, so my personal code never really needed to use it. It also didn't help that people often describe reflection as "hard to learn" and only for "experienced developers." It turns out reflection isn't that complicated, although questions still remain of its usefulness in building applications.
Reflection is the combination of inspecting runtime elements of an application and modifying its runtime structure1. The first piece of this definition is more specifically referred to as type introspection.
May 20th, 2018
Building a Java API with Generics
I've learned a lot about Java over the past few months. I've read books on it, built projects with it at work, and even posting on this website about it! Its obvious when looking at some of my old Java code how far I've progressed (especially since my SaintsXCTF Android app). I decided to pull a bunch of concepts I've learned together and build an API in Java. This discovery goes through the API and my design decisions throughout.
The API I built for this discovery post has two major pieces. The first is a workout API. It has an
Exercise interface along with two concrete classes that implement it -
Ski. I am an avid runner and have been experimenting with nordic skiing so I thought they would be appropriate.
January 16th, 2018
Java 8 Default Method
Java was not originally designed to support multiple inheritance of implemented methods. Because of this, a class could only extend one other class. However, multiple inheritance could be simulated by implementing multiple interfaces. The only catch was the methods in the interface had no body and had to be created in the implementing class.
This design helped avoid multiple inheritance issues, such as the diamond problem. However, using interfaces for APIs is far from perfect.
May 15th, 2018
The Curious Case of Java's Clone Method
A common problem when programming is performing a deep copy of an object or data structure. In many languages this is a complex problem, especially when the object gets large. In Java there are many different ways to perform a deep copy on an object. The original language implementation for copying was the
Cloneable interface. Since
Cloneable was the first method for copying objects in Java, it is important to understand how it works along with all its shortcomings. This is my journey to understand Java’s
Cloneable interface in all its complexity.
If you've ever heard or read about Java’s
Cloneable interface you likely were told the following three words: Don’t Use It. This is good advice, as there are many simpler ways to implement deep copying. Unfortunately there is a lot of code in existence that already uses
Cloneable. To understand how it works, first let’s look at the source code for the interface:
May 13th, 2018
Complexities of Generics and Arrays in Java
Usually when I work in Java generics are easy to reason about. They enforce types on a collection or a class I created. When declaring a class with a generic, an instance of that class can be parameterized with one (or more) element types. A simple example is an
ArrayList, which is implemented with a generic parameter like so:
AbstractList and implements
AbstractList actually implements
List itself, so the definition of
implements <List> is not needed. It is only there for clarity and readability.
January 30th, 2018
Java 8 Optionals
Everyone who has written code in Java (or most imperative and object oriented languages) knows the pain of dealing with
null values are especially painful in Java since they throw a
NullPointerException whenever operations are performed on them. When a
NullPointerException is encountered, the usual fix is to perform a null check similar to the following code snippet:
null has other issues besides littering code with null checks. As explained in a book I read on Java 8,
null doesn't follow the statically typed system that Java uses1. It is an incorrect way to represent the absence of a value.
February 7th, 2018
Java 8 Completable Future
This post reviews Java 8's new
CompletableFuture class, which builds upon the
Future class introduced in Java 51. A
CompletableFuture represents an operation in progress that will contain a value when completed2.
I created a simple API to test it out. The API contains a list of programming languages and information about them. Getting a language from the API takes a long time to complete (simulating a long running network call) so I used
CompletableFuture to deal with the language information once it arrives. In the meantime I can perform other operations.
January 15th, 2018
Java 8 Strategy Design Pattern
Since I started learning Java 8, much of my lambda code is from the stream API or the new
Iterable.forEach() function. While both are great, I've been looking for ways to integrate Java 8 lambdas with other programming patterns. While reading a book on Java 8 today I saw one pattern with a lot of potential: the strategy pattern1.
The strategy design pattern defines a group of algorithms. One of the algorithms in this group is selected for use at runtime2. With a functional interface, a lambda function (algorithm) is passed to the strategy pattern. The functional interface specifies the general algorithm structure while the lambda function implements the details at runtime.
February 8th, 2018
What Is Functional Programming in Java 8?
With all the functional additions in Java 8, I think its important to define what it means for a program to be 'functional.' The book Java 8 In Action tries to supply this definition.
It is hard to explain functional programming with Java since it doesn't follow a pure functional style. Instead Java 8 takes pieces of functional programming and incorporates them into Java's existing imperative and object oriented paradigm.
July 2nd, 2018
Groovy Basics Part I: Concise Syntax
Starting this summer I'll be picking a programming language to look at in-depth every season. The language may be one I want more knowledge on or it may be a completely new language I've never explored. For this summer, the language of choice is Groovy. Groovy is a programming language run on the JVM that can be used alongside Java code1. It supports static and dynamic typing, along with a host of features in hopes to shorten the verbose Java syntax. Groovy is used in a number of different projects such as Grails, Gradle, and Jenkins.
One of the main reasons I want to look at Groovy is my work on a project involving Jenkins at work. I also always had an interest in the language as an alternative to Java. This post looks at some basic features of Groovy that caught my eye after an hour or two of exploring. This post and Part II serve as my first impressions of Groovy. After these two posts I'll dig deeper into Groovy features and use it in Jenkins programs. Now its time to start exploring!
November 10th, 2019
The Basics of Programming Language Garbage Collection
While reading a book on C#, I came across a section about garbage collection. I always knew that programming languages such as Java performed garbage collection, but I never researched how garbage collectors (GCs) worked. The book mentioned that C# uses a tracing GC with generations and marking. These were foreign concepts to me, so I decided to conduct additional research on the topic. This article gives a high-level overview of garbage collectors and the APIs available to interact with them in Java and C#.
May 12th, 2019
Delegate Objects in C#
C# has an object called a delegate which is similar to lambda functions and functional interfaces in Java. A delegate object has a single role - calling a method. This article explores delegates and compares them to similar language constructs in C# and Java.