How to create and initialize List or ArrayList in one line in Java

creating and initializing List in same time
Sometime we want to create and initialize List like ArrayList or LinkedList in one line much like creating array and initializing it on same line. If you look Array on Java programming language you can create and initialize both primitive and object array e.g. String array very easily in just one line but in order to create a List equivalent of that array, you need to type lot of code. This is also one of the tricky Java question some time appears in Interview as Write Java code to create and initialize ArrayList in same line. In this Java tips we will see this trick which allow you to create and initialize List much like an Array. This tip can also save lot of time while creating test program or quickly trying some stuff.

How to read file in Java using Scanner Example - text files

Reading file with Scanner
From Java 5 onwards java.util.Scanner class can be used to read file in Java. Earlier we have seen example of reading file in Java using FileInputStream and reading file line by line using BufferedInputStream and in this Java tutorial we will See How can we use Scanner to read files in Java. Scanner is a utility class in java.util package and provides several convenient method to read int, long, String, double etc from source which can be an InputStream, a file or a String itself. As noted on How to get input from User, Scanner is also an easy way to read user input using (InputStream) as source.Main advantage of using Scanner for reading file is that it allows you to change delimiter using useDelimiter() method, So you can use any other delimiter like comma, pipe instead of white space.

Difference between == and equals() method in Java - String Object

What is the difference between == and equals() method for comparing Objects in Java is one of the classical Interview Questions which appears now and then in many interviews? This question is mostly asked in conjunction with String because comparing String using == and equals() method returns different results. I have often seen along with other popular String  question e.g. StringBuffer vs StringBuilder, Why String is final etc.  Java is a pure object-oriented language and every object has one state and location in the memory and equals () and == are related to the state and location of the object, now in this article will try to understand this concept and difference between == and equals method in Java.

Java Enum ValueOf Example - How to use

valueOf Example in Java Enum
valueOf method of Java Enum is used to retrieve Enum constant declared in Enum Type by passing String in other words valueOf method is used to convert String to Enum constants. In this Java Enum valueOf example we will see how to use valueOf method in Java. valueOf method is implicitly available to all Java Enum because every enum in Java implicitly extends java.lang.Enum class. valueOf method of enum accepts exactly same String which is used to declare Enum constant to return that Enum constant. valueOf method is case-sensitive and invalid String will result in IllegalArgumentException. In short String passed to valueOf method of Java enum must be same to String returned by name() method like returns RED and you should pass RED to valueOf() to get TrafficSignal.RED.

How to fix java.lang.unsupportedclassversionerror unsupported major.minor version 49.0 50.0 51.0 in Java

The java.lang.unsupportedclassversionerror unsupported major.minor version 50.0 error comes in Java environment when you compile your Java file in a higher Java version e.g. Java 1.6 and then trying to run the same Java program in lower Java version e.g. Java 1.5. Java is backward compatible i.e. you can run your Java 5 binary or JAR into Java or JRE 6 but vice-versa is not true. You can not run a Java program which is compiled in a higher Java version into lower JRE. class file version changes between every Java version and class file generated by javac command of Java 6 installation is not appropriate for JRE of Java 5 Installation.

How to sort Object in Java - Comparator and Comparable Example

Java Object Sorting Example
How do you sort a list of Objects in Java is one of the frequently asked coding questions in Java interviews and surprisingly not every Java programmers know How sorting of object happens in Java. Comparator and Comparable interface along with Collections.sort() method are used to sort the list of object in Java. compare() and compareTo() method of Comparator and Comparable interface provides comparison logic needed for sorting objects. compareTo() method is used to provide Object's natural order sorting and compare() method is used to sort Object with any arbitrary field. Almost all value classes in Java library e.g. String, Integer, Double, BigDecimal implement compareTo() to specify their natural sorting order.

Difference between Primary key vs Candidate Key in table - SQL database

