Found 7442 Articles for Java

Can we override only one method while implementing Java interface?

Maruthi Krishna
Updated on 22-Apr-2025 11:31:34

1K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or make a contract specifying how the methods and fields of a type should be, you can define an interface. When a class implements an interface, it should provide concrete implementations for all of its abstract methods. But is it possible to override only one method from an interface? Overriding a Method from an Interface In Java, all methods in an interface are abstract (unless declared as default or static). When a class implements an interface, it must override those abstract methods. No, ... Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:29:01

90K+ Views

Sorting a string alphabetically means rearranging its characters in order, from A to Z. For example, the string "java" becomes "aajv" after sorting. Different ways to sort a String Alphabetically There are two ways to do this in Java using different approaches - Using toCharArray() and Arrays.sort() Method Sorting the array manually Using toCharArray() and Arrays.sort() Method The toCharArray() method of this ... Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:25:46

3K+ Views

In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it? Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways- Using For-Loop Using For-Each Loop ... Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:23:59

357 Views

When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements. Need for Converting an Array to an ArrayList Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when - Need to use dynamic Collections. ... Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:22:42

2K+ Views

When you are working with strings in Java, you may want to keep only the alphabetic characters (A-Z, a-z) and remove everything else, like numbers, punctuation, and symbols. Java gives you multiple ways to do this for different cases or scenarios. Removing non-alphabetical characters from a string There are different methods, and they are - Using split() method Using replaceAll() method ... Read More

How to remove an element from ArrayList or, LinkedList in Java?

Maruthi Krishna
Updated on 16-Jun-2025 19:03:21

848 Views

The ArrayList and LinkedList classes implement the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements, as shown below - Using remove(int index) Using remove(Object o) There are two additional removal approaches provided by the Collection and Iterator interfaces. They are - ... Read More

How to make an ArrayList read only in Java?

Maruthi Krishna
Updated on 16-Apr-2025 11:20:17

906 Views

A collection is an object that holds a reference to other objects. A Java array list is one of the collections. it is a resizable array and is represented by the class named ArrayList. This java.util.Collections class provides methods to manipulate the collection objects in Java. We can make an ArrayList read-only using the Collections.unmodifiableList() method. Need to make a ArrayList Read-Only Sometimes we may need to make the ArrayList object read-only - To protect data from unnecessary or unwanted changes. ... Read More

How to send data over remote method in java?

Maruthi Krishna
Updated on 16-Apr-2025 17:02:51

724 Views

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing/running in one system (JVM) to access/invoke an object running on another JVM. RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi. To create an RMI Java application, you need to follow the steps given below - Step 1: Define the Remote Interface A remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface. Therefore, you need to create an interface that extends ... Read More

What are the key steps in read/write from/to a URL connection in java?

Maruthi Krishna
Updated on 16-Apr-2025 16:29:17

407 Views

The URL class in the java.net package represents a web address or URL (Uniform Resource Locator), which points to a web resource such as a file, page, or directory in the World Wide Web (internet). The URL class provides various constructors, one of which accepts a String parameter (representing a URL) and constructs an object of the URL class. Reading From a URL You can read content from a URL using the openStream() method. This method opens a connection to the web address specified by the current URL object and gives you an InputStream object. The Java InputStream is an ... Read More

How to handle EOFException in java?

Maruthi Krishna
Updated on 18-Apr-2025 18:14:21

1K+ Views

While reading the contents of a file using a Java program, we may reach the end of the file abruptly; in such cases, EOFException (End Of File) is thrown. Especially, this exception occurs while reading data using the InputStream objects, such as DataInputStream. In other scenarios, a specific value will be thrown when the end of the file is reached. Why Does the EOFException Occur The EOFException occurs when a stream reaches the end of the file while reading its contents. If we consider the DataInputStream class, it provides various methods such as readBoolean(), readByte(), readChar(), etc, to read primitive ... Read More

Advertisements