Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Category: Java

Java Runtime Environment contains Java Virtual machine, libraries and other components to run applets and applications written in Java.
JVM + libraries+other components= JRE

Category: Java

Java virtual machine helps run java bytecodes. when .java file are compiled using javac compiler into .class file. These .class file contain bytecode understood by Java virtual machine.

Category: Java

Java Development Kit (JDK) contains JRE and compile rs and debuggers for developing applets and applications.

Category: Java

JVM help run java byte codes created by compiling .java files into .class files.
JRE contains JVM (Java Virtual Machine), libraries and other components t o run Java applets and applications.
JDK contains JRE, compilers and debugger (development environment for applications using Java programming language).

Category: Java

(Java Runtime Environment) JRE contains Java Virtual Machine (JVM) + Java packages (util, math, lang, awt, etc) + runtime libraries You need to install JRE on your machine in order to run Java applications or applets.
Java compiler Javac converts .java file into .class files which contain bytecode, (Java Virtual Machine) JVM converts bytecode into machine language. JVM is platform dependent, there are different JVM for different OS, Bytecode converted into machine language for particular machine, executes depending on its kernel. Without JVM you cannot run Java Applications.

Category: Java

Both compilers and interpreters are used to convert program written in high level language into machine code understood by computers.
Interpreter converts program one statement at a time, takes less time in analyzing source code, slower than compiler, there is no intermediate object formed, hence memory efficient.
Compilers scans entire program and then converts it as a whole into machine language, takes more time analyzing source code, however it is faster than interpreter, generates intermediate object code hence takes more memory.
Programming languages like JavaScript, Python and Ruby use interpreters.
Programming languages like C, C++ and Java use compilers.

Category: Java

Java doesn’t support multiple inheritance in classes because of “Diamond Problem”.
However multiple inheritance is supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of diamond problem with interfaces.

Category: Java

Java is not said to be pure object oriented because it support primitive types such as int, byte, short, long etc. I believe it brings simplicity to the language while writing our code.
Obviously java could have wrapper objects for the primitive types but just for the representation, they would not have provided any benefit.
As we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.

Category: Java

PATH is an environment variable used by operating system to locate the executables. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable.
Classpath is specific to java and used by java executables to locate class files. We can provide the classpath location while running java application and it can be a directory, ZIP files, JAR files etc.

Category: Java

main() method is the entry point of any standalone java application. The syntax of main method is public static void main(String args[]).
main method is public and static so that java can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program.

Category: Java

When we have more than one method with same name in a single class but the argument are different, then it is called as method overloading.
Overriding concept comes in picture with inheritance when we have two methods with same signature, one in parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if parent class method is changed, so as child class.

Category: Java

Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args[]).

Category: Java

We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.

Category: Java

Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modules based. A java class fully classified name contains package and class name. For example, java.lang.Object is the fully classified
name of Object class that is part of java.lang package.
java.lang package is imported by default and we don’t need to import any class from this package explicitly.

Category: Java

Java provides access control through public, private and protected access modifier keywords. When none of these are used, it’s called default access modifier.
A java class can only have public or default access modifier.

Category: Java

final keyword is used with Class to make sure no other class can extend it, for example String class is final and we can’t extend it.
We can use final keyword with methods to make sure child classes can’t override it.
final keyword can be used with variables to make sure that it can be assigned only once.
However the state of the variable can be changed, for example we can assign a final variable to an object only once but the object variables can change later on.
Java interface variables are by default final and static.

Category: Java

static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.
static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

Category: Java

We can’t declare a top-level class as static however an inner class can be declared as static.
If inner class is declared as static, it’s called static nested class. Static nested class is same as any other top-level class and is nested for only packaging convenience.

Category: Java

If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.
import java.lang.Math;
//inside class
double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.
import static java.lang.Math.PI;
//no need to refer class now
double test = PI * 5;
Use of static import can cause confusion, so it’s better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.

Category: Java

Java 7 one of the improvement was multi-catch block where we can catch multiple exceptions in a single catch block. This makes are code shorter and cleaner when every catch block has similar code.
If a catch block handles multiple exception, you can separate them using a pipe (|) and in this case exception parameter (ex) is final, so you can’t change it

