How Constructor Chaining works in Java - Example

How to call one constructor from another constructor in Java or What is Constructor Chaining in Java is one of the tricky questions in Java interviews. Well, you can use this keyword to call one constructor from another constructor of the same class if you want to call a constructor from based class or super class then you can use super keyword. Calling one constructor from other is called Constructor chaining in Java. Constructors can call each other automatically or explicitly using this() and super() keywords. this() denotes a no-argument constructor of the same class and super() denotes a no argument or default constructor of parent class. Also having multiple constructors in the same class is known as constructor overloading in Java.

What is public private protected and package or default access modifier in Java

public, private, protected and package or default are four access modifiers available in Java. These access modifiers provide Java programmers to control accessibility or visibility of class, method or any field of a class. Good understanding of public, private or protected modifier is required in order to implement proper encapsulation in Java and create a Java program which is easier to maintain. In this Java tutorial, we will see what is public, private, protected and default modifiers are, which modifier can be used with top-level class and nested class and what is the difference between public, private, protected and default modifier in Java.

What Java developer Should Know about Object and java.lang.Object

Java is an object oriented programming language and core of Java API is java.lang.Object class. In order to work properly in Java platform its important to learn fundamentals of Object in Java e.g. What is an Object in Java and How to use Object in Java. There are two meanings of Object in Java one which is used to refer object of Object oriented programming language or OOPS and other is java.lang.Object class. Every class in Java which explicitly doesn’t extend any class, implicitly extends java.lang.Object class. Crucial methods like finalize and wait and notify are declared in Object class, which is source of one of the java questions Why wait and notify are declared in Object class and not on java.lang.Thread class.

When to use ArrayList vs LinkedList in Java

ArrayList and LinkedList are two popular concrete implementations of List interface from Java's popular Collection framework. Being List implementation both ArrayList and LinkedList are ordered, the index-based and allows duplicate. Despite being from the same type hierarchy there are a lot of differences between these two classes which makes them popular among Java interviewers. The main difference between ArrayList vs LinkedList is that former is backed by an array while later is based upon linked list data structure, which makes the performance of add(), remove(), contains() and iterator() different for both ArrayList and LinkedList.

How to display date in multiple timezone in Java with Example - PST GMT

We can use SimpleDateFormat class to display a date in multiple Timezone in Java. While working in global Java application it's quite common to display dates in the different time zone, classical example is Server is running on either PST or GMT timezone and clients are global or at least running on global trading hubs like Hong-kong, Mumbai, Tokyo, London etc. Unfortunately, Date and Time API in Java is quite tricky and until you have a good understanding of Date and Time classes and methods e.g. Calendar, SimpleDateFormat and thread-safety issue , You can easily create bugs. One of misconception Java programmer has is converting a date in the different timezone. Actually, Date in Java is always in GMT and it represents a number of millisecond since 01-01-1970 00:00 and when we print Date, it calls the toString method and displays date time information in the local timezone.

JDBC - How to Convert java.sql.Date to java.util.Date in Java with Example

How to convert java.sql.Date into a java.util.Date and vice-versa is a popular JDBC interview question which is also asked a follow-up question of the difference between java.sql.Date and java.util.Date which we have seen in our last article. Since both SQL date and Util date store values as a long millisecond, it's easy to convert them back and forth. Both java.sql.Date and java.util.Date provides a convenient method called getTime() which returns long millisecond equivalent of a wrapped date value. Here is a quick example of converting java.util.Date to java.sql.Date and then back to util Date. This article is in the next of earlier posts on Java Date and Time API  like How to convert Date to String and How to find current Date and Timestamp in Java on any timezone. if you haven't read them already you may find them useful.

How to check leap year in Java - program example

Write a Java program to find if a year is a leap year or not is a standard Java programming exercise during various Java programming course on school, colleges and various training institutes both online and offline,  along with other popular homework's e.g. printing Fibonacci numbers, checking palindromes or finding prime numbers. Just to recap a leap year is a year with 366 days which is 1 extra day than a normal year. This extra day comes in the month of February and on leap year Feb month has 29 days than normal 28 days. If you are following then you might know that leap year comes in an interval of 4 years. This year 2012 is a leap year and Feb has 29 days, you can check.

