Java

This category contains all the posts under core java and ways to program in Java

Java 8 Date Time API Usage

Introduction Every effort we put in to showcase the extent of Java 8 Date Time API usage leaves us only scratching the surface of it. This post walks through a couple of examples and some standard methods that can be helpful in daily use. Timezone aspects are not covered in this post. Refer to this post to for an introduction to Java 8 Date Time API. Old And The New Let us see how date formatting code changes with the new Java 8 Date Time API. If you landed directly on this post, we recommend reading this introductory post first. [crayon-5a318f90d1b54967278301/] Goodbye ThreadLocals and c...
Read More

Java 8 Date Time API

What ailed Date and Time handling in Java? The existing java.util.Date and SimpleDateFormat are mutable and not thread-safe, more often than not, leading to concurrency issues for users. Having to deal with this overhead is not welcoming for novice developers. Inconsistent API (months starting at 1 while days start at 0), no ability to handle Time alone, no utilities like plus/minus dates lead to popularity of libraries like joda-time. Many enterprise products use joda-time, when date/time usage and manipulation should have been provided by the language which was in it's 7th iteration (person...
Read More

Try With Resources in Java 7 and Above

Introduction Try with Resources in Java 7 and above allows for automatic closing of resources that implement java.lang.AutoCloseable or the more familiar java.io.Closeable (which extends AutoCloseable) Anyone through a basic Java course will be familiar with this block of code. Open a text file to write data and close it when done. [crayon-5a318f90d33e3269794915/] The method attempts to write to a file and throws any exception that hinders in achieving this. The finally block ensures that no file handle is left open by this method. But close() can throw an exception if closing the file f...
Read More

Introduction to Functional Interface – Java8

What is Functional Interface? A functional interface is any interface which contains only one abstract method. A functional interface can have one or more default and static methods. Also, there is an annotation in Java @FunctionalInterface which is used to annotate the functional interfaces. It is not a mandate for functional interfaces to carry this annotation. There are many predefined functional interfaces in Java which belong to the package java.util.function What is the significance of these interfaces? These interfaces as we know declare a method which can be implemented and us...
Read More

Introduction to Lambda Expressions – Java8

What are Lambda Expressions? Lambda expressions are another way of writing inline unnamed code block which performs a simple function. Not a direct analogy but if you recall anonymous classes in Java they are a way of writing inline classes. On similar lines Lambda expressions are a way to write method body inline. They can be considered as anonymous methods, there is no need to formally define these methods but the code in the lambda expression is the body of the method. Lambda Expression Example If I want to write a print method which prints the content of a List of strings which is pa...
Read More