Posts

Showing posts from December, 2020

Python Functions - Part 1

In this article, I will be giving you an introduction to Python functions. What is a Python function A Python function is nothing but a block of code with a name. You can use its name to execute the code multiple times without having to write the code each time. It is similar to a method in Java except that it need not be defined within a class. How to define a function In order to write the code for a function, we first need to create a Python module.  You can refer to this article to understand how to create a Python module. Let us assume that we have a module functions.py.   Within this module, you can specify your first function. Consider the following code: def add(): num1 = int(input("Enter the first number:")) num2 = int(input("Enter the second number:")) result = num1+num2 print("sum is ",result) This code specifies a function add . You need to specify the keyword def before the function name. The function name is followed by empt

How to create a standalone JPA application

In this article, I will be explaining how to create a standalone JPA application. that saves data to a database table and queries a table. JPA stands for the Java Persistence API. In order to know more about JPA and how it works, you can refer to this article. I will be using Hibernate as the JPA provider and MySQL database, however you can use any other JPA Provider/database. MySQL Installation The first step would be to download and install MySQL. You can download MySQL from here as per your operating system. Once the download is complete, you can follow the onscreen instructions to proceed with the installation. I recommend going with the developer default that installs MySQL, MySQLWorkbench and other utilities required for development. Creating Sample Database Next, you need to create a database table. You can execute the following SQL code to create the sample database table called Fruit . create database jpademo;use jpademo;create table fruit ( id int auto_increment, name varch

Java 11 String enhancements with examples

Java 11 has made some significant improvements to the String class. In this article, we will be exploring the Java 11 String enhancements strip Java 11 has added a new strip method to the String class. This method removes all the leading and trailing whitespaces in a String. Consider the following code: String input = " Hello World ";System.out.println(input);String output = input.strip();System.out.println(output); This code produces the following output: Hello WorldHello World In addition to strip , the String class also has a trim method which also removes all leading and trailing whitespaces. This method exists right from the early versions of Java and so has some limitations. Earlier, the ASCII character set was used. So, trim can only remove whitespaces represented by ASCII characters. However, later on Java adopted the Unicode character set.  Unicode has some additional characters that represent whitespace. The trim method cannot remove these whitespaces.

Java 8 Comparator Changes

Java 8 has made many improvements to the Comparator interface. In this article, I will be explaining the Java 8 Comparator changes. Edit Comparator.compare From Java 8 onwards, Comparator is designated as a functional interface. So, you can implement the Comparator.compare method via a lambda expression . Consider the following Book class: public class Book { String name; int numPages; public Book(String name, int numPages) { this.name = name; this.numPages = numPages; } //getters and setters} And suppose you have a List of Book objects which needs to be sorted on the basis of the numPages field. Prior to Java 8, you had to create a class that implements the Comparator interface and use it to sort the Book List. The following code demonstrates this: public class BookCompartor implements Comparator<Book> { public int compare(Book book1, Book book2) { return book1.getNumPages() - book2.getNumPages(); }}public class ComparatorBeforeJava8Demo { public sta