Difference between RuntimeException and checked Exception in Java

RuntimeException vs Checked Exception in Java
Java Exceptions are divided in two categories RuntimeException also known as unchecked Exception and checked Exception. Main difference between RuntimeException and checked Exception is that It is mandatory to provide try-catch or try finally block to handle checked Exception and failure to do so will result in compile-time error, while in case of RuntimeException this is not mandatory. Difference between checked and unchecked exception is one of the most popular question on Java interview for 2 to years experienced developer especially related to Exception concepts.

Difference between GenericServlet vs HttpServlet in Servlet JSP - J2EE question

Difference between GenericServlet and HttpServlet is one of the classic Servlet Interview Question, asked on many Servlet and JSP Interviews on 2 to 4 years experience developers. Since both GenericServlet and HttpServlet forms basis of Servlets its important to know What are they and What is main difference between them. From common sense and there names, its obvious that GenericServlet is a generic and protocol independent implementation of Servlet interface while HttpServlet implements HTTP protocol specifics. If you are working in Java web application or J2EE projects, you are most likely to deal with HttpServlet all time as HTTP is main communication protocol of web. In this Servlet JSP article we will outline some important difference between HttpServlet and GenericServlet which is worth knowing and remembering.

Difference between Array vs ArrayList in Java

What is the difference between Array and ArrayList is quite a common question among beginners especially those who started coding in C and C++ and prefer to use Array? Both Array and Array List are used to store elements, which can be either primitive or objects in case of Array and only objects in case of ArrayList in Java. The main difference between Array vs ArrayList in Java is the static nature of the Array and the dynamic nature of ArrayList. Once created you can not change the size of Array but ArrayList can re-size itself when needed. Another notable difference between ArrayList and Array is that Array is part of core Java programming and has special syntax and semantics support in Java, While ArrayList is part of the Collection framework along with other popular classes like Vector, Hashtable, HashMap or LinkedList.

How to remove all white space from String in Java - beginning end and between words

String API in Java provides several utility methods to remove white space from String in Java, from the beginning, end, and between words. White space means blank space including tab characters. One of the classic methods to remove white space from beginning and end of a string in Java is the trim() method, which removes white space from beginning and end. While using trim() method, key thing to remember is that trim() returns a different String object than older one because String is immutable in Java. On the other hand if you want to remove all white spaces from string i.e. white space from beginning, end and between words then you can use replaceAll() method of String and pass regular expression \s to find and replace all white spaces including tab with empty string "".

Difference between trustStore vs keyStore in Java SSL

Main difference between trustStore vs keyStore is that trustStore (as name suggest) is used to store certificates from trusted Certificate authorities(CA) which are used to verify certificate presented by Server in SSL Connection while keyStore is used to store private key and own identity certificate which program should present to other parties (Server or client) to verify its identity. That was one liner difference between trustStore vs  keyStore in Java but no doubt these two terms are quite a confusion not just for anyone who is the first time doing SSL connection in Java but also many intermediate and senior level programmer. One reason of this could be SSL setup being a one-time job and not many programmers get opportunity to do that.

How to convert String from lowercase to uppercase and lowercase in Java

Convert String from uppercase to lowercase in Java
Sometime we need to convert String from lowercase to uppercase or from uppercase to lowercase e.g. before printing or storing into database etc. String class in Java provides some utility method to perform this case conversion. You can use toUpperCase() to convert any lower case String to uppercase and toLowerCase() to convert any uppercase String to lowercase. Key thing to remember while using toUpperCase() and toLowerCase() is that they return a different String rather than modifying same String because String is immutable in Java. So if you use old String, assuming it has been converted into uppercase or lowercase then you may create but, instead just store new String returned by these methods into same variable which is hold to store old String.

How to Read Input From Console in Java using Scanner Example

Apart from reading file, Scanner can also read user input from Console in Java. Just like in case of reading file, we have provided File as source for scanning, We need to provide as source to scan for user input in Console. Once you created and initialized java.util.Scanner, you can use its various read method to read input from user. If you want to read String, you can use nextLine(), if you want to read integer numbers, you can use nextInt(). Subsequently you can use nextFloat() to read float input, nextDouble() to read double input etc. Scanner class also allows you to define your own pattern and scan for that.

