Posts

Showing posts from December, 2019

Spring framework - What and Why

The Spring framework is widely used for developing different types of applications. In this article, I will dive into the details of what is the spring framework and the advantages that it provides. Before Spring A typical Java application consists of many objects. For example if you are building an application that interacts with the database, your application would have objects corresponding to the POJO class, DAO class, etc. The problem with Java was that there was no way to wire together these objects. So your main class or the starting point of the application would somehow need to create all the required objects and the other parts of the application would use these as required. Design patterns like Factory Pattern, Abstract Factory Pattern helped to ease this slightly, but still the responsibility of creating all the necessary objects still lay with the application code. Java then introduced the EJB framework which eased this to some extent. The problem with EJB framework was th

How to setup Pydev with Eclipse

Image
In this article, I will be covering how to set up the Pydev plugin in Eclipse. Pydev is the Python plugin for Eclipse. It helps you to write Python code using Eclipse. Step 1 – Download Python Download the latest version of Python from https://www.python.org/downloads/ as per your operating system. You should get a file as follows: Step 2 – Install Python Double click the downloaded executable file (file with .exe extension). Click Customize Installation 2. This will show the following screen. Click next: 3. This will show the following screen. Choose the appropriate path for installation. Click Install   4. Installation process will start as follows:   5. The following screen will be displayed once the installation is complete. Click Close Step 3 – Configure Pydev Open Eclipse. Click on Help -> Eclipse MarketPlace. This will display the following screen: Type Pydev in the Search box & click on Go:   This will display the following. Click the install button next to Pydev: This w

Java Try With Explained with Code Samples

The Java try with statement allows you to declare some resources with the try statement. Java automatically closes these resources once the try statement ends. This makes the code clean. Java introduced the try-with statement as part of Java 7. Without Try/With Consider the following code that does not use try-with: public static void saveFile(String fileName, String content) { try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName)); bufferedWriter.write(content); bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } This code uses BufferedWriter to write to a file. Once the code finishes writing the content to the file, it closes the BufferedWriter. Using try/With You can re-write the above code using a try-with statement as follows: public static void saveFile2(String fileName, String content) { try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))){ bufferedWriter.write(content);

Spring Modules Explained

The Spring framework is widely used for different types of applications right from web applications, to even standalone applications. Spring framework provides a wiring mechanism, so you do not have to configure your dependencies manually, Spring does this automatically for you.  In this article, I will be giving an overview of the modules within the Spring framework. I will be explaining how many modules are there in the Spring framework and also be giving an overview of the modules. Introduction The Spring Framework consists of several modules. Each module performs a different functionality. Depending on your requirements, you can add the dependencies for the necessary modules. Spring modules are grouped into different categories as explained below. Edit Spring Core This contains the spring-core, spring-beans, spring-context, spring-context-support, and spring-expression modules . These are the main modules that make up the Spring framework.  Any Spring application needs t

Java 9 Stream.ofNullable explained with code samples

One of the improvements made by Java 9 to the Stream API is the Stream.ofNullable method. In this article, we will be taking a closer look at this method. Introduction Java 9 has added the Stream.ofNullable method to the Stream interface. This helps in avoiding a NullPointerException. Prior to Java 9 Prior to Java 9, you could use the Stream.of method to create a Stream with values. However, a Stream could not be created from a null value and if done, it would result in a NullPointerException. The following code demonstrates this: Stream<Integer> stream = Stream.of(10);System.out.println("Stream size:"+stream.count()); So this creates a Stream with a single element. Now suppose you specify a null value in the Stream.of method as follows: Stream<Integer> stream = Stream.of(null);System.out.println("Stream size:"+stream.count()); The above code will result in a NullPointerException. Using Java 9 Java 9 Stream.ofNullable helps to avoid a NullPointerExceptio

Java 8 Stream API Distinct method with code samples

In this article, I will be demonstrating the distinct method provided by Java 8 Stream API. In order to understand the Stream API in detail, refer to  this  blog post. Edit Introduction The Stream interface has a method called distinct. You can use this method to eliminate duplicates from a Stream. So you can use this to eliminate duplicates from a Collection. Using Stream.distinct with Integers The following code sample demonstrates using the Stream.distinct with an Integer List: List<Integer> input = Arrays.asList(3,12,11,45,12,5,3,9,20,11,3);System.out.print("Input:");input.forEach(num -> System.out.print(num+" "));System.out.println();System.out.print("Output:");List<Integer> distinctInput = input.stream().distinct().collect(Collectors.toList());distinctInput.forEach(num -> System.out.print(num+" ")); So first, the code invokes the stream method on the input List. Then, the code invokes the distinct method to obtain un

Java Callable Interface Explained with Code Samples

In this article, I will be explaining the Callable interface in Java. I will be explaining what is Runnable and Callable in Java and what is the use of the Callable interface. Runnable Interface Right from the start, the Runnable interface has been associated with multi-threaded programming.  The Runnable interface provides a run method that executes code in a separate thread. The following code demonstrates this: public class RunnableDemo { public static void main(String[] args) { System.out.println("Creating new thread in main"); Runnable r = () -> System.out.println("Executing thread body...."); Thread myThread = new Thread(r); myThread.start(); System.out.println("Completed main"); }} So this code first creates a new Runnable interface and uses a lambda expression to implement the run method.  The code then creates a new Thread and invokes the start method which spawns a new Thread and executes the code in the Runnable implementatio

How to find the number of years between two Dates

Prior to Java 8, you had to write complex logic to find the number of years between two dates. The DateTime API introduced from Java 8 onwards provides some methods that let you easily find the number of years between two dates. Edit The following code demonstrates this: public static void main(String[] args) { LocalDate date1 = LocalDate.parse("1997-04-28"); LocalDate date2 = LocalDate.parse("2015-11-25"); Period period = date1.until(date2); int yearsBetween = period.getYears(); System.out.println("yearsBetween:"+yearsBetween);} Java 8 has added a LocalDate class to represent a Date. So in this code, both the input Dates are held within LocalDate objects. There is a method called until on the LocalDate class. This returns a Period instance. Period has a method called getYears which returns the number of years. So this code prints the following output: yearsBetween:18

JAX-RS Exception Handling explained with code samples

There are several ways to perform exception handling in a JAX-RS application. In this article, I will be explaining some of the JAX-RS exception handling mechanisms. Using an ExceptionMapper One way to perform exception handling in a JAX-RS application is using a custom exception mapper. The following code demonstrates a custom exception mapper: @Providerpublic class MyExceptionMapper implements ExceptionMapper<MyException>{@Overridepublic Response toResponse(MyException ex) {return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();}}   This is an Exception Mapper for a custom exception called MyException. It has the @Provider annotation. The MyExceptionMapper class implements the ExceptionMapper interface and overrides the toResponse method. In this method, you can place the correct HTTP status code. In this case, the code uses the HTTP status code for Internal Server Error i.e. code 500. So this code will be sent to the client when MyException occurs. Throwing a We