Category: Java

Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded.

Category: Java

Interfaces are core part of java programming language and used a lot not only in JDK but also java design patterns, most of the frameworks and tools. Interfaces provide a way to achieve abstraction in java and used to define the contract for the subclasses to implement.
Interfaces are good for starting point to define Type and create top level hierarchy in our code. Since a java class can implements multiple interfaces, it’s better to use interfaces as super class in most of the cases.

Category: Java

Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have abstract method without body and it can have methods with implementation also.
abstract keyword is used to create an abstract class. Abstract classes can’t be instantiated and mostly used to provide base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in abstract class.

Category: Java
  • abstract keyword is used to create abstract class whereas interface is the keyword for interfaces.
  • Abstract classes can have method implementations whereas interfaces can’t.
  • A class can extend only one abstract class but it can implement multiple interfaces.
  • We can run abstract class if it has main() method whereas we can’t run an interface.
Category: Java

Interfaces don’t implement another interface, they extend it. Since interfaces can’t have method implementations, there is no issue of diamond problem. That’s why we have multiple inheritance in interfaces i.e an interface can extend multiple interfaces.

Category: Java

A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.

Category: Java

Enum was introduced in Java 1.5 as a new type whose fields consists of fixed set of constants. For example, in Java we can create Direction as enum with fixed fields as EAST, WEST, NORTH, SOUTH.
enum is the keyword to create an enum type and similar to class. Enum constants are implicitly static and final.

Category: Java

Java Annotations provide information about the code and they have no direct effect on the code they annotate. Annotations are introduced in Java 5. Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or
by compiler. We can also specify annotation availability to either compile time only or till runtime also. Java Built-in annotations are @Override, @Deprecated and @SuppressWarnings.

Category: Java

Java ternary operator is the only conditional operator that takes three operands. It’s a one liner replacement for if-then-else statement and used a lot in java programming. We can use ternary operator if-else conditions or even switch conditions using nested ternary operators.

Category: Java

super keyword can be used to access super class method when you have overridden the method in the child class.
We can use super keyword to invoke super class constructor in child class constructor but in this case it should be the first statement in the constructor method.

Category: Java

We can use break statement to terminate for, while, or do-while loop. We can use break statement in switch statement to exit the switch case. You can see the example of break statement at java break. We can use break with label to terminate the nested loops.
The continue statement skips the current iteration of a for, while or do-while loop. We can use continue statement with label to skip the current iteration of outermost loop

Category: Java

this keyword provides reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having same name.

classTest

{

    inta;

    intb;

    // Parameterized constructor

    Test(inta, intb)

    {

        this.a = a;

        this.b = b;

    }

    voiddisplay()

    {

        //Displaying value of variables a and b

        System.out.println("a = "+ a + "  b = "+ b);

    }

    publicstaticvoidmain(String[] args)

    {

        Test object = newTest(10, 20);

        object.display();

    }

}

Category: Java

No argument constructor of a class is known as default constructor. When we don’t define any constructor for the class, java compiler automatically creates the default no-args constructor for the class. If there are other constructors defined, then compiler won’t create
default constructor for us.

Category: Java

Yes, we can have try-finally statement and hence avoiding catch block.

Category: Java

Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. In Java, process of deallocating memory is handled automatically by the garbage collector.
We can run the garbage collector with code Runtime.getRuntime().gc() or use utility method System.gc().

Category: Java

Java System Class is one of the core classes. One of the easiest way to log information for debugging is System.out.print() method.
System class is final so that we can’t subclass and override it’s behavior through inheritance. System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of it’s methods are static.
Some of the utility methods of System class are for array copy, get current time, reading environment variables.

Category: Java
  • Pass by value: The method parameter values are copied to another variable and then the copied object is passed to the method. The method uses the copy.
  • Pass by reference: An alias or reference to the actual parameter is passed to the method. The method accesses the actual parameter.

Often, the confusion around these terms is a result of the concept of the object reference in Java. Technically, Java is always pass by value, because even though a variable might hold a reference to an object, that object reference is a value that represents the object’s location in memory. Object references are therefore passed by value.

Both reference data types and primitive data types are passed by value. 

Category: Java

Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.
 Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it.

