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:

Output:

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

Read more

Static Import in Java with Example

In this article, we will discuss static import in Java. Just as import statements are used to access a class without its package qualification. In a similar manner, static imports are used to access static members without their class qualification. Some basic static methods used with the class names are as follows: Math.sqrt(4): It is used to find the square root of the given number. Math.max(4, 2): It is used to find the maximum of the given numbers. Math.min(4, 2):

Read more

Advantages and Disadvantages of Generics in Java

Generics in Java program was introduced in Java version 5. It is used to ensure the data type safety of objects. So, it constructs the code steady by detecting bugs at compile time. Why do we need generics in Java? When we write programs, errors occur. Some errors occur at run time while some errors occur at compile time. Those errors which occur at compile-time can be easily detected and rectified in the program. But, run time errors occur when

Read more

Nested Switch in Java

In this article, we will discuss about nested switch in Java with examples. When a switch statement contains further switch statements inside it, then it is called a nested switch. Inside the nested switch statement, we can give multiple cases and a default case. The inner switch statement must be part of any outer case statement. We cannot use the inner switch statements outside the cases.

Read more

TreeSet Vs TreeMap in Java

TreeSet is an implementation class of NavigableSet. In a TreeSet, duplicate as well as heterogenous (values of different data types) values are not allowed. If you’re trying to insert heterogeneous objects then it displays a ClassCastException. If the tree set exists, then we can insert a null value, and if the treeset doesn’t exist then we cannot insert a null value in it. All versions of Java below 6.0 allows insertion of null value, but all other versions show Null

Read more
1 2