Primary key vs Candidate Key
What is the difference between primary key and candidate key is another popular SQL and database interview questions which appear in various programming interviews now and then? The concept of primary key and candidate key is not just important from the interview point of view but also on designing databases and normalization. By the way, this is my second post about primary keys, In the last one, we have seen a comparison of primary key vs unique key, which also happens to be one of the frequently asked database questions. By definition primary key is a column or collection of columns, which uniquely defines a row in a table. Candidate keys are keys that can be a primary key and also able to uniquely identify any row in the table.

What is difference between Clustered and Non-Clustered Indexes in SQL - Database

Clustered vs Nonclustered Indexes in SQL
The difference between Clustered and Nonclustered index in a relational database is one of the most popular SQL interview questions almost as popular as the difference between truncate and deleteprimary key vs unique key and correlated vs noncorrelated subqueries. Indexes are a very important concept, it makes your queries run fast and if you compare a SELECT query which uses an indexed column to one who doesn't you will see a big difference in performance. There can be two kinds of indexes in relational database Clustered and Nonclustered indexes. A clustered index determines the physical sorting order of rows in a table similar to entries on yellow pages which are sorted in alphabetical order.

What is difference between primary key and unique key in table - SQL database

primary key vs unique key in SQL
The primary key and unique key are two important concepts in a relational database and used to uniquely identify a row in a table. Both primary key and unique keys can identify a row uniquely but there is some subtle difference between them which we will see in this article. In fact, primary key vs unique is a popular SQL interview questions along with classics like truncate vs delete and  How to manage transactions in a database, mostly asked to fresher and 2 to 3 years experience guys in any programming language. SQL is not just limited to any DBA or PLSQL developer but its an important skill even for Java programmer and you can expect SQL interview questions even in many Java interviews.

Java program to calculate Simple Interest - homework exercise

How to calculate simple interest in Java or Write a Java program to calculate Simple interest is a popular Java programming exercise people use in Java courses in school and college. So if you have recently got a homework exercise about calculating Simple interest in Java and wondering How to write a Java program then this tutorial can help you, provided you know What is simple interest and What is the formula of simple interest. If you are completely new in Java programming than I would suggest to try HelloWorld Example in Java and understand What is Path and Classpath in Java.

Could not create the Java virtual machine Invalid maximum heap size: -Xmx

"Could not create the Java virtual machine" is a general Java Virtual Machine error when you run java command directly or indirectly and it's not able to create a virtual machine because of invalid maximum heap size, invalid minimum heap size or just an error in command line. This error not only come when you run Java program from the command line but also when you run them using any IDE like Eclipse or Netbeans. Could not create the Java virtual machine Invalid maximum heap size -Xmx is a special case for it which comes when you try to run your java command with -Xmx option and value of that option is invalid like syntax error, value error, or anything else.

String contains() and indexOf() Example - Check String for SubString

Some time you want to check if one String contains another String or SubString or not e.g. "HelloWorld" contains "Hello" and "World" SubString. There are many ways to check SubString in String in Java e.g. contains(), indexOf() or matches() method of java.lang.String class. In our last article we have seen how to use matches method of String class with regular expression and In this article we will see example of contains() and indexOf() method to check any String or SubString in Java. contains and indexOf method are defined in java.lang.String class and used to check if String contains a particular SubString or not, only difference between contains() and indexOf() method is that, contains is added in Java 5 along with String matches() method while indexOf() is as old as equals(), hashCode and compareTo().

How to escape HTML Special characters in JSP and Java

Escaping HTML special characters in JSP or Java is a common task for Java programmers. There are many ways to escape HTML meta characters in Java, some of them we have already seen in last article escaping XML metacharacters in Java.  For those who are not familiar with HTML special characters, there are five e.g. < , >, &, ' and '' and if you want to print them literally just like here, Than you need to escape those character so < becomes &lt; , > becomes &gt; and so on. Of course, you can write your own custom tag or method for converting HTML special characters to entity format which browser understand but you don't need to do this because there are more easy and standard way to escape HTML special characters in JSP and Java. In this JSP and Java tutorial, we will learn about HTML special characters and explore some techniques to escape them in JSP pages and Java code. By the way, this is also a popular JSP Interview question mostly asked 2 years experience programmers.