Stack memory only contains local primitive variables and reference variables to objects in heap space.
 Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally.

Category: Java

String is a Class in java and defined in java.lang package. It’s not a primitive data type like int and long. String class represents character Strings. String is used in almost all the Java applications and there are some interesting facts we should know about String. String is
immutable and final in Java and JVM uses String Pool to store all the String objects.
Some other interesting things about String is the way we can instantiate a String object using double quotes and overloading of “+” operator for concatenation.

Category: Java

We can create String object using new operator like any normal java class or we can use double quotes to create a String object. There are several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilder.
String str = new String(“abc”);
String str1 = “abc”;
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

Category: Java

A String is said to be Palindrome if its value is same when reversed. For example “aba” is a Palindrome String.
String class doesn’t provide any method to reverse the String but StringBuffer and StringBuilder class has reverse method that we can use to check if String is palindrome or not.
private static boolean isPalindrome(String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
}

Category: Java

We can use replaceAll method to replace all the occurance of a String with another String. The important point to note is that it accepts String as argument, so we will use Character class to create String and use it to replace all the characters with empty String.
private static String removeChar(String str, char c) {
if (str == null)
return null;
return str.replaceAll(Character.toString(c), “”);
}

Category: Java

We can use String class toUpperCase and toLowerCase methods to get the String in all upper case or lower case. These methods have a variant that accepts Locale argument and use that locale rules to convert String to upper or lower case.

Category: Java

Below are 5 ways to compare two Strings in Java:

  1. Using user-defined function : Define a function to compare values with following conditions :
    • if (string1 > string2) it returns a positive value.
    • if both the strings are equal lexicographically
      i.e.(string1 == string2) it returns 0.
    • if (string1 < string2) it returns a negative value.
  2. Using String.equals()
    • str1.equals(str2);
  3. Using String.equalsIgnoreCase()
    • str2.equalsIgnoreCase(str1);
  4. Using Objects.equals() : Objects.equals(Object a, Object b)
    • Objects.equals(string1, string2);
  5. Using String.compareTo()
    • int str1.compareTo(String str2);
Category: Java

This is a tricky question because String is a sequence of characters, so we can’t convert it to a single character. We can use use charAt method to get the character at given index or we can use toCharArray() method to convert String to character array.

import java.util.Arrays;

public class StringChar {

public static void main(String[] args) {
    String st = "This is great";

    char[] chars = st.toCharArray();
    System.out.println(Arrays.toString(chars));
}

}

Output

[T, h, i, s, , i, s, , g, r, e, a, t]

Category: Java

We can use String getBytes() method to convert String to byte array and we can use String constructor new String(byte[] arr) to convert byte array to String.

package com.javaprogramto.arrays.bytearray;

public class StrintToByteArrayExample {

public static void main(String[] args) {

    // creating string
    String string = "javaprogrmto.com";

    // string to byte array
    byte[] array1 = string.getBytes();

    // printing byte array
    for (int i = 0; i < array1.length; i++) {
        System.out.print(" " + array1[i]);
    }
}

}

Output:

106 97 118 97 112 114 111 103 114 109 116 111 46 99 111 109

Category: Java

This is a tricky question used to check your knowledge of current Java developments. Java 7 extended the capability of switch case to use Strings also, earlier java versions doesn’t support this.
If you are implementing conditional flow for Strings, you can use if-else conditions and you can use switch case if you are using Java 7 or higher versions.

public class Demo {
public static void main(String[] args) {
String department = “AKD05”;
switch(department) {
case “AKD01”:
System.out.println(“Finance”);
break;
case “AKD02”:
System.out.println(“Sales”);
break;
case “AKD03”:
System.out.println(“Production”);
break;
case “AKD04”:
System.out.println(“Marketing”);
break;
case “AKD05”:
System.out.println(“Operations”);
break;
default:
System.out.println(“None!”);
}
}
}

Output

Operations

Category: Java

