Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 3 of 50

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

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 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

Why can\\\\\\\'t a Java class be both abstract and final?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Apr-2025 4K+ Views

In Java, both abstract and final are class modifiers but they are completely opposite to each other. That's why Java class cannot be both abstract and final. Abstract class An abstract class in Java is a class that may contain both abstract methods (without implementation) and concrete methods (with implementation). If a class has even one abstract method, it must be declared abstract. You cannot create objects of an abstract class directly. If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and ...

Read More

Can Enum extend any class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Apr-2025 2K+ Views

If you’ve been coding in Java for some time, you might have wondered why you can't make an enum extend another class. It seems like it should, but Java won't let you. Let's break down why this happens Enumeration in Java Enumeration (enum) in Java is a user-defined datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year, and seasons, etc. If we had to store all seven days of a week into one single variable then we might tend to use data ...

Read More

How to read a .txt file with RandomAccessFile in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Apr-2025 2K+ Views

In general, while reading or writing data to a file, you can do so, from the start of the file. You cannot read/write from random position. The java.io.RandomAccessFile class in Java enables you to read/write data to a random-access file. This acts similar to a large array of bytes where the cursor known as file pointer works like an index. With the help of file pointer you can get the position of this pointer using the getFilePointer() method and set it using the seek() method. RandomAccessFile Class: Reading Files This class provides various methods to read and write data ...

Read More

What are the restrictions imposed on a static method or a static block of code in java?

Maruthi Krishna
Maruthi Krishna
Updated on 18-Apr-2025 5K+ Views

Static Methods and Static Blocks Static methods belong to the class and they will be loaded into memory along with the class; you can invoke them without creating an object. (using the class name as reference). Whereas a static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading. Example The following example demonstrates the usage of static blocks and static methods in Java - public class Sample { ...

Read More

How many types of memory areas are allocated by JVM in java?

Maruthi Krishna
Maruthi Krishna
Updated on 18-Apr-2025 2K+ Views

Java Virtual Machine is a program/software that runs Java applications. It takes Java bytecode (.class files) and converts the bytecode (line by line) into machine-understandable code, line by line so the processor can understand and execute it. JVM contains a module(components) known as a class loader. It is responsible for loading the program into memory and preparing it to run. Class Loader performs three main tasks - It Loads the class into the memory. ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 16-Apr-2025 556 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

Pattern UNIX_LINES field in Java with Examples

Maruthi Krishna
Maruthi Krishna
Updated on 08-Aug-2024 347 Views

This flag enables Unix lines mode. In the Unix lines mode, only '' is used as a line terminator and ‘\r’ is treated as a literal character. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept ...

Read More

Java program to verify whether a given element exists in an array

Maruthi Krishna
Maruthi Krishna
Updated on 31-Jul-2024 1K+ Views

Given an array and one of its element as an input, write a Java program to check whether that element exists in given array or not. You can find any element from an array using search algorithms. In this article, we will use linear search and binary search algorithms. Using Linear Search Algorithm In this approach, follow the steps below to verify whether a given element exists in an array − Iterate through the array using for loop. Compare each element with the required element. If found return the index. Example The following Java program shows how ...

Read More

Java program to delete duplicate lines in text file

Maruthi Krishna
Maruthi Krishna
Updated on 08-Jul-2024 3K+ Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true, if you try to add an existing element using this method, the addition operations fails returning false. Problem Statement Given a file which contains duplicate lines, write a program in Java to read the file, remove duplicate lines, and write the unique lines to a new file. Input Hello how are you Hello how are you welcome to Tutorialspoint Output Hello how are you welcome to Tutorialspoint ...

Read More
Showing 21–30 of 500 articles
« Prev 1 2 3 4 5 50 Next »
Advertisements