Posts

Showing posts from October, 2018

How to print the current date and time

In this blog post, I will be demonstrating how you can print the current date and time.   Consider the following code snippet: import java.util.Calendar;public class PrintCurrentDateTimeDemo {public static void main(String[] args) {Date date = new Date();System.out.println("Current date and time is "+date);}}   Here, we are using a  java.util.Date instance .   This represents a particular instance in time. When we create a new Date object with the default constructor, it creates a Date object with the current date and time. The Sysout statement then prints the Date and time in this date object. So this code prints the following output: Current date and time is Mon Feb 25 09:31:36 IST 2019    

Java loops Examples

In this blog post, I am going to demonstrate all of Java’s loops. I am going to write loops that prints the sum of numbers in a List. For Loop private static int sumFor(List<Integer> numbers){int sum = 0;for(int i=0; i < numbers.size();i++){sum += numbers.get(i);}return sum;}   For Each Loop private static int sumForEach(List<Integer> numbers){int sum = 0;for(int num:numbers){sum += num;}return sum;} While Loop private static int sumWhile(List<Integer> numbers){int sum = 0;int i = 0;while (i < numbers.size()){sum += numbers.get(i);i++;}return sum;} Do While Loop private static int sumDoWhile(List<Integer> numbers){int sum = 0;int i = 0;do{sum += numbers.get(i);i++;}while (i < numbers.size());return sum;}

What is a marker interface is Java

In this blog post, I will be explaining what is a marker interface in Java.   Marker Interface Definition A marker interface is simply an interface that has no methods. Some examples of marker interfaces in Java are Serialiazable , Cloneable ,  etc.   How Marker Interfaces work So if there are no methods in a marker interfaces, how do they work? In other words, how does a class that implements a marker interface know what methods to provide? The answer to this is that marker interface specify a design pattern  used to provide run-time type information about the objects.  The very fact that a class implements a marker interface indicates something to the JVM or compiler. So when the JVM sees an object of the marker Interface type, it will perform some special operation. You can also define your own marker interfaces and write code that behaves in a special way if the marker interface is implemented. So for ex. objects that belong to a class that implements the marker interface can foll

How to find the maximum and minimum number in a List

In this blog post, I will be demonstrating how you can find the largest and smallest number in an array. Consider the following code snippet: package learnjava.collections;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class MaxMinDemo {public static void main(String[] args) {List<Integer> input = new ArrayList<Integer>();input.add(3);input.add(9);input.add(5);input.add(15);input.add(11);System.out.println("Biggest element in the list is:"+Collections.max(input));System.out.println("Smallest element in the list is:"+Collections.min(input));}}   This code uses the Collections.max and Collections.min methods. The Collections class contains static utility methods that operate on collections like List, Set etc.  The Collections.max  method returns the greatest element in the list according to the natural order or sorting. Similarly, the Collections.min returns the smallest element in the list according to the natura

DoublePredicate Interface in Java 8 with code examples

In this blog post, I will be explaining how the Java 8 functional interface DoublePredicate works. To know more about functional interfaces, you can refer this blog post. Edit The DoublePredicate  interface provides a method called test. This method accepts a parameter of Double data type and returns a boolean. The DoublePredicate interface is a specialization of the Predicate interface. While the Predicate interface accepts any data type, the DoublePredicate  interface accepts a Double value. To see an example of the Predicate interface, refer to  this  blog post. DoublePredicate Example Consider the following code snippet: public class DoublePredicateDemo {public static void main(String args[]){DoublePredicate greaterThan1 = (input) -&gt; input &gt; 1;System.out.println("0.75 is greater than 0 = "+greaterThan1.test(0.75));System.out.println("1.12 is greater than 0 = "+greaterThan1.test(1.25));}} Here, the DoublePredicate.Test method checks if the in

How to convert a List to a Set

In this blog post ,I will be showing you how you can convert a List to a Set. Consider the following code snippet: public class ListToSetDemo {public static void main(String[] args) {List<Integer> list = Arrays.asList(5,3,11,15,9);//Method 1Set<Integer> set = new HashSet<Integer>(list);System.out.println("Set is "+set);//method 2Set<Integer> set2 = new HashSet<Integer>();set2.addAll(list);System.out.println("Set is "+set2);}} This code demonstrates two ways to convert a List to a Set. Method 1 Here, we are creating a new HashSet . We are invoking the constructor that accepts a Collection object. Here, we are passing in the input list to this constructor.   Method 2 Here again, we are creating a new HashSet . However, we are using the default constructor. We are then invoking the addAll method and passing in the input list.   When you run this code, it will print the following output: Set is [3, 5, 9, 11, 15]Set is [3, 5, 9, 11, 15] No

How to Create a Maven project in Eclipse

Image
In this blog post, I will be explaining how to create a Maven project in Eclipse: Step 1 – Open Eclipse 2. Click on File –> New –> Other 3. Click on Maven Project under Maven: 4. Click Next: 5. Click on Create a simple project (Skip archtype selection). Click on Next: 6. Enter group Id and artifact Id in the screen: 7.  Click Finish. Your Maven project is ready. You can start adding dependencies in your pom file and start writing Java code in src/main/java folder:   Further Reading Apache Maven Beginner to Guru Maven Crash Course Java Programming in Eclipse

Java This Keyword Explained

In this blog post, I will be explaining the ‘this’ keyword. Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object i.e. the object on which the method was invoked. Consider the following code snippet: private int radius;public  Circle(int radius){this.radius = radius;}   Since both the passed in variables and the instance fields have the same names, this is used to distinguish. So this.radius refers to the current object’s copy of the radius instance field & radius refers to the value passed in.

Creating a Basic Java Project in Eclipse

Image
In this blog post, I will demonstrate how you can create a simple HelloWorld class in Eclipse. Step1 – Open Eclipse Step 2 – Click on File –> New –> Java Project Step 3 – Enter project Name and Click Next Step 4: Click Finish Your Java project is now ready! You can start creating Java files within the src folder!

Predicate interface in Java 8 with examples

In this blog post, I will be explaining how the Java 8 functional interface Predicate works. To know more about functional interfaces, you can refer this blog post. Edit The Predicate interface provides a method called test. This method accepts a parameter of any data type and returns a boolean. Predicate method with Integer argument Consider the following code snippet: public class PredicateDemo {public static void main(String[] args) {Predicate<Integer> greaterThan8 = (input) -> input > 8;System.out.println("4 is greater than 8 = "+greaterThan8.test(4));System.out.println("12 is greater than 8 = "+greaterThan8.test(12));}} Here, the Predicate.test method checks if the input number is greater than 8. So when the above code is executed, it will print the following output: 4 is greater than 8 = false12 is greater than 8 = true Predicate method with String argument Consider the following code snippet: import java.util.function.Predicate;public class