Difference between Error vs Exception in Java - Interview question

Both Error and Exception are derived from java.lang.Throwable in Java but main difference between Error and Exception is kind of error they represent. java.lang.Error represent errors which are generally can not be handled and usually refer catastrophic failure e.g. running out of System resources, some examples of Error in Java are java.lang.OutOfMemoryError or Java.lang.NoClassDefFoundError and java.lang.UnSupportedClassVersionError. On the other hand java.lang.Exception represent errors which can be catch and dealt e.g. IOException which comes while performing I/O operations i.e. reading files and directories.

Difference between transient vs volatile variable or modifier in Java

transient vs volatile modifier in Java
What is difference between transient and volatile variable or modifier in Java is one of the most common Serialization Interview Question in Java. Though volatile variables are not related to Serialization at all, this question is mostly asked in conjunction with other Serialization question. Both transient and volatile modifiers are completely different to each other. In fact this question is as popular as Serializable vs Externalizable in Java. Main difference between transient vs volatile variable is that transient variables are not serialized during Serialization process in Java while volatile variables are used to provide alternative synchronization in Java. By using volatile keyword or modifier on fields, signals compiler that this variable is accessed by multiple thread and any reordering or optimization by compiler should be avoided. In this Java article we will see some difference between transient and volatile variable by learning each of them and When to use transient and volatile variable in Java.

JDOM Example : Reading and Parsing XML with SAX parser in Java

XML parsing with JDOM parser
JDOM is an open source library which allow XML parsing and reading in Java program. JDOM is designed by using Java programming technique and customized for Java programmers, so that Java programmer with  very little knowledge of XML documents can use JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory design pattern to create instance of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our last example of parsing XML documents in Java, JDOM uses new() operator to create its parser instances. In fact JDOM is very easy to understand and most of the time its self explanatory. While trying this Java program to parse XML file, you can explore JDOM API. JDOM API also allows to use XPATH expression to query XML documents with package org.jdom2.xpath. In next section we will see complete code example of How to read an XML file using JDOM parser in Java program.

How to Search Element in Java Array with Example

find an index of the object in Array
While programming in Java, many times we need to check if a String array contains a particular String or an int array contains a number or not. Though array is an object in Java but it does not provide any convenient method to perform these kinds of operations. Sometimes you even need an index of an item stored in Array, unfortunately, Java API does not provide any direct method. Thanks to Open Source Apache Commons provides a utility class called ArrayUtils which allows you to check for an Item in Array, find its index in the array, find its lastIndex in Array and perform several other common operations. In fact, ArrayUtils has overloaded methods for different kinds of Array e.g. String, int, float, double, Object, etc, which makes this kind of programming task trivial. In this article, we will see an Apache commons example of How to use ArrayUtils class to search an item in Array and find its index.

Difference between Serializable vs Externalizable in Java - Interview Question

Serializable vs Externalizable
What is the difference between Serializable and Externalizable in Java is famous core Java interview questions and for some of them its one of those difficult Java question, which no one wants to see in Java interview. I was in that category until I explored How Serialization works in Java and find out more about the Serialization process. What makes Serialization questions tricky is, Serialization as a persistence mechanism is not very popular. Many programmers prefer database, memory-mapped file or simple text file over Serialization. But Serialization has a distinguished advantage over these mechanisms.

Difference between throw vs throws in Java

throw vs throws in Java
throw vs throws in Java programming Exceptionthrow and throws are two Java keyword related to Exception feature of Java programming language. If you are writing Java program and familiar with What is Exception in Java, its good chance that you are aware of What is throw and throws in Java. In this Java tutorial, we will compare throw vs throws and see some worth noting difference between throw and throws in Java. Exception handling is an important part of Java programming language which enables you to write robust programs. There are five keywords related to Exception handling in Java e.g. try, catch, finally, throw and throws. Apart from the difference between final, finally and finalize,  throw vs throws is one of the frequently asked Java interview questions. Difference listed here will not only help you to answer this interview question but also understand concept behind throw and throws better.

How to iterate over HashSet in Java - loop or traverse Example

