Java Articles

Page 165 of 450

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

Can we create a program without a main method in Java?

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

Java is a statically typed, object-oriented programming language that needs a main method as an entry point for running. But it is possible to develop Java programs without a main method under certain conditions. Different Approaches The following are the different approaches for creating a program without a main method in Java − Using Static Blocks Using a Servlet Using a JavaFX Application Using Static Blocks (Before Java 7) In previous implementations of Java (prior to Java 7), a program was possible to run with a ...

Read More

Can we create a program without a main method in Java?

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

Java is a statically typed, object-oriented programming language that needs a main method as an entry point for running. But it is possible to develop Java programs without a main method under certain conditions. Different Approaches The following are the different approaches for creating a program without a main method in Java − Using Static Blocks Using a Servlet Using a JavaFX Application Using Static Blocks (Before Java 7) In previous implementations of Java (prior to Java 7), a program was possible to run with a ...

Read More

Get the POST request body from HttpServletRequest

Swati Jadhav
Swati Jadhav
Updated on 17-Mar-2025 524 Views

When handling HTTP POST requests in Java using Servlets, you may need to extract the request body from the HttpServletRequest object. This is useful when dealing with raw JSON data, form submissions, or XML payloads. Extracting Request Body from HttpServletRequest The HttpServletRequest object provides an InputStream from which you can read the request body. You can use getReader() or getInputStream(), depending on your needs. Using BufferedReader (getReader) The getReader() method returns a BufferedReader that allows you to read the request body as text. import java.io.BufferedReader; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/postHandler") public ...

Read More

3 ways to initialize an object in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 17-Mar-2025 10K+ Views

In Java, objects may be initialized in various ways for different purposes. When a class is implementing the Cloneable interface, it opens up one more way of creating object instances. What is Cloneable Interface? Cloneable interface in Java states that a class can clone its objects. It enables the class to make a copy of itself using the clone() method. If the class is not Cloneable, the clone() method called on its object would throw an error. Different Approaches Consider a class Tester that has implemented a Cloneable interface. Now you can initialize an object using the following three ways ...

Read More

3 ways to initialize an object in Java

Alshifa Hasnain
Alshifa Hasnain
Updated on 17-Mar-2025 10K+ Views

In Java, objects may be initialized in various ways for different purposes. When a class is implementing the Cloneable interface, it opens up one more way of creating object instances. What is Cloneable Interface? Cloneable interface in Java states that a class can clone its objects. It enables the class to make a copy of itself using the clone() method. If the class is not Cloneable, the clone() method called on its object would throw an error. Different Approaches Consider a class Tester that has implemented a Cloneable interface. Now you can initialize an object using the following three ways ...

Read More

Get the location of an element in Java ArrayList

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

In this article, we will learn to get the location of an element in Java. In Java, an ArrayList is used for storing and manipulating dynamic collections of elements. When working with an ArrayList, you might need to find the index (position) of a specific element. What is the indexOf()? The location of an element in an ArrayList can be obtained using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is unavailable in the ArrayList, then this method returns -1. Syntax int index = arrayList.indexOf(element); Finding the ...

Read More

Java Program for Cocktail Sort

Alshifa Hasnain
Alshifa Hasnain
Updated on 10-Mar-2025 480 Views

In this article, we will learn to perform cocktail sorting in Java. Cocktail Sort, also known as Bidirectional Bubble Sort, is a variation of the standard Bubble Sort algorithm. What is Cocktail Sort? Cocktail Sort works in contrast to bubble sort, wherein elements are iterated from left to right, and the largest element is first brought to its correct position, and so on. In cocktail sort, elements are iterated over in both directions (left and right) in an alternating fashion. Cocktail Sort Working Following are the steps to perform cocktail sort in Java − Traverse ...

Read More

How to create an array of Object in Java

Johar Ali
Johar Ali
Updated on 07-Mar-2025 1K+ Views

In this article, we will learn to create an array of objects in Java. An array of Object classes can be created that can accept any type of object. During the operation on such an array, instanceof operator can be used. Different Approaches The following are the two different approaches to creating an array of objects in Java − Using an Object[] Array Using a Class-Specific Object Array Using an Object[] Array Java provides a built-in Object class, which is the superclass of all classes. This means an array ...

Read More

Java DatabaseMetaData getURL() method with example

Alshifa Hasnain
Alshifa Hasnain
Updated on 06-Mar-2025 583 Views

In this article, we will learn about the DatabaseMetaData getURL() method in Java. The DatabaseMetaData interface provides useful methods to obtain details about the database and the JDBC driver. One such method is getURL(), which returns the URL of the JDBC driver being used. What is the getURL() Method? The getURL() method in Java's DatabaseMetaData interface is used to retrieve the URL of the database to which the current connection is established. Syntax String dbUrl = metaData.getURL(); This method retrieves the URL of the underlying Database Management System and returns it in the form of a String variable. ...

Read More
Showing 1641–1650 of 4,498 articles
« Prev 1 163 164 165 166 167 450 Next »
Advertisements