Posts

Showing posts from March, 2020

Iterator and Enumeration - Differences Explained

In this article, I will be comparing the Iterator and Enumeration interfaces. I will be explaining the similarities and differences between Iterator and Enumeration. Definitions Both Iterator and Enumeration are interfaces defined in the Java Collection framework. You can use both to loop through the elements of a Collection. Let us first understand how they work   Iterator Iterator is an interface defined in the Java Collection framework. You can use it to loop through the elements of a collection. The following code demonstrates this: List<Integer> numbers = Arrays.asList(2,4,6,8,10); Iterator<Integer> itr = numbers.iterator(); while (itr.hasNext()) { System.out.print(itr.next()+" "); } This code uses an Iterator to loop through the elements in a List and print them. Iterator.hasNext() method returns true if there are more elements in the collection. The iterator.next() method returns the next element in the collection. So this code prints the following ou

Python Numeric and String variables

In this article, I will be covering Python numeric and String variables Python Numeric and String Variables Introduction Unlike Java, in Python, variables do not have a data type . You just need to specify a variable name. Also, unlike Java, you do not need to declare a variable to be of a particular data type. Python automatically allocates memory to a variable the moment you assign it a value. Creating a variable In order to create a variable, you just need to specify the variable name , followed by the assignment operator (=) followed by the value . This automatically allocates memory to the variable based on the data type of the assigned value. The following code demonstrates this: i=10 This code declares the variable i and assigns the value 10 to it. Python Numeric Variables You can create numeric variables by simply assigning a number to the variable name. Python supports storing whole numbers and decimal numbers. The following code demonstrates this: a = 10b = 100.5   This code

Spring XML Configuration Example

In this article, I will be demonstrating how to configure a standalone Spring application using XML configuration . I will be demonstrating both setter and constructor injection . I will be using Eclipse and Maven . In order to get a basic introduction to Spring, you can check out this article. Project Set-up Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse. Step 2 – Add Spring dependencies to the maven pom file. You can add the following: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> </dependencies> Edit Creating

Hibernate HQL Syntax with code examples

HQL stands for Hibernate Query language. It is similar to SQL, but instead of the database table and column names, it uses the class and field names. So whenever we need to query the database, we write HQL queries in Hibernate. Based on the underlying database, Hibernate automatically converts HQL into equivalent SQLs statements. HQL has a very rich syntax and I will be covering the Hibernate HQL syntax in detail in this article.   From Clause The from clause consists of the from keyword followed by an entity name.  It specifies the entity from which Hibernate needs to retrieve data. So if we have a Person entity, we can specify the from clause as follows: String queryStr = "from Person";Query<Person> query = session.createQuery(queryStr);List<Person> books = query.getResultList(); Select Clause The select clause can also be used to retrieve data from the database. The select clause is more powerful than the from clause. It provides more control over the resultse

Java 9 Module creation with code samples

Image
One of the new features added by Java 9 is the module system. This article is the last part in a  3 part article. In  Part 1 , I gave a high level introduction to modules and the advantages that they provide. In Part 2 ,  I covered the details of what exactly a module is, what it consists of and other module internals. In this part, I will be explaining how to create and use a module in Eclipse.   See also Module Introduction and Module Internals Creating a Module As covered in my earlier article, a module is a group of packages that helps to organize code better.  Let us first create a Java project that has a module. Step 1 – Ensure that Java 9 is installed and configured in Eclipse. Step 2 – Create a new Java project. Enter a project name. Ensure that JDK 9 is selected.   Step 3 – Click Next. The following screen is shown:   Step 4 : Click “Finish”. A screen for entering module name is shown as below:   Step 5 – Enter the module name as “ com.learnjava.calculatormodule ” Step 6

How to add a number of days to a Date in Java

In this article, I will be demonstrating how to add a number of days to a Date in Java Edit Before Java 8 Prior to Java 8, the Date and Calendar classes were available for Date manipulation. They make operations like adding a number of days to a Date very difficult. The following code demonstrates adding days to a date using the Calendar class: String stringDate="2019-07-15"; Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(stringDate); Calendar cal = Calendar.getInstance();cal.setTime(date1); // manipulate datecal.add(Calendar.DATE, 5); Date dateWith5Days = cal.getTime(); System.out.println(dateWith5Days); So the above code first uses SimpleDateFormat.parse to convert a String date to a java.util.Date. It then creates a Calendar object corresponding to this date. Finally, it uses the Calendar.add method to add days to the date. Using Java 8 Java 8 introduced the LocalDate class which makes date manipulations like adding/subtracting days/months very