Difference between HashSet and HashMap in Java? Answered

HashSet and HashMap in Java
Hello friends, if you have given Java developer interview then there is good chance that you may have come across questions like Difference between HashSet vs HashMap or HashSet vs TreeSet etc. In this article, we are going to discuss differnece between HashMap and HashSet, two of the popular Collection classes from JDK. The HashSet vs HashMap is a classical Java Collection interview question that focuses on What are differences between HashSet and HashMap in terms of features, usage, and performance. If you are in Java programming even for a year or so, you are likely to be familiar with What is HashSet in Java and What is HashMap in Java, these two are the most popular collection classes. 

What is PATH and CLASSPATH in Java? Path vs ClassPath Example

What is PATH and CLASSPATH in Java
The PATH and CLASSPATH are the two most important environment variables of the Java environment which are used to find the JDK binaries used to compile and run Java in windows and Linux and class files which are compiled Java bytecodes. From my personal experience I can say that PATH and CLASSPATH are the two most problematic things for beginners in Java programming language due to two reasons; first because in most Java courses nobody tells details of what is a PATH and CLASSPATH, What do PATH and CLASSPATH do, What is meaning of setting PATH and CLASSPATH, What happens if we do not set them, Difference between PATH vs CLASSPATH in Java or simply How Classpath works in Java, etc. 

Why is main method public, static, and void in Java? Answer

What is the main method in Java?
The main() method in Java is a special method that is used by JVM to start the execution of any Java program. The main method is also referred to as the entry point of Java application which is true in the case of core Java applications because execution starts from the main() method, but in the case of container-managed environments like Servlet, EJB, or MIDlet this is not true as these Java programs have their own life-cycle methods like init(), service() or destroy() for Servlet's which is used by the container. The main method in Java is run by the main thread which is a non-daemon thread and the Java program runs until the main method finishes or any other user thread is running. 

How to convert JSON String to Java Object using Gson? JSON Deserialization Example

Hello Java Programmers, In the last article, you have learned how to convert a Java object to JSON String and in today's article, you will learn the opposite, i.e. converting a JSON String to a Java object. The first example was known as JSON serialization example and this one is known as JSON deserialization because we are creating a Java object from a String. The idea is very similar to classical Serialization in Java where you convert a Java object to another binary format that can be transported over the network or can be saved in the disk for further usage.

Difference between var, val, and def in Scala? Examples

Hello guys, if you are new to Scala or preparing for Scala interview then you may have come across the question, what is difference between var, val, and def keywords in Scala? This is one of the frequently asked questions from Scala interviews. Even though both var and val keyword is used to declare variables in Scala there is a subtle difference between them. A var is a variable. It’s a mutable reference to a value. Since it’s mutable, its value may change throughout the program's lifetime. On the other hand, the val keyword represents a value. It’s an immutable reference, meaning that its value never changes. Once assigned it will always keep the same value.

3 ways to convert String to JSON object in Java? Examples

It's very common nowadays to receive JSON String from a Java web service instead of XML, but unfortunately, JDK doesn't yet support conversion between JSON String to JSON object. Keeping JSON as String always is not a good option because you cannot operate on it easily, you need to convert it into a JSON object before you do anything else e.g. retrieve any field or set different values. Fortunately, there are many open-source libraries which allows you to create JSON object from JSON formatted String like Gson from Google, Jackson, and json-simple. In this tutorial, you will learn how to use these 3 main libraries to do this conversion with step-by-step examples.

Difference between Correlated and Non-Correlated Subquery in SQL? Examples

Hello guys, the difference between correlated and non-correlated subquery is one of the popular SQL interview question and an important concept for any programmer to learn. The correlated subquery is one of the tricky concepts of SQL. It's similar to recursion in programming which many programmers struggle to understand, but like recursion, it also offers the unique capability to solve many SQL query-based problems like the second-highest salary problem where you need to compare one row of the table to another row. It gives you a different kind of power. The main difference between a regular, non-correlated, and correlated subquery in SQL is in their working, a regular subquery just runs once and returns a value or a set of values that is used by the outer query.

How to sort ArrayList in Java? Examples

