Posts

Showing posts from February, 2020

New Java 8 Features Overview with explanations

Though I’ve covered many Java 8 new features, I’ve never really given an overview of all the new features as a whole. So I will be covering that in this article. Edit Functional Interfaces Functional interfaces are simply interfaces with just one abstract method. Java 8 has introduced the @FunctionalInterface annotation that designates an interface as a functional interface. Functional Interfaces and Lambda expressions go hand in hand. Java 8 has also added some built-in functional interfaces like Predicate, Consumer, Supplier, etc. You can read more about Functional interfaces here . Lambda expressions The most talked-about feature in Java 8 is lambda expressions . Java is an object-oriented language, however, object-oriented code sometimes tends to be lengthy and verbose. Java 8’s lambda expressions enable programmers to overcome this issue by writing code as functions . This gets rid of a lot of boilerplate code and helps to keep things clean. Lambda expressions make use of f

Java 8 Optional Explained with code samples

Java 8 has introduced a bunch of new features. The Optional feature introduced by Java 8 helps in avoiding unexpected exceptions. In this article, I will be covering this feature in detail. Edit Optional Introduction An optional can be used to represent a value that can either be present or absent. Many times methods return a null value. If the code that invokes the method expects a non-null value, this can result in a NullPointerException . To avoid this, you need to add explicit null checks in your code. This can make the code difficult to read. Consider the following code: Person person = new Person();if(person != null){ // Null Check String name = person.getName();} The above code needs to have an explicit null check for Person object. Only if the person object is not null, the code invokes the getName method. Java 8 Optionals help to avoid explicit null checks like this. So you can re-write the above code using an Optional as follows: Optional<Person> person = Option

Java Method Overloading Explained with Code Samples

Java method overloading allows defining two or more methods within the same class that share the same name. Method overloading makes the code clean and increases the readability of the code. Method overloading helps to achieve polymorphism which is one of the object oriented principles.  In this article, I will be covering method overloading in detail. Introduction When methods are overloaded, their parameter declarations need to be different. The following code demonstrates this: public class AdditionService {             int add() { // no parameters             return 3+5;       }             int add(int i) { //one integer parameter             return i+i;       }             int add(int i,int j) { //two integer parameters             return i+j;       }             double add(double i,double j) { //two double parameters             return i+j;       }            } This class has 4 methods with the name add . All four add methods differ in the parameters , so there is no compilation

Introduction to Swagger

Recently, I got an opportunity to understand a bit about Swagger and how it works. So in this article, I will be explaining what swagger is, how it works. This article is for those readers who have heard about Swagger but wish to know about it further. Swagger is a tool that developers can use to document REST services. It is based on the Open API specification. Challenges in documenting a REST application SOAP web services use a WSDL file for documenting the web service. However, when it comes to REST services, there are no established standards for documentation.  So developers need to create documents manually. This method is tedious and error prone. Open API Specification OPEN API is a specification that developers can use to document REST services. It helps in documenting the following: Available HTTP methods(GET, POST, PUT, etc) supported by each REST method, Input/outputs that the REST service accepts/produce Authentication information Open API specification files can be written

How to sort a List in Descending order

In this article, I will be demonstrating how you can sort a List in descending order. In order to see how to sort a List, you can refer to this blog post.   Approach 1 – Using Collections.reverse The Collections class has a utility method called reverse.  You can use this to sort a List in descending order. The following code demonstrates this: private static void usingCollectionsReverse() { List<Integer> input = Arrays.asList(15,12,34,11,93,21,64); System.out.println("Before sorting:"+input); Collections.sort(input); Collections.reverse(input); System.out.println("After sorting:"+input);} Here, the code first invokes the Collection.sort . This sorts the List in ascending order. Then the code invokes the Collections.reverse method to reverse the List. So this code prints the following output: Before sorting:[15, 12, 34, 11, 93, 21, 64]After sorting:[93, 64, 34, 21, 15, 12, 11] Approach 2 – Using Collections.reverseOrder There is an overloaded version of

Java 9 Module Internals Explained in Brief

One of the new features added by Java 9 is the module system. This article is the second part in a  3 part article. In Part 1 , I gave a high level introduction to modules and the advantages that they provide. In this part, I will dive more into the details of what exactly a module is, what it consists of and other module internals. See also Module Introduction and Creating a Module . What is a module As explained in the introductory article on modules, modules are basically groups of packages. Modules help to organize the code better and to achieve the Single Responsibility Principle(SRP). Also, a module can be deployed by itself. So if there is a large application, you can break it down into modules. Each module can be built into a separate jar file and deployed as required instead of deploying a big fat jar file for the whole application. Module Descriptor Along with very module, you need to provide a module descriptor. The module descriptor needs to be present at the root of the