Posts

Tomcat Maven Plugin with code sample

Image
The Tomcat Maven plugin helps to run a Java web application in an embedded Tomcat container. Thus, this allows developers to quickly develop and build a web application without having to install Tomcat. In this article, we will be learning how to use this plugin. Creating a Maven Project Let us first create a simple Maven web project. I will be using Eclipse IDE. In order to create a Maven web project in Eclipse, you need to follow the steps given below: Step 1 – Click on File > New > Other . Click on Maven Project . Click Next : Step 2 – The following screen is displayed. Click Next : Step 3 – In the Filter text box, type “maven-archetype-webapp” .  Select “ org.apache.maven.archetypes “. Click Next : Step 4 – Enter Group Id and Artifact Id . Click Finish : This creates a web project with the required directory structure. It also has a basic hello world page:   Adding the Tomcat plugin In order to support the Tomcat Maven plug...

Python While Loop with code samples

Much like a Java while loop, you can use a Python while loop to repeatedly execute a block of code as long as a condition is true. In this article, we will learn more about the Python while loop. Python While loop Syntax A while loop in Python has the following syntax: while <condition>: loop body The while keyword is followed by a condition. If the condition evaluates to true, the body of the loop is executed. Once the body of the loop is completed, the condition is checked again and this is repeated until the condition evaluates to false at which point the loop is exited. While loop Sample Code The following code demonstrates how you can use the while loop: num=10counter=1total=0while counter<=num: total += counter counter+=1print("sum of first 10 numbers is",total) This code adds the numbers from 1 to 10 and prints the sum. It uses a while loop to iterate from 1 to 10. The while loop checks the condition counter <= num and if so adds the current value of count...

Parallel Streams in Java with code samples

In my earlier article, I had covered the Stream API in depth. In this article, we will be taking a look at parallel streams. Simply speaking, a parallel stream is a stream that operates on its elements in parallel. How to obtain a parallel stream Just like the stream method, all the Collection interfaces have a parallelStream method. This returns a parallel stream on the underlying Collection. Consider the following code: List<String> food = Arrays.asList("Pizza","Sandwich","Burger","Rice");Stream<String> foodStr = food.parallelStream(); This code first creates a String List food with some values. It then invokes the parallelStream method on food. This returns a String Stream which is a parallel stream. In addition, the Stream interface also has a method parallel . If the stream on which this method is invoked is a sequential stream, it returns a parallel stream corresponding to it. If the stream on which it is invoked in a paral...

Java Inheritance Interview Questions With Examples

In this article, I will be listing some Java inheritance interview questions What are the benefits of inheritance in Java? Answer: Inheritance has the following benefits: It permits code reuse. So, if there is some common code that can be used across a set of subclasses it can be specified in the base class and reused by all the subclasses. It facilitates polymorphism via method overriding. Method overriding allows a subclass to specify a method with the same type signature as a method in the superclass.  To know more about method overriding refer to this blog post. It makes the code extendable. So, if there is already some code, developers can use the code by creating a subclass and they need not write it again What are the different types of inheritance? Answer: Java supports the following types of inheritance: Single Inheritance – In this case, a class has only one subclass Multi-level Inheritance – In this case, a subclass in turn has a subclass Hierarchical Inheritance...

IntBinaryOperator

In this blog post, I will be explaining how the Java 8 functional interface  IntBinaryOperator works. To know more about functional interfaces, you can refer to this  blog post. What is IntBinaryOperator IntBinaryOperator is an in-built functional interface in the java.util.Function package. It accepts two int arguments, operates on it and produces a result of type int . It is a specialization of the BinaryOperator interface. It has a applyAsInt method. It applies the logic in this method on the int arguments passed in and produces an int result. IntBinaryOperator Code Sample The following code demonstrates this   interface: IntBinaryOperator intBinaryOp = (num1,num2) -> num1+num2;int input1=14,input2=12;int result = intBinaryOp.applyAsInt(input1, input2);System.out.println("result:"+result); Line 1 declares a IntBinaryOperator instance intBinaryOp and implements it via a lambda expression that adds the input arguments and returns the result. Line 3 invokes the applyA...

How to create a Spring Boot Thymeleaf web application

Image
In this article, I will be demonstrating creating a Spring Boot Thymeleaf web application. Project Creation and Setup Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot and Thymeleaf dependency . So, the pom.xml  file should be similar to the following: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.learnjava</groupId> <artifactId>learnjava-springbootthymeleaf-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </par...

Python functions arguments with examples

In Part 1 of the Python functions article, I gave you an introduction to Python functions. In this article, we will take a look at Python functions with arguments. Arguments allow passing some data to a function so that the function then operates on the data. Python functions arguments are mainly of 4 types: positional, default, keyword, and arbitrary. Let us now take a look at each type of argument Positional Arguments Positional arguments are arguments whose values are passed in the same order in which they are specified in the function definition. Consider the following code: def showMarks(name,marks): print("Hello", name,"you have scored",marks,"marks!") This code specifies a showMarks function. It accepts arguments name and marks and simply prints a message with these values. You can now invoke this function as follows: showMarks("Jane",97) This assigns the value “Jane” to the first argument that is name and the value 97 to the seco...