ArrayList vs Vector in Java - Interview Question

ArrayList vs Vector in Java
ArrayList and Vector is two most widely used Collection class in Java and used to store object in ordered fashion. Every Java programmer which is introduced to Java Collection Framework either started with Vector or ArrayList. For beginners Difference between Vector and ArrayList in Java  and LinkedList vs ArrayList are two most  popular Java Interview question. ArrayList vs Vector is not only important on interview perspective but also on effective use of Java Collection API. After reading this article you will know when to use Vector in Java, When to use ArrayList in Java and would be able to compare ArrayList vs Vector over several important parameters e.g. Speed, Synchronization, Code quality etc. Before seeing difference on Vector vs ArrayList, let's What is common between the.

Java String Matches Example - Regular Expression Tutorial

String Matching Example in Java
String matches method in Java can be used to test String against regular expression in Java. String matches() method is one of the most convenient ways of checking if String matches a regular expression in Java or not. Quite often we need to write code that needs to check if String is numeric, Does String contains alphabets e.g. A to Z or a to Z, How to check if String contains a particular digit 6 times, etc. There are so many cases in Java programming where we need a regular expression. If you are familiar with Java's regular expression API than you must know about java.util.regex.Pattern and java.util.regex.Matcher class, these class provides regular expression capability to Java API.

What is difference between Overloading and Overriding in Java

Overloading vs Overriding in Java
In last couple of articles we have seen What is method overloading and What is method overriding in Java and now we will see What is difference between overloading and overriding in Java. Overloading vs overriding is one of those frequently asked Java interview question which can not be ignored. Overloading vs Overriding has appeared in almost every Java interview, mostly at beginner and intermediate level e.g. 2 to 4 years experience. In fact most of those tricky Java interview Question came from Overloading and Overriding. It's one of the tricky fundamental to understand. In this article we will some important difference between Overloading and Overriding which not only help to understand concept better but also serves as good recap for Java interviews.

Top 10 Java Web Service Interview question answers

Java Web Services Questions and Answers
Web Services interview questions are part of J2EE interviews for jobs which are looking some experience in Java web services Space. Most of the Web services questions come from two different way of implementing Web Services e.g. SOAP and REST. SOAP is an standard and mature way of calling Web Services which uses XML while REST is a new way of implementing Web Services which is based on HTTP protocol. In fact REST Web Service interview questions are getting more and more popular on web services interviews in Java, So if you are going for any Java J2EE interview which require some web service experience, be prepare with both SOAP and REST Web services questions.

keytool command examples Java - add view ssl certificate in keyStore trustStore

The keytool command in Java is a tool for managing certificates into keyStore and trustStore which is used to store certificates and requires during SSL handshake process. By using keytool command you can do many things but some of the most common operation is viewing certificate stored in keystore, importing new certificates into keyStore, delete any certificate from keystore etc. For those who are not familiar keyStore, trustStore and SSL Setup for Java application , Here is a brief overview on What is a trustStore and keyStore in Java. Both trustStore and keyStrore is used to store certificate signed by signer authority or CA (Certificate authority), with keyStore additionally storing personal certificate for client which is used during client authentication on SSL handshake process if its enable.

Difference between ServletConfig and ServletContext in JSP Servlet J2EE

Difference between ServletConfig and ServletContext
ServletContext and ServletConfig these two are important interface of Servlet API which is used by Java J2EE programmer during web application development. Correct understanding of What is ServletContext and ServletConfig is very important for any J2EE application developer. Apart from that Difference between ServletContext and ServletConfig are a popular Servlet JSP interview questions and mostly asked on both fresher and experienced Java programmer during J2EE interviews. Both ServletContext and ServletConfig are basically configuration objects which are used by servlet container to initialize various parameter of web application. But they have some difference in terms of scope and availability so first we see what is ServletContext and ServletConfig objects are and then we will see difference between ServletConfig and ServletContext in Java J2EE.

