Java constructor is a special method used to initialize objects. It has the same name as the class and is automatically called when an object is created. You can also declare a constructor as private to restrict object creation from outside the class. What is a Private Constructor? A private constructor is a constructor that is declared using the private access modifier. It restricts object creation from outside the class i.e., the class having a private constructor cannot be subclassed. Private constructors are used in design patterns like Singleton, or to prevent subclassing or instantiation. Purpose of a Private ... Read More
List is a collection in Java that allows us to store elements. In this article, we will learn how to get the index of an element in a List in Java. We have various ways to get the index of an element in a List in Java. They are - Using the indexOf() method Using a for loop Using Stream API Using the indexOf() method The indexOf() method returns the index of the first occurrence of the specified element in this list, or -1 if this ... Read More
A method is a collection of statements that are grouped together to perform an operation. In Java, you can define a method having the same name as the class but it is not recommended as per the coding standard. Can We define a Method Having Same as Class? Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. But this is not recommended as per coding standards in Java. Normally the constructor name and class name always the same in Java. Example of a ... Read More
List Iterators in Java are mainly useful for traversing a list in both directions (forward and backward). They allow you to iterate through the elements of a list while providing methods to modify the list during iteration. In this article, we will learn how to create or make a list iterator in Java. Ways to Create a List Iterator in Java Using ListIterator Interface Using the Iterator Interface Using Stream API Using ListIterator Interface We can utilize listIterator() method of the List interface, which allows element ... Read More
A list is an ordered collection that holds a sequence of elements. In Java, a list is represented by the List Interface of the java.util package. This provides various methods to maintain and manipulate the list.To create a list, you need to instantiate any of the implementing classes of this interface, suchas, ArrayList, LinkedList, Stack, Vector, etc. We can create a List of elements in multiple ways - Without specifying the type Using ArrayList Let's explore these methods in detail. Without specifying the "type" We can create a List without specifying ... Read More
In this article, we will learn to reverse a string using recursion in Java. Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is known as a recursive call of the function. You can reverse a string using the recursive function as shown in the following program. Steps to reverse a string using recursion Following are the steps to reverse a string using recursion - Create a class called StringReverse with a method reverseString that takes ... Read More
Stack is a linear data structure where we can store elements. It uses the LIFO (last in, first out) principle, which means the item we add last will be the first one to be removed. In this article, we will understand how to reverse a string using stacks. Let's take an example: Input: String input_string = "Java Program"; Output: String reversed_string = "margorP avaJ"; Ways to Reverse a String Using Stacks Below are the different approaches to reverse a string using stacks: Reverse a string using built-in stack methods ... Read More
In this article, we will understand how to implement multiple inheritance. Unlike other programming languages, such as C++, Java does not support multiple inheritance through classes. This means that a class cannot inherit from more than one class. But why? Let's understand first what multiple inheritance is and why it is so. Multiple Inheritance Multiple Inheritance is one of the features of object-oriented programming, where a class (child class) can inherit properties of more than one class (parent class) using the extends keyword. But Java does not support this to avoid complexity and ambiguity. It can ... Read More
In this article, we will understand how to write a Java program to calculate simple interest. But, before doing it, let's understand how we calculate simple interest mathematically. The simple interest is a way to determine the amount of interest gained on a principal amount at the specified interest rate for a given time. Unlike compound interest, its principal amount does not change over time. To calculate Simple Interest, we use the following formula− Simple Interest (S.I) = Principal * Time * Rate / 100 where, P is the principal amount T is the time R is the rate ... Read More
Jackson Library: The @JsonView AnnotationIn Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether a variable is a JsonProperty, should be ignored, or what condition should be applied to it. The JsonView annotation is used to include/exclude ... Read More