Hello Java programmers, sorting ArrayList in Java is not difficult; there are multiple ways to sort a given ArrayList in Java. For example you can use  the Collections.sort() method to  sort ArrayList in ascending and descending order in Java. The  Collections.sort() the method optionally accepts a Comparator. If provided, it uses Comparators' compare() method to compare Objects stored in Collection to compare with each other; in case of no explicit Comparator, Comparable interfaces' compareTo() method is used to compare objects from each other. If objects stored in ArrayList don't implement Comparable, they can not be sorted using the Collections.sort() the method in Java.

What is final modifier in Java? fina class, method, and variable example

Hello Java Programmers, the final variable is a very important concept in Java. It's a modifier that you can apply on variables, methods, and classes and when you apply the final modifier it can make variables immutable, prevent the method from overriding in subclasses, means no polymorphism, and when you make a class final in Java it cannot be extended anymore, taking out Inheritance from the picture. There are both pros and cons of using the final modifier in Java and that's why it's very very important for a Java developer to have an in-depth knowledge of the final modifier in Java and that's what you will learn in this article. 

Best way to Convert Integer to String in Java with Example

Integer to String conversion in Java
There are many ways to convert an Integer to String in Java e.g. by using Integer.toString(int) or by using String.valueOf(int), or by using new Integer(int).toString(), or by using String.format() method, or by using DecimalFormat, String concatenation, or by using StringBuilder and StringBuffer, etc but which one is the best? Some of them we have already seen on my earlier posts e.g. how to convert int to String and converting double to String.  Well, we are going to find the best way to convert Integer to String in Java in this post. 

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log files is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package java.util.zip to hold all classes related to zipping and unzipping files and streams. In this series of articles, you will learn how to use those classes like ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream, etc. 

How to add JAR files in Eclipse Project's Build path? Example

In this Java Eclipse tutorial, I will show you two ways to add external JAR files in Eclipse Java projects. Many times we need to use external JAR files in our Java application for different needs like for general purposes you may use Google Guava or Apache Commons. If you are using Spring or Hibernate framework then you need their JAR files as well. In order to use any third-party library or framework, we have to add their JAR files in the classpath, to compile and run our Java programs. Since Eclipse is the most popular IDE for developing Java applications, it's important to know how you can add external JARs into your Java project's build path. 

5 Tips to Fix Exception in thread "main" java.lang.NoClassDefFoundError in Java, Examples

NoClassDefFoundError in Java
The Exception in thread "main" java.lang.NoClassDefFoundError is a common error in Java that occurs if a ClassLoader cannot find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running the Java application. This error can occur to any thread, but if it happens in the main thread, then your program will crash because Java program runs only until main() thread is running or any other non-daemon thread is running. As per Javadoc, NoClassDefFoundError can be thrown during the linking or loading of the class file

14 Enum Interview Questions and Answers for 1 to 2 Years Experienced Java Programmers

Hello Java developers, Java Enum is one of the best features of Java Programming language which was first added on the JDK 1.5 Tiger release along with Generics, Autoboxing, and varargs. While the concept of Enum was also present in C Programming language, in Java its much more powerful. Enum in Java represents a fixed number of well-known things like the number of days in a week, the number of months in the calendar year, etc. Unlike C and C++ Enum in Java are much powerful and they are not an integer constant, Instead, Enum is a Type in Java like class or interface which provides compile-time type safety. In this Java Enum tutorial, we will see a couple of important points about Java Enum which help to understand Enum better and get the most out of it. 

How to convert String to char in Java? Example Tutorial

Hello Java Programmers, if you are wondering how to convert a String to char value in Java then you have come to the right place. Earlier, we have seen how to convert a character to String in Java and today we'll learn opposite i.e. converting String object to a character value. You know the difference right? String is an Object while char is a primitive value. So here we are also going to convert a Java object into a primitive value. By the way, this conversion only make sense if String is a single character. For example, you have a String with value "s", how do you convert that to a char 's' in Java (FYI, string literal are quoted inside double quotes in Java, like "c" and character literals are quoted inside single quotes e.g. 'c' in J)? Well, if you know, Java String is made of a character array and java.lang.String class provides a method toCharArray() to retrieve that character array.