Rules of Method Overloading and Overriding in Java

The overloading and overriding rule in JavaSince you can either overload or override methods in Java, its important to know what are the rules of overloading and overriding in Java. any overloaded method or overridden method must follow rules of method overloading and method overriding to avoid compile-time error and logical runtime errors;  where you intend to override a method but method get overloaded. That is not uncommon and happens several times when Java programmer tries to override equals in Java or overriding compareTo method in Java while implementing Comparable interface, which accepts Object type of argument. From Java 5 onwards which introduces @Override annotation along with Enum, Generics, and varargs method you can completely avoid that problem.

Top 10 Tough Core Java Interview Questions Answers for Programmers

tough core Java interviews questions and answers
What is a tough core java interview question? Why do people look for tough Java questions before going for an interview? well, I don't think I need to answer these tough questions because it's pretty natural to prepare for tough questions even if you are not expecting tough questions from core Java. If you are prepared for the tough and tricky Java interview question then you feel more confident and answer other Java interview questions with confidence. On the other hand, if you are not prepared for tough and tricky core Java questions than seeing them on Java interview or written test may surprise you. But the definition of tough core Java questions is not universal, the same Java question which is easy for one programmer might be tough for other Java programmers.

Palindrome: Java program to check number is palindrome or not

How to find if a number is a palindrome in Java
Checking whether a number is palindrome or not is a classical Java homework exercise. When you start learning Java programming most institute which teaches Java programming provides these classical Java programs like How to find Armstrong number in Java or how to find the Fibonacci number in Java using recursion etc. Write a Java program to check if the number is palindrome comes from the same category. For those who are not familiar with palindrome numbers, a palindrome number is a number which is equal to reverse of itself. For example, 121 is a palindrome because the reverse of 121 is 121, while 123 is not a palindrome in Java because the reverse of 123 is 321 and 121!=321. Finding a palindrome number is easy in Java, you just need to develop logic to reverse a number in Java.

Can we overload static method in Java Program - Example

Overloading static method In Java
Yes, we can overload static method in Java. In terms of method overloading static method is just like normal methods and in order to overload the static method you need to provide another static method with the same name but different method signature. Static overloaded method is resolved using Static Binding during compile time. Overloading method in Java is completely different than the overriding method and as discussed in the last article we can not override static method in Java but we can certainly overload a static method in Java. Here is an example which confirms that we can overload static method in Java:

Can we override private method in Java - Example program

No, We can not override private method in Java, just like we can not override static method in Java. Like static methods, private method in Java is also bonded during compile time using static binding by Type information and doesn't depends on what kind of object a particular reference variable is holding. Since method overriding works on dynamic binding, its not possible to override private method in Java. private methods are not even visible to Child class, they are only visible and accessible in the class on which they are declared. private keyword provides highest level of Encapsulation in Java. Though you can hide private method in Java by declaring another private method with same name and different method signature.

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 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 :

What is transient variable in Java - Serialization Example

What is transient variable in Java
transient variable in Java is a variable whose value is not serialized during Serialization and which is initialized by its default value during de-serialization, for example for object transient variable it would be null. this behavior can be customized by using Custom Serialized form or by using Externalizable interface. transient variable is used to prevent any object from being serialized and you can make any variable transient by using transient keyword. By the way difference between transient and volatile variable in Java is a famous Java interview question but transient variable is completely different than volatile variable which we have discussed in our post What is volatile variable in Java.

Difference between HashSet and HashMap in Java

HashSet and HashMap in Java
HashSet vs HashMap is a classical Java Collection interview question which 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 most popular collection classes. Despite being hash based collection HashSet and HashMap are different to each other because the underlying interface is different. HashSet implements Set interface via extending AbstractSetclass and HashMap implements Map interface. Before seeing differences between let's see what is common between HashSet and HashMap in Java:

5 difference between Hashtable and HashMap in Java