Iterating over HashSet in Java
Java program to Iterate over HashSet in Java with ExampleIn our last Java collection tutorial, we have seen How to iterate over ArrayList in Java and in this tutorial we will see How to iterate over HashSet in Java. There are two ways to iterate, loop or traverse over HashSet in Java, first using advanced for-each loop added on Java 5 and second using Iterator which is a more conventional way of iterating over HashSet in Java. Now questions are When should you use for loop and when Iterator is an appropriate option. Well I usually use for loop If I only read from HashSet and doesn't remove any element, while Iterator is preferred approach. You should not remove elements from HashSet while iterating over it in for loop, Use Iterator to do that.

How to convert Hexadecimal to Decimal, Binary and Octal in Java program - Example

Hexadecimal to decimal and binary conversion in Java
This Java program will convert Hexadecimal number to decimal, binary and Octal in Java programming language using JDK standard API methods. For beginners hexadecimal is base 16 number, while decimal is base 10, Octal is base 8, and binary is base 2 numbers in Number systems. Binary only contains 0 and 1 bits, octal contains 0 to 7, decimal contains 0 to 9 and Hexadecimal contains 0-9,A,B,C,D,E,F where F represent 15. Thankfully Java library provides a convenient method to convert any integer from one number system to another. In our last article we have seen How to convert decimal numbers to binary in Java and in this article we will convert Hexadecimal numbers to binary, octal, and decimal numbers.

How to convert ArrayList to String in Java - Spring Example

Some time we need to convert ArrayList to String in Java programming language in order to pass that String to stored procedure, any method or any other program. Unfortunately Java collection framework doesn't provide any direct utility method to convert ArrayList to String in Java. But Spring framework which is famous for dependency Injection and its IOC container also provides API with common utilities like method to convert Collection to String in Java. You can convert ArrayList to String using Spring Framework's StringUtils class. StringUtils class provide three methods to convert any collection e.g. ArrayList to String in Java, as shown below:

Difference between static and non static nested class in Java - Inner class

Static vs non Static class in Java
In Java, you can make a class either static or non-static. Now, what is the difference between making a class static vs non-static? well there is lot of difference between them. First of all there are two kinds of class in Java, one is called top level class and other is called nested class. As name suggested top level class is a class which is declared in .java file and not enclosed under any other class. On other hand nested class is declared inside another class. The class which enclosed nested class is known as Outer class. Now let's come back to static vs non static class. In Java programming language you can not make a top level class static. You can only make nested class either static or non static. If you make a nested class non static then it also referred as Inner class. Now let's come to the point regarding Difference between static and non static nested class in Java

20 EJB 3.0 Interview Questions and Answers - Java J2EE

EJB interview questions are core part of any Java J2EE interview. As EJB forms business layer for modern J2EE enterprise application, Good knowledge of EJB is expected from J2EE programmer. Purpose of these EJB interview questions is to give an Idea about what kind of questions you can expect on J2EE and EJB interviews. EJB was always tough for Java programmer because of heavy weight architecture comprised with many interfaces e.g. home interface, remote interface, local interface, bean class etc. It take too much time and knowledge to implement and use EJB in your Java web and enterprise application forget about challenges posed by application servers like WebLogic or IBM WebSphere.

Java Program to Read a File in Java - Windows UNIX Example

Java provides  package in JDK API for reading File in Java from File system e.g. C:\ or D:\ drive in Windows or any other directory in UNIX. First, you can use FileInputStream to open a file for reading. FileInputStream takes a String parameter which is a path for the file you want to read. Be careful while specifying File path as path separator is different on Window and UNIX. Windows uses back slash while UNIX uses forward slash as a path separator. By the way, Java supports forward slash in Windows, It means if use forward slash your program will work on both Windows and UNIX. Alternatively, you can also get PATH separator from System property path.separator. In this Java tutorial, we will code example of How to read File in Java.

How to use Java Enum in Switch Case Statement - Exampel Tutorial

Java Enum in Switch Case Statement
Yesterday, someone ask me Can we use Java Enum in Switch case? Obviously, he was learning Enum and not aware that How powerful Enum in Java is. Yes, You can use Enum in Switch case statement in Java like int primitive. If you are familiar with enum int pattern, where integers represent enum values prior to Java 5 then you already knows how to  use Switch case with Enum. Using Java Enum in the Switch case is pretty straightforward, Just use Enum reference variable in Switch and Enum constants or instances in CASE statement. In this Java tutorial we will see one example of How to use Enum in Switch statement in Java. By the way Enum is feature rich in Java, Enum can implement interface in Java, Enum can override method in Java, Enum can have constructor in Java and Enum is full functional Type like class or interface.

