NumberFormatException in Java and how to prevent it

NumberFormatException occurs in Java when we are trying to convert string into some numeric type but string is not in the correct format. It usually occurs when we parse string to integer, double, long etc datatypes but string is not present in correct format. Let us see an example code : Output Since ‘123a’ cannot … Read more

Local vs Instance vs Class vs Reference variable in Java

In Java we have 4 type of variables :- Local Variable Instance Variable Class variable Reference variable Java developers often get confused while using these terms, let us understand the difference between them in detail : Local Variable Local variables are those which are defined inside a block, method or constructor. These variables get destroyed … Read more

Value of PI in Java

PI value in mathematics is a very important value used in number of formulas. In java value of PI can be obtained through Math class of java.lang package. Math.PI is a double constant whose value = 3.141592653589793 Let us look at a program to calculate area of circle using Math.PI Output

Declaration vs Instantiation vs Initialization

Declaration, Instantiation and Initialization are very confusing terms in Java. Let us clear their meanings with example : Declaration A variable is said to be declared when we give name to variable and associate it with a class. Example :- Student stu; (here Student is a class) Instantiation Instantiation means we are creating an object … Read more

Selection Sorting in Java

In selection sorting each element is compared with all the following elements of the array. If the element is greater than any following element then swapping is done. After the first cycle we obtain the smallest element at the starting. Now in the second cycle, second element is compared with all following elements to obtain … Read more

Bubble Sorting in Java

Bubble sorting and Linear sorting are the easiest methods of sorting. Also they are the least optimized sorting algorithms. First we are going to look at bubble sorting in java. In Bubble sort each element is compared with its next element, if the element is greater than the next element then they are swapped. This … Read more

Sorting in Java

Sorting is the process through which elements can be arranged in ascending or descending order. Sorting can be done on numbers, strings etc. In Java inbuilt functions are also available to directly sort the series. These inbuilt functions are provided by Collections framework in Java. Example :- Unsorted series = 24, 20, 100, 203, 1, … Read more

Create a new Java project and program in Eclipse

In previous post we saw how to download eclipse and create a new workspace. Now let us see how to create a new Java project, program and run the program in Eclipse. Steps to create and run Java program : Step 1 : After eclipse has opened, a welcome screen will appear. You can explore … Read more