// Java program to print all the permutations
// of the given string
public class GFG {

// Function to print all the permutations of str
static void printPermutn(String str, String ans)
{

    // If string is empty
    if (str.length() == 0) {
        System.out.print(ans + " ");
        return;
    }

    for (int i = 0; i < str.length(); i++) {

        // ith character of str
        char ch = str.charAt(i);

        // Rest of the string after excluding
        // the ith character
        String ros = str.substring(0, i) +
                    str.substring(i + 1);

        // Recursive call
        printPermutn(ros, ans + ch);
    }
}

// Driver code
public static void main(String[] args)
{
    String s = "abb";
    printPermutn(s, "");
}

}

Output

abb abb bab bba bab bba

Category: Java
// program to check if the string is palindrome or not

function checkPalindrome(string) {

    // find the length of a string
    const len = string.length;

    // loop through half of the string
    for (let i = 0; i < len / 2; i++) {

        // check if first and last string are same
        if (string[i] !== string[len - 1 - i]) {
            return 'It is not a palindrome';
        }
    }
    return 'It is a palindrome';
}

// take input
const string = prompt('Enter a string: ');

// call the function
const value = checkPalindrome(string);

console.log(value);
anana
radar

Output

Category: Java

String is immutable and final in java, so whenever we do String manipulation, it creates a new String. String manipulations are resource consuming, so java provides two utility classes for String manipulations – StringBuffer and StringBuilder.
StringBuffer and StringBuilder are mutable classes. StringBuffer operations are threadsafe and synchronized where StringBuilder operations are not thread-safe. So when multiple threads are working on same String, we should use StringBuffer but in single threaded environment we should use StringBuilder.
StringBuilder performance is fast than StringBuffer because of no overhead of synchronization.

Category: Java
  • There are several benefits of String because it’s immutable and final. String Pool is possible because String is immutable in java.
  • It increases security because any hacker can’t change its value and it’s used for storing sensitive information such as database username, password etc.
  • Since String is immutable, it’s safe to use in multi-threading and we don’t need any synchronization.
  • Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader.
Category: Java

// Java program to demonstrate working of split(regex, limit) with small limit.

publicclassGFG {

    // Main driver method

    publicstaticvoidmain(String args[])

    {

        // Custom input string

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", 2);

        for(String a : arrOfStr)

            System.out.println(a);

    }

}

Category: Java

String is immutable in java and stored in String pool. Once it’s created it stays in the pool until unless garbage collected, so even though we are done with password it’s available in memory for longer duration and there is no way to avoid it. It’s a security risk because anyone having access to memory dump can find the password as clear text.
If we use char array to store password, we can set it to blank once we are done with it. So we can control for how long it’s available in memory that avoids the security threat with String.

Category: Java

There are two ways to check if two Strings are equal or not – using “==” operator or using equals method. When we use “==” operator, it checks for value of String as well as reference but in our programming, most of the time we are checking equality of String for value only. So we should use equals method to check if two Strings are equal or not.
There is another function equalsIgnoreCase that we can use to ignore case.
String s1 = “abc”;
String s2 = “abc”;
String s3= new String(“abc”);
System.out.println(“s1 == s2 ? “+(s1==s2)); //true
System.out.println(“s1 == s3 ? “+(s1==s3)); //false
System.out.println(“s1 equals s3 ? “+(s1.equals(s3))); //true

Category: Java

As the name suggests, String Pool is a pool of Strings stored in Java heap memory. We know that String is special class in java and we can create String object using new operator as well as providing values in double quotes.

Category: Java

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String
object is returned.
This method always return a String that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

Category: Java

Strings are immutable, so we can’t change its value in program. Hence it’s thread-safe and can be safely used in multi-threaded environment.

Category: Java

Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as
HashMap keys.

Category: Java

Collection is the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Java platform doesn’t provide any direct implementations of this interface.
Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the deck of cards.
List is an ordered collection and can contain duplicate elements. You can access any element from its index. List is more like array with dynamic length.
A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.

Category: Java

In Java, an Iterator is one of the Java cursors. Java Iterator is an interface that is practiced in order to iterate over a collection of Java object components entirety one by one.
The Java Iterator is also known as the universal cursor of Java as it is appropriate for all the classes of the Collection framework. The Java Iterator also helps in the operations like READ and REMOVE.
Java Iterator Methods
The following figure perfectly displays the class diagram of the Java Iterator interface. It contains a total of four methods that are:

hasNext()
next()
remove()
forEachRemaining()

import java.io.*;  

import java.util.*;  

    

public class JavaIteratorExample {  

    public static void main(String[] args)  

    {  

        ArrayList cityNames = new ArrayList();  

       cityNames.add(“Delhi”);  

        cityNames.add(“Mumbai”);  

        cityNames.add(“Kolkata”);  

        cityNames.add(“Chandigarh”);  

        cityNames.add(“Noida”);  

            // Iterator to iterate the cityNames  

        Iterator iterator = cityNames.iterator();  

            System.out.println(“CityNames elements : “);  

    

        while (iterator.hasNext())  

            System.out.print(iterator.next() + ” “);  

            System.out.println();  

    }  

}  

Category: Java

Enumeration is twice as fast as Iterator and uses very less memory. Enumeration is very basic and fits to basic needs. But Iterator is much safer as compared to Enumeration because it always denies other threads to modify the collection object which is being iterated by it.
Iterator takes the place of Enumeration in the Java Collections Framework. Iterators allow the caller to remove elements from the underlying collection that is not possible with Enumeration. Iterator method names have been improved to make its functionality clear.

Category: Java
  • We can use Iterator to traverse Set and List collections whereas ListIterator can be used with Lists only.
  • Iterator can traverse in forward direction only whereas ListIterator can be used to traverse in both the directions.
  • ListIterator inherits from Iterator interface and comes with extra functionalities like adding an element, replacing an element, getting index position for previous and next elements.
Category: Java

HashMap and Hashtable both implements Map interface and looks similar, however there are following difference between HashMap and Hashtable.

  • HashMap allows null key and values whereas Hashtable doesn’t allow null key andvalues.
  • Hashtable is synchronized but HashMap is not synchronized. So HashMap is better for single threaded environment, Hashtable is suitable for multi-threaded environment.
  • LinkedHashMap was introduced in Java 1.4 as a subclass of HashMap, so incase you want iteration order, you can easily switch from HashMap to LinkedHashMap but that is not the case with Hashtable whose iteration order is unpredictable.
  • HashMap provides Set of keys to iterate and hence it’s fail-fast but Hashtable provides Enumeration of keys that doesn’t support this feature.
  • Hashtable is considered to be legacy class and if you are looking for modifications of Map while iterating, you should use ConcurrentHashMap.
Category: Java

For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative. If, however, you need to traverse the keys in a sorted order, then TreeMap is your better alternative. Depending upon the size of your collection, it may be faster to add elements to a HashMap, then convert the map to a TreeMap for sorted key traversal.

Category: Java

ArrayList and Vector are similar classes in many ways.

  • Both are index based and backed up by an array internally.
  • Both maintains the order of insertion and we can get the elements in the order of insertion.
  • The iterator implementations of ArrayList and Vector both are fail-fast by design.
  • ArrayList and Vector both allows null values and random access to element using index number.

These are the differences between ArrayList and Vector.

  • Vector is synchronized whereas ArrayList is not synchronized. However if you are looking for modification of list while iterating, you should use CopyOnWriteArrayList.
  • ArrayList is faster than Vector because it doesn’t have any overhead because of synchronization.
  • ArrayList is more versatile because we can get synchronized list or read-only list from it easily using Collections utility class
Category: Java
  • Arrays can contain primitive or Objects whereas ArrayList can contain only Objects.
  • Arrays are fixed size whereas ArrayList size is dynamic.
  • Arrays doesn’t provide a lot of features like ArrayList, such as addAll, removeAll, iterator etc.

Although ArrayList is the obvious choice when we work on list, there are few times when array are good to use.

  • If the size of list is fixed and mostly used to store and traverse them.
  • For list of primitive data types, although Collections use autoboxing to reduce the coding effort but still it makes them slow when working on fixed size primitive data types.
  • If you are working on fixed multi-dimensional situation, using [][] is far more easier than List<List>
Category: Java