Java program to add two numbers - Example code how to

How to add two numbers in Java
In this Java program tutorial, we will add two numbers provided by the user on the command line. Write a Java program to add two numbers in Java is a common programming exercise for programmers and mostly asked in college exams, school homework, and Java courses as well. There are a different variant as well where you need to ask that write a method to add two numbers e.g. sum(int a, int b) and may ask you to get numbers as input from the user. Taking user input is a little bit tricky for Java programmers but once you know how to do that, it's rather simple.

Java program to get SubList from ArrayList - Example

Sometimes we need subList from ArrayList in Java. For example, we have an ArrayList of 10 objects and we only need 5 objects or we need an object from index 2 to 6, these are called subList in Java. Java collection API provides a method to get SubList from ArrayList. In this Java tutorial, we will see an example of getting SubList from ArrayList in Java. In this program, we have an ArrayList which contains 4 String objects. Later we call ArrayList.subList() method to get part of that List.

DOM vs SAX Parser in Java - XML Parsing in Java

DOM vs SAX parser in Java
DOM and SAX parser are two most popular parser used in Java programming language to parse XML documents. DOM and SAX concept are originally XML concept and Java programming language just provide an API to implement these parser. Despite both DOM and SAX are used in XML parsing, they are completely different to each other. In fact difference between DOM and SAX parser is a popular Java interview question asked during Java and XML interviews. DOM and SAX parser has different way of working, which makes Java programmer to understand difference between DOM and SAX parser even more important.

Java Program to convert String ArrayList to String Array

Converting String ArrayList into String array is very common programming task in Java. you often need to convert Array to Array List in Java  and vice-versa. In this Java program, we will How to convert String ArrayList to String array. This is also a common programming exercise which is asked too many Java programmers in various Java related courses. It’s also worth noting that ArrayList in Java is internally backed by array and Array in Java are objects much like String which is also an Object in Java. In this Java program, we first create an ArrayList which stores name of months as String e.g. Jan, Feb, and Mar. Later we use ArrayList  toArray() method to convert ArrayList into an array.

What is blank final variable in Java - Example

What is blank final variable in Java
Blank final variable in Java is a final variable which is not initialized while declaration, instead they are initialized in a constructor. Java compiler will complain if a blank final variable is not initialized during construction. If you have more than one constructor or overloaded constructor in your class then blank final variable must be initialized in all of them, failing to do so is a compile time error in Java. Alternatively, you can use constructor chaining to call one constructor from other using this keyword, in order to delegate initialization of blank final variable in Java. In this Java tutorial, we will see What is blank final variable in Java and a code example on How to use a blank final variable.

Autoboxing, Enum, Generics, Varargs methods - Java 5 Features Quick Overview

What is Autoboxing, Generics, Enum and Varargs method in Java 5
Java 5 introduces Autoboxing, Generics, varargs and Enum along with several other features and improvement. It's been few years when Java programming language was enhanced with these features but still Java programmer thing Autoboxing, Enum, Generics or Variable arguments as an advanced feature and afraid to learn them. They are very much part of Java fundamentals just like Abstraction, Inheritance, Encapsulation and Polymorphism are part of Object oriented programming concepts. It's important to understand what are these feature and How to use them, even if you don't use them in your code, you may have to work on someone else code which is written in Java 5 and uses Generics Collection, Autoboxing quite frequently. In this Java 5 tutorial, we will see the brief overview of Java 5 features like Generics, Enum, Autoboxing and unboxing and variable arguments methods along with examples and explanations.

What is Struts Action Class in Java J2EE - How to use

What is Action class in Struts
Struts in java is a framework, used to make web application its is based on Model View Controller or MVC design Pattern where Model represent the internal state and action used to change the state view represent presentation component and a controller is responsible for receiving the request from the client and decide which business logic should be called. Basically, Struts have different classes to represent this Model, View, and Controller we call them as Action, Action Form, and Action Servlet. So

