Object Oriented Programming Articles

Page 183 of 588

How to Create Your Own Annotations in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 09-Apr-2025 1K+ Views

When we start learning Java, we often wonder about symbols like @override and @inherited. They are a special kind of tag termed as Annotations that can be applied to classes, methods, fields, parameters, and other elements of the code. Java provides support for some built-in annotations, however, we are allowed to create our own annotations too. In this article, we are going learn how to create and use our own custom annotations. Before creating our own annotations, let's familiarize ourselves with the basics of annotations in Java. What are Annotations? Annotations are features of Java that allows us to add ...

Read More

Can we define a parameterized constructor in an abstract class in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 09-Apr-2025 5K+ Views

A common question in Java OOPs is whether abstract classes can have parameterized constructors. Yes, we can define a parameterized constructor in an abstract class. What is a Parameterized Constructor in Java A parameterized constructor is a special type of class constructor that accepts parameters/arguments when creating an object. Unlike a default (no-arg) constructor, it allows you to initialize an object with specific values at the time of creation. Syntax The Following is the syntax: public class ClassName { private dataType field1; private dataType field2; // Parameterized ...

Read More

Is there a case when finally block does not execute in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 09-Apr-2025 2K+ Views

Questions related to Java exception handling are very frequent in interviews for many companies and even in exams. One such question that an interviewer might ask is whether there is a case when the finally block does not execute in Java. We will try to find the answer to this question in the simplest way possible. In Java, the finally block is designed to execute regardless of whether an exception is thrown or handled in the try-catch blocks. However, finally block may not execute if System.exit() is called either inside try or catch block. Before moving to the question, ...

Read More

Arrays vs Set in JavaScript.

Alshifa Hasnain
Alshifa Hasnain
Updated on 29-Mar-2025 3K+ Views

In this article, we will learn about the difference between an array and a set in JavaScript. Arrays are used to store ordered collections of elements, whereas in Sets, we can store only unique values. What is an Array?  An Array is an ordered, indexed collection of values in JavaScript. It allows duplicate values and provides various built-in methods to manipulate elements. Syntax let numbers= [1, 2, 3, 4, 5]; What is a Set? A Set is an unordered collection of unique values in JavaScript. Unlike arrays, a Set does not allow duplicate values. Syntax let numbers= new Set([1, 2, ...

Read More

Convert HashSet to TreeSet in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 29-Mar-2025 1K+ Views

In this article, we will learn to convert a HashSet to a TreeSet in Java. Converting from one type of collection to another is a simple process when we require them to change their behavior or the nature of our data structure. Why Convert HashSet to TreeSet? For the following operations, we can convert a HashSet to a TreeSet − Sorted Order: TreeSet maintains elements in ascending order. Range-Based Operations: It supports operations like headSet(), tailSet(), and subSet(). NavigableSet Features: TreeSet provides methods like higher(), lower(), ceiling(), ...

Read More

Difference between List and Set in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 27-Mar-2025 4K+ Views

In Java, List and Set are both interfaces that belong to the Collection framework. Both interfaces extend the Collection interface. They are both used to store a collection of objects as a single unit. Before JDK 1.2, we used to use Arrays, Vectors, and Hashtable for grouping objects as a single unit. Difference Table The following are the key differences between List and Set − Sr. No. ...

Read More

What is the character wrapper class and its methods in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 26-Mar-2025 7K+ Views

In this article, we will learn about the character wrapper class and its methods in Java. The Character class is the primitive char type wrapper, providing useful methods for manipulation, classification, and conversion of characters. The Character Wrapper Class The Character class of the java.lang package wraps a value of the primitive datatype char. It offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor. Syntax Character ch = new Character('a'); Various Methods in the Character Class The following are some of the methods used in the ...

Read More

What is the purpose of private constructor in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 26-Mar-2025 1K+ Views

In this article, we will learn about the purpose of private constructor in Java. Constructor is a unique method used to initialize objects. Constructors are public or protected by default, which allows outside classes to create instances Why Use a Private Constructor? Private constructor is mainly applied to manage object creation. It does not allow other classes to instantiate the class, creating particular design patterns or restrictions. Purpose of a Private Constructor The private constructor is useful in case we want to restrict the object creation. For example − Singleton pattern can be implemented using ...

Read More

How to declare a static String array in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 20-Mar-2025 2K+ Views

In this article, we will learn the declaration of static Strings array in Java. Arrays can be used to store multiple values in one variable. Static array has a specific size which cannot be increased in the later part of the program after creation. What is a Static String Array? A static array is a declared array as static, which means that it is associated with the class, not with a class instance. This brings the array into a shared state among all instances of the class. Static variables are loaded when the class is loaded, even before class instances ...

Read More

Initialize an ArrayList in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 18-Mar-2025 2K+ Views

In this article, we will learn to initialize an ArrayList in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Different Approaches The following are the two different approaches to initialize an ArrayList in Java − Using add() Method Using asList() method Using add() Method One of the most frequent methods of ...

Read More
Showing 1821–1830 of 5,877 articles
« Prev 1 181 182 183 184 185 588 Next »
Advertisements