ArrayList and LinkedList both implement List interface but there are some differences between them:

  • ArrayList is an index based data structure backed by Array, so it provides random access to its elements with performance as O(1) but LinkedList stores data as list of nodes where every node is linked to its previous and next node. So even though there is a method to get the element using index, internally it traverse from start to reach at the index node and then return the element, so performance is O(n) that is slower than ArrayList.
  • Insertion, addition or removal of an element is faster in LinkedList compared to ArrayList because there is no concept of resizing array or updating index when element is added in middle.
  • LinkedList consumes more memory than ArrayList because every node in LinkedList stores reference of previous and next elements.
Category: Java

Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment. Java 1.5 Concurrent API included some collection classes that allows modification of collection while iteration because they work on the clone of the collection, so they are safe to use in multi-threaded environment.

Category: Java

Both Queue and Stack are used to store data before processing them. java.util.Queue is an interface whose implementation classes are present in java concurrent package. Queue allows retrieval of element in First-In-First-Out (FIFO) order but it’s not always the case.
There is also Deque interface that allows elements to be retrieved from both end of the queue.
Stack is similar to queue except that it allows elements to be retrieved in Last-In-First-Out (LIFO) order.
Stack is a class that extends Vector whereas Queue is an interface.

Category: Java

java.util.Collections is a utility class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other
odds and ends.
This class contains methods for collection framework algorithms, such as binary search, sorting, shuffling, reverse etc.

Category: Java
ComparableComparator
1) Comparable provides a single sorting sequence. In other words, we can sort the collection on the basis of a single element such as id, name, and price.The Comparator provides multiple sorting sequences. In other words, we can sort the collection on the basis of multiple elements such as id, name, and price etc.
2) Comparable affects the original class, i.e., the actual class is modified.Comparator doesn’t affect the original class, i.e., the actual class is not modified.
3) Comparable provides compareTo() method to sort elements.Comparator provides compare() method to sort elements.
4) Comparable is present in java.lang package.A Comparator is present in the java.util package.
5) We can sort the list elements of Comparable type by Collections.sort(List) method.We can sort the list elements of Comparator type by Collections.sort(List, Comparator) method.
Category: Java

If we need to sort an array of Objects, we can use Arrays.sort(). If we need to sort a list of objects, we can use Collections.sort(). Both these classes have overloaded sort() methods for natural sorting (using Comparable) or sorting based on criteria (using Comparator).
Collections internally uses Arrays sorting method, so both of them have same performance except that Collections take sometime to convert list to array.

Category: Java

Exception is an error event that can happen during the execution of a program and disrupts its normal flow. Exception can arise from different kind of situations such as wrong data entered by user, hardware failure, network connection failure etc.
Whenever any error occurs while executing a java statement, an exception object is created and then JRE tries to find exception handler to handle the exception. If suitable exception handler is found then the exception object is passed to the handler code to process the exception, known as catching the exception. If no handler is found then application throws the exception to runtime environment and JRE terminates the program.
Java Exception handling framework is used to handle runtime errors only, compile time errors are not handled by exception handling framework.

Category: Java
  • throw: Sometimes we explicitly want to create exception object and then throw it to halt the normal processing of the program. throw keyword is used to throw exception to the runtime to handle it.
  • throws: When we are throwing any checked exception in a method and not handling it, then we need to use throws keyword in method signature to let caller program know the exceptions that might be thrown by the method. The caller method might handle these exceptions or propagate it to its caller method using throws keyword. We can provide multiple exceptions in the throws clause and it can be used with main() method also.
  • try-catch: We use try-catch block for exception handling in our code. try is the start of the block and catch is at the end of try block to handle the exceptions. We can have multiple catch blocks with a try and trycatch block can be nested also. catch block requires a parameter that should be of type Exception.
  • finally: finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. Finally block gets executed always, whether exception occurrs or not.
Category: Java

Java Exceptions are hierarchical and inheritance is used to categorize different types of exceptions. Throwable is the parent class of Java Exceptions Hierarchy and it has two child objects – Error and Exception. Exceptions are further divided into checked exceptions and runtime exception.
Errors are exceptional scenarios that are out of scope of application and it’s not possible to anticipate and recover from them, for example hardware failure, JVM crash or out of memory error.
Checked Exceptions are exceptional scenarios that we can anticipate in a program and try to recover from it, for example fileNotFoundException. We should catch this exception and provide useful message to user and log it properly for debugging purpose. Exception is the parent class of all CheckedExceptions.
Runtime Exceptions are caused by bad programming, for example trying to retrieve an element from the Array. We should check the length of array first before trying to retrieve the element otherwise it might throw ArrayIndexOutOfBoundException at runtime. RuntimeException is the parent class of all runtime exceptions.

