Solved Error: cannot be cast to java.lang.Comparable

This article will look at how to solve the above-given Class Cast Exception. This ClassCast Exception occurs at runtime when we improperly typecast an object of a class to a related class of which it is not an instance. Now, what does it mean when the exception says: cannot be cast to java.lang.Comparable. Let us first have a brief look at what is Comparable. We use Comparable Interface mainly when we need to sort an Array of Custom Objects. In

Read more

Java String Interpolation with Examples

In this article, we will look at the concept of String Interpolation. We will look at its description, the need for String Interpolation. Also, we look at different techniques to perform String Interpolation with implementation in Java. String Interpolation is a process where we evaluate a String containing one or more Placeholders (Temporary Name or Substitute) by replacing it with their corresponding values. It allows to dynamically print out text output. So, in short String Interpolation allows us to put

Read more

Evaluate Postfix Expression in Java

In this article, we will look at how to evaluate a given Postfix expression to a result value. This is a problem related to Stack Data Structure. We will look at the algorithm with a step-by-step example along with the implementation in code. Let us first have a quick look at Postfix Expressions. So, Postfix Expressions are the mathematical expressions where for each pair of Operator and Operands, the operators come after operands. Hence, the evaluation order is from left

Read more

Java Reflection Invoke Static Method

This article will look at how we can use Java Reflection API to invoke a Static method. We will see the steps and the explanation with code. Let us first have a quick look at Reflection API. Reflection in Java is a process of examining or modifying the behavior of a class at run time. Moreover, It is an API or Application Programming Interface having its use in debugging. It is also used in testing tools, with the primary focus

Read more

Volatile vs Synchronized in Java

In this article, we will discuss volatile vs synchronized in Java. First, we will define what is synchronized. Synchronized Keyword Synchronized is the keyword which is applied on blocks and methods but not on the classes and variables. When multiple threads are trying to access the same Java object so this situation leads to a data inconsistency problem. So in this situation, we should go for a synchronized keyword. So we use synchronized keywords along with the name of the

Read more

One Dimensional Array in Java – 1D Array

Here, we will discuss what is a one-dimensional array and how to use it in Java. A one-dimensional array or 1D array is the list of variables of the same data type stored in the contiguous memory locations. We can access these variables of a 1-d array by an index value in square brackets followed by name of the array. So to declare the array we need the datatype of the values which we want to store, array name, and

Read more

Java Program for Employee Details using Class and Object

In this article, we will talk about the Java program for employee details using class and object. An Employee is a person or also be referred to as an entity that consists of various attributes such as – emp_id, emp_name, emp_salary, emp_department, emp_email, emp_address, and many more. We use the getter (to receive Employee details) and setter (to set Employee details) method in this program. In the main class, we will create an object of the EmployeeDetails class and with

Read more

Covariant Return Type in Java

In this article, we will learn about covariant return types in Java. Before Java version 5.0, we could not change the return type of an overridden method but a new method was introduced in JDK 5.0, called covariant where we could change the return type of the overridden method. The return type must be non-primitive. (i.e., it should be a sub-type of the overridden class). Program:


Explanation: In the above program, we created three classes: class ‘Employee’,

Read more
1 2 3 4 5 27