Posts

Showing posts from January, 2020

Java Varargs explained with code samples

Java supports passing a variable number of arguments (varargs) to a method. In this article, I will be explaining this feature. What is varargs JDK 5 added the varargs feature, which allow creating a method that accepts a variable number of arguments. Prior to the varargs support, if you wanted to create a method with variable arguments, you either had to overload the method or use a Collection or array as a parameter. Varargs Syntax A variable number of arguments is specified using an argument name and three dots. The following code demonstrates this: void method myMethod(String ...str){//method body} This specifies a String argument str which is a vararg. So you can invoke this method as follows: myMethod("a"); //with 1 parametermyMethod("a","b"); //with 2 parametersmyMethod("a", "b", "c"); //with 3 parameters Code Sample The following code demonstrates a code sample that uses varargs: public class VarargDemo { public stat

Java 8 LocalDateTime class explained with code samples

Just like the LocalDate and LocalTime classes, Java 8 has also introduced the LocalDateTime class. In this article, I will be covering this class. Edit What is LocalDateTime class? The Java 8 LocalDateTime class represents a date with a time component. It has the year, month, day, hour, minute, second and nanoseconds components. So for example, you can use it to represent a date time like 12th August 1997 7:30 am . LocalDateTime Creation There are several static methods on the LocalDateTime class that can be used to create a LocalDateTime. The following code demonstrates this:   LocalDateTime dateTime1 = LocalDateTime.now();  System.out.println("Today's date is "+dateTime1);    LocalDateTime dateTime2 = LocalDateTime.parse("2019-07-15T10:15:30");  System.out.println("Date2 is "+dateTime2);    dateTime2 = LocalDateTime.of(2017,05,17,5,25);  System.out.println("Date3 is "+dateTime2);   The LocalDateTime.now returns the current time. Th

Automatic Dirty Checking in Hibernate

Hibernate provides as feature called Automatic Dirty checking whereby changes to a persistent object are automatically saved to the database when the session is flushed or the transaction is committed. So the code does not need to invoke an explicit save or update. In this article, I will be covering this feature in detail. Introduction Hibernate supports 3 entity states, transient, persistent and detached. In order to understand more about entity states, you can refer to this article. So basically, an object can move into the persistent state when any one of the following happens: When the code invokes session.save, session.persist or session.saveorUpdate When the code invokes session.load or session.get Any changes to a persistent object are automatically saved to the database when the session in flushed. Flushing  is the process of synchronizing the underlying database with the objects in the session. There is a session.flush method available but you generally don’t need to invoke

How to create a basic python program in Eclipse using Pydev

Image
In this article, I will be demonstrating how you can create a basic Python program using eclipse in Pydev. If you want to see how to setup Pydev with Eclipse, refer to this article. Creating a Python Project Step 1 – Click on File –> New –> Pydev project The following screen is shown: Step 2 – Enter a project name and click on Next: Step 3 – The following screen is shown. Click on finish: This creates the project as follows:   Creating a new Python Module After creating a new Python project, the next step is to create a new Python module. Step 1 – Right click on the project, click on New –> Pydev module: The following screen will be shown: Step 2 – Enter a name for the module. Leave the package field blank. Click on “Finish”: Step 3 – This displays the following screen. Click on “OK” This shows the python module in the project as follows:   Writing some basic Python code The next step is to write Python code. Step 1 – Type the code shown below:   The first statement prints th

Java 8 LongSupplier explained with code samples

In this blog post, I will be explaining how the Java 8 functional interface LongSupplier works. To know more about functional interfaces, you can refer this blog post. Edit The LongSupplier interface provides a method called getAsLong. This method does not accept any arguments. It returns a Long data type. The LongSupplier interface is a specialization of the Supplier interface that returns an Long. To see an example of the Supplier interface, refer to this blog post. LongSupplier example Consider the following code snippet: LongSupplier getRandom = () -> new Random().nextLong();System.out.println("Random number 1 = "+getRandom.getAsLong());System.out.println("Random number 2 = "+getRandom.getAsLong());   Here, we have implemented the LongSupplier.getAsLong method using a lambda expression. This getAsLong method simple returns a random integer less than 100. So when this code is executed, it will print output similar to the following: Random number 1

Java 9 Modules Introduction Explained in Brief

Java 9 Modules are one of the new features added by Java 9. This is a 3 part article, in which I will be covering modules in detail. In part 1 of this article, I will be introducing you to modules. See also Modules Internals and Creating a Module . What are modules Modules are basically groups of packages . Just like you put a set of related classes into a package, you can put a set of related packages into a module. This helps to organize code better and to follow the Single Responsibility Principle (SRP).  So all the packages in a single module help to achieve a single functionality. Why they were added Prior to Java 9, there was no concept of modules. So the core jar files related to JDK like rt.jar was very big in size and it was not possible to use them on smaller devices. Also, with JDK 9 and before, developers could access internal JDK files so security was an issue. Java 9 modules help to overcome both these issues. With modules, the JDK is now organised so that the internal J