Category: Java

Exception and all of its subclasses doesn’t provide any specific methods and all of the methods are defined in the base class Throwable.

String getMessage() – This method returns the message String of Throwable and the message can be provided while creating the exception through it’s constructor.

String getLocalizedMessage() – This method is provided so that subclasses can override it to provide locale specific message to the calling program. Throwable class implementation of this method simply use getMessage() method to return the exception message.

synchronized Throwable getCause() – This method returns the cause of the exception or null id the cause is unknown.

String toString() – This method returns the information about Throwable in String format, the returned String contains the name of Throwable class and localized message.

void printStackTrace() – This method prints the stack trace information to the standard error stream, this method is overloaded and we can pass PrintStream or PrintWriter as argument to write the stack trace information to the file or stream.

Category: Java
  • Checked Exceptions should be handled in the code using try-catch block or else main() method should use throws keyword to let JRE know about these exception that might be thrown from the program. Unchecked Exceptions are not required to be handled in the program or to mention them in throws clause.
  • Exception is the super class of all checked exceptions whereas RuntimeException is the super class of all unchecked exceptions.
  • Checked exceptions are error scenarios that are not caused by program, for example FileNotFoundException in reading a file that is not present, whereas Unchecked exceptions are mostly caused by poor programming, for example NullPointerException when invoking a method on an object reference without making sure that it’s not null.
Category: Java

throws keyword is used with method signature to declare the exceptions that the method might throw whereas throw keyword is used to disrupt the flow of program and handing over the exception object to runtime to handle it.

Category: Java

We can extend Exception class or any of its subclasses to create our custom exception class. The custom exception class can have its own variables and methods that we can use to pass error codes or other exception related information to the exception handler.

package com.journaldev.exceptions;
import java.io.IOException;
public class MyException extends IOException {
 private static final long serialVersionUID = 4664456874499611218L;
 
 private String errorCode="Unknown_Exception";
 
 public MyException(String message, String errorCode){
 super(message);
 this.errorCode=errorCode;
 }
 
 public String getErrorCode(){
 return this.errorCode;
 }
}
Category: Java

OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and it’s thrown by JVM when it ran out of heap memory. We can fix this error by providing more memory to run the java application through java options.

$>java MyProgram -Xms1024m -Xmx1024m -XX:PermSize=64M -XX:MaxPermSize=256m
Category: Java
  • Exception in thread main java.lang.UnsupportedClassVersionError: This exception comes when your java class is compiled from another JDK version and you are trying to run it from another java version.
  • Exception in thread main java.lang.NoClassDefFoundError: There are two variants of this exception. The first one is where you provide the class full name with .class extension. The second scenario is when Class is not found.
  • Exception in thread main java.lang.NoSuchMethodError: main: This exception comes when you are trying to run a class that doesn’t have main method.
  • Exception in thread “main” java.lang.ArithmeticException: Whenever any exception is thrown from main method, it prints the exception is console. The first part explains that exception is thrown from main method, second part prints the exception class name and then after a colon, it prints the exception message.
Category: Java
  • final and finally are keywords in java whereas finalize is a method.
  • final keyword can be used with class variables so that they can’t be reassigned, with class to avoid extending by classes and with methods to avoid overriding by subclasses, finally keyword is used with try-catch block to provide statements that will always gets executed even if some exception arises, usually finally is used to close resources. finalize() method is executed by Garbage Collector before the object is destroyed, it’s great way to make sure all the global resources are closed.
  • Out of the three, only finally is related to java exception handling.
Category: Java

When exception is thrown by main() method, Java Runtime terminates the program and print the exception message and stack trace in system console.

Category: Java

We can have an empty catch block but it’s the example of worst programming. We should never have empty catch block because if the exception is caught by that block, we will have no information about the exception and it wil be a nightmare to debug it. There should be at least a logging statement to log the exception details in console or log files.