Model – Action classes
View - Action form classes
Controller – Action Servlet classes

In this article, we are focusing on the Model layer of struts framework. Action class is used to provide an interface to application model layer.  What is Action class and how to use Action class is also a popular Struts interview Question asked in various J2EE interviews.

What is CopyOnWriteArrayList in Java - Example Tutorial

CopyOnWriteArrayList vs Array List in Java
CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations.

What is new in Java 7 - Top 5 JDK 7 feature

What is new in Java or JDK 7
It's close to a year now JDK 7 or Java 7 has been released, still programmer asks What is new in Java 7 ? What is the best feature introduced in JDK7, where is the list of all Java 7 new feature etc? I thought to let's document Top 5 new features introduced in Java 7 for easy reference, this will not only answer What is new in Java 7 but also provide a quick overview of What are those features in Java 7.  Java 7 introduced many new features in Java programming language like try catch with a resource, String in Switch etc but it also makes a lot of change on Java Development API by introducing new File API in Java and several other minor changes. E.g. you can now find hidden files from Java program without applying any hack. anyway let's see my list of Top 5 Java 7 features :

What is method overriding in Java – Example Tutorial

Method overriding in Java
Method overriding in Java is a concept based on polymorphism OOPS concept which allows the programmer to create two methods with the same name and method signature on the interface and its various implementation and the actual method is called at runtime depending upon the type of an object at runtime. Method overriding allows you to write flexible and extensible code in Java because you can introduce new functionality with the minimal code change. Method overriding is different than the method overloading in Java which we have discussed in the last article.

Difference between String and StringBuffer in Java

String vs StringBuffer in Java
String and StringBuffer are two classes which are most widely used in any Java program. If I say you can not write a program without using String in Java then it would not be an exaggeration. The string is everywhere, the main method accepts String argument, logs are String etc. Though many Java programmers familiar with String, not many are careful while performing operations on String, Since String is final in Java; every operation e.g. converting String into Uppercase, creating SubString,  converting String to Lowercase all result in a separate new String Object, which can take trigger frequent garbage collection and affect your application performance. here comes StringBuffer in Java which is a mutable version of String, though it's not as feature rich as String and you can not use StringBuffer in place of String but StringBuffer should be used whenever you are performing String concatenation instead of String in Java.

What is Thread and Runnable in Java - Example

What is Thread in Java?
Thread in Java is an independent path of execution that is used to run two tasks in parallel. When two threads run in parallel that is called multithreading in Java. Java is multithreaded from the start and excellent support of Thread at language level e.g. java.lang.Thread class, synchronized keyword, volatile and final keyword makes writing concurrent programs easier in Java than any other programming language e.g. C++. Being multi-threaded is also a reason for Java's popularity and being the number one programming language. On the other hand, if your program divides a task between two threads it also brings a lot of programming challenges and issues related to synchronization, deadlock, thread-safety, and race conditions.

What is class file in Java - How to create Class File

What is class file in Java
Class file in Java is compiled form of Java source file. When we compile Java program which is written in Java source file  ended with .java extension, it produces one more class files depending upon how many classes are declared and defined in that Java source file. One Java source file can only contain one public class and its name must match with name of file e.g. file can contain a public class whose name should be HelloWorld as shown below :

How to traverse iterate or loop ArrayList in Java

How to Loop ArrayList in Java
Iterating, traversing or Looping ArrayList in Java means accessing every object stored in ArrayList and performing some operations like printing them. There are many ways to iterate, traverse or Loop ArrayList in Java e.g. advanced for loop, traditional for loop with size(), By using Iterator and ListIterator along with while loop etc. All the method of Looping List in Java also applicable to ArrayList because ArrayList is an essentially List. In next section we will see a code example of Looping ArrayList in Java.

Difference between HashMap and ConcurrentHashMap in Java Collection

HashMap vs ConcurrentHashMap in Java
ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection class, that makes the main difference between HashMap and ConcurrentHashMap which is one is non-synchronized , non-thread safe and not for use in Concurrent multi-threaded environment while ConcurrentHashMap is a thread-safe collection and intended to be used as primary Map implementation especially for multi-threaded and Concurrent environment. Apart from thread-safety, there are some subtle differences between HashMap and ConcurrentHashMap which we will see in this article.