Java 8 introduced a new class called Optional, which is a container object that may or may not contain a non-null value. The main purpose of the Optional class is to provide a way to handle null values in a more elegant and readable way than using traditional null checks. In this article, we will take…
Category: Interview questions
Top 10 Exception Handling interview questions in java
Exception handling is an important topic in Java and is often covered in job interviews for Java developers. Here are the top 10 exception handling interview questions, along with example answers: 1.What is an exception in Java? An exception is an event that occurs during the execution of a program that disrupts the normal flow of…
What is ‘final’ variable in Java? Explained with example.
In Java, a final variable is a variable whose value cannot be changed after it is initialized. Final variables are declared using the “final” keyword and once a value is assigned to a final variable, it cannot be reassigned. There are several benefits to using final variables. For example, final variables can be used to ensure…
What is ‘this’ keyword in java? Explained with example.
The “this” keyword in Java is used to refer to the current object of a class. It is used to access the current object’s properties and methods, and it can also be used to invoke constructors of the same class. For example, consider the following class: class MyClass { int x; MyClass(int x) { this.x =…
What is ‘super’ keyword in java? Explained with example.
The “super” keyword in Java is used to refer to the immediate parent class of an object. It is typically used within the context of inheritance, where a subclass inherits properties and methods from its parent class. The “super” keyword can be used in several ways in Java, but its most common usage is to invoke…
Fail Fast Vs Fail Safe Iterator In Java
Fail fast iterator while iterating through the collection , instantly throws Concurrent Modification Exception if there is structural modification of the collection. Fail Safe Iterator makes copy of the internal data structure (object array) and iterates over the copied data structure.Any structural modification done to the iterator affects the copied data structure. Fail Fast Iterator Fail Safe Iterator…
String vs StringBuilder vs StringBuffer in Java
String String is immutable ( once created can not be changed )object . The object created as a String is stored in the Constant String Pool. Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously. String once assigned can not be changed. StringBuffer…
Iterable interface in Java
The Iterable Interface is defined in java.lang package. The Iterable interface is the super interface in collection framework. It is the root interface for all the collection classes. The Collection interface extends the Iterable interface. So,all the subclasses that implementing the Collection interface also implement the Iterable interface. Implementing this interface allows an object to be the target of the enhanced “for each” statement. The for-each loop is used…
Iterator vs ListIterator in Java
Iterator ListIterator Can traverse all the collections including set, map, etc. It can be used to traverse only list type collection like ArrayList, LinkedList. Iterates the collection only in the forward direction. Can iterate over the collection in forward as well as backward direction. Cannot obtain indexes. Can obtain indexes. No way to add new elements…
Top 5 frequently asked questions on Iterator Interface in Java
Q #1) What is the Iteration in Java? Answer: An iteration is a process by which a code block is repeatedly executed until a given condition holds or doesn’t exist. Using iteration you can traverse through a sequence of elements or process the data. Q #2) How many types of Iterators are there in Java? Answer: Iterators are…