Hashtable vs HashMap in Java
Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are a significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.Unlike Difference between ArrayList and HashMap,  Difference between Hashtable and HashMap are more subtle because both are similar kind of collection. Before seeing difference between HashMap and Hashtable let's see some common things between HashMap and Hashtable in Java.

Top 10 Java Programming Coding Interview Questions Answers for programmers

Java coding interview questions answers
Java coding interview questions are mostly some programming exercises which is asked to be solved in java programming language in limited time during any core Java or J2EE interview. No matter whether you have 2 years of experience or 4 years of experience, There is always some coding interview question in Java programming job you are applying. Actually questions related to Java Coding are increasingly getting popular on Java interviews for two reasons, first its difficult to crack Java coding interview questions than answering fact based questions like Why String is immutable in Java or Why main is static in Java; Second reason of popularity of Coding question in Java interviews is read need of good developers who are required to do lot of coding in projects rather than doing some maintenance works.

10 Advanced core Java interview questions for senior and experienced programmer

Java interview questions for Senior and Experienced programmer
Java is very big and there is no way to prepare completely for any core java interview but there is a level of question depends upon your experience, if you are fresher than questions asked in Java interview are mostly based on fundamentals like Iterator vs Enumeration in Java, Why main is public static and void or may be ArrayList vs LinkedList in Java. Things changes when you apply for senior developer, Technical lead or Team Lead Java positions, questions asked on that level are more advanced and less popular among Java circles, you may be asked questions from design pattern, questions from multi-threading, Collections and even asked to write code, design classes and prepare JUnit tests. In this Java article I will share some advanced Core Java interview questions which is appear in Senior level interviews mostly on 4 to 6 years and 6 to 8 years of experience. One important thing to note is that you can not clear interview by just mugging answers of these question because interviewer is most likely asked you follow-up question based upon your response and only way to get through it is to understand the topic well.

What is PATH and CLASSPATH in Java - Path vs ClassPath

What is PATH and CLASSPATH in Java
The PATH and CLASSPATH are two most important environment variables of Java environment which is 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 two most problematic thing for beginners in Java programming language due to two reasons; first because in most of Java courses nobody tell 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. These basic question which answers most of the details about PATH and CLASSPATH in Java are mostly not answered until Java programmer itself acquire these knowledge, Things may be changed nowadays but important of PATH and CLASSPATH is still high. Most common cause of dreaded error like java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException is either incorrect or misconfigured CLASSPATH in Java.

What is main method in Java ? Why main is public static and void

What is main method in Java
main method in Java is an standard method which is used by JVM to start execution of any Java program. main method is referred as entry point of Java application which is true in case of core java application but in case of container managed environment like Servlet, EJB or MIDlet this is not true as these Java programs have there own life-cycle methods like init(), service() or destory() for Servlet's which is used by container. main method in Java is run by main thread which is a non daemon thread and Java program runs until main method finishes or any other user thread is running. When we start JVM by running java command we also provide name of class which contains main method, which is later invoked by JVM to start Java program execution. for example in below command :

How to sort an ArrayList of Objects using Comparator in Java 8 - Ascending and Descending Order

There are a lot of examples of Sorting ArrayList in Java on the internet, but most of them use either String or Integer objects to explain sorting, which is not enough, because in real world you may have to sort a list of custom objects e.g. your domain or business objects like Employee, Book, Order, Trade etc. In order to sort an ArrayList of objects, you need two things, first a class to provider ordering and a method to provide sorting. If you know about ordering and sorting in Java then you know that Comparable and Comparator class is used to provide the ordering for objects. The Comparable interface provides natural order e.g. lexicographic order of String or name for Employees, while Comparator is used to provide custom order. It gives you the flexibility to sort your objects on the parameter you want e.g. you can sort a list of Coupon objects on the percentage discount, expiry dates, or based upon the total cost.

How to Remove Entry (key/value) from HashMap while Iterating? Example Tutorial

Can you remove a key while iterating over HashMap in Java? This is one of the interesting interview questions as well as a common problem Java developer face while writing code using HashMap. Some programmer will say No, you cannot remove elements from HashMap while iterating over it. This will fail fast and throw concurrent modification exception. They are right but not completely. It's true that the iterator of HashMap is a fail-fast iterator but you can still remove elements from HashMap while iterating by using Iterator's remove() method. This is the same technique which we have used in past to remove elements while iterating over a List in Java. The key here is not to use the remove() method from Map or List interface to avoid java.util.ConcurrentModfiicationException in Java.

How to convert String to char in Java? Example

Earlier, we have seen how to convert a character to String in Java and today we'll learn opposite i.e. converting String to a character. Suppose, 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 e.g. "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. If your String contains just one character then, the only element in character array is what you are looking after. Though that's not the only way to convert a String to char in Java.

Difference between Correlated and Non-Correlated Subquery in SQL

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 e.g. 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 run once and return a value or a set of values which is used by the outer query, but correlated subquery runs for each row returned by the outer query because the output of the whole query is based upon comparing the data returned by one row to the all other rows of the table. That's why it is also very slow and generally avoided until you don't know any other way to solve the problem.

10 points about final modifier in Java

Here are some important details about final modifier in Java:

1) The final is a modifier in Java, which can be applied to a variable, a method or a class.

2) When a final modifier is used with a class then the class cannot be extended further. This is one way to protect your class from being subclassed and often sensitive classes are made final due to security reason. This is also one of the reasons why String and wrapper classes are final in Java.

3) When the final keyword is used with a method that it cannot be overridden in Java, which means you cannot override the logic of the method in the subclass. This is also done to protect the original logic of method.

Difference between var, val, and def 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 through the program lifetime. On the other hand, 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. It’s similar to a final variable in Java or constants in other languages. It's also worth to remember that the variable type cannot change in Scala. You may say that a var behaves similarly to Java variables.

How to convert JSON String to Java Object - Gson/JSON Deserialization Example

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 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 which can be transported over the network or can be saved in the disk for further usage. That's why the process of converting a Java object to JSON is known as serialization and converting a JSON document to Java object is known as De-Serialization. You can use any JSON library to perform serialization and de-serialization e.g. Jackson Databind, Gson, or Json-simple.  In this program, I'll show you how to use Gson to create a Java object from given JSON String.

How to convert Java object to JSON String - Gson Java/JSON Serialization Example

If you are a Java or Android developer and learning JSON to support JSON format for your project and looking for a quick and simple way to convert your Java object into json format then you have come to the right place. In this article, I'll teach you how to convert a Java object to JSON document using Google's Java library called Gson. This library allows you to convert both Java object to JSON String and a JSON document to Java object. So, let's first do some serialization. Serialization in the context of Gson means mapping a Java object to its JSON representation. In the real world, the data can be really complex with lots of attributes and nested with composed object but for now, we'll start with a class which just contain few value attributes e.g. String, int and boolean.

Java Program to calculate Perimeter/Circumference of Circle

One of the common programming exercise for beginners is to write a program to calculate the perimeter or circumference of a circle in Java. This exercise is not just common in Java but also on other programming language courses e.g. C, C++ or Python. I first did this exercise on C++ long ago, but that time the joy of a working program was something different. I was quite happy to see my program compiled without error and can print the correct circumference after entering radius. That program taught me how to take input from a user and how to display output into the console, I didn't know how to create methods to encapsulate code. For us, only main method matters at that time. Anyway, let's focus on the job at hand i.e. how to find the circumference of Circle of given radius. You have to accept radius from the user and show the output to console.

How to add JAR file in Eclipse Project Build path - External dependency

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 e.g. for general purpose 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. For example, suppose, you have downloaded Apache commons JAR file from the internet and wants to add that file to your Java project. In order to use classes and methods from that library, your project must reference those and this is what we will learn here.

How to sort ArrayList in java - List Sorting - Ascending Descending Order

How to sort ArrayList ascending descending order 
Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() the method optionally accepts a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If objects stored in ArrayList don't implement Comparable then they can not be sorted using Collections.sort() the method in Java.