Java Articles

Page 126 of 450

How do we extract all the words start with a vowel and length equal to n in java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 28-Aug-2025 2K+ Views

In this article, we will learn to extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java. We can solve this problem using the following ways: Using the split() method of the String class. Using the StringTokenizer class. Using Regular Expressions. Using the split() method of the String class To extract all the words that start with a vowel and have a length equal to n from a given text file or string in Java, we can use the following steps: The ...

Read More

Java program to convert a list of string to comma separated string

Aishwarya Naglot
Aishwarya Naglot
Updated on 26-Aug-2025 1K+ Views

A list is a collection in Java which is used for storing group of elements. It is an ordered collection which also allows to store duplicate elements. A String is a sequence of characters which is used for representing text in Java. Here, in this article, we are given a list of strings, we have to convert it into a comma-separated string in Java using different approaches. To understand this problem, consider the following input-output scenarios: Scenario 1 Input: ["Apple", "Banana", "Mango", "Orange"] Output: Apple, Banana, Mango, Orange Scenario 2 Input: ["Red", "Green", "Blue"] Output: ...

Read More

What is the order of execution of non-static blocks with respect to a constructor in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 26-Aug-2025 3K+ Views

In Java, non-static blocks run before the constructor whenever an object is created. If multiple non-static blocks exist, they execute in the order they appear in the class, followed by the constructor. In this article, we will understand this order of execution with the help of examples. What Are Non-Static Blocks in Java? The Non-static blocks are class-level blocks which don't contain a prototype. A non-static block must perform any logic whenever an object is instantiated, regardless of the constructor. The Non-static blocks are automatically called by the JVM for every object creation in the Java stack area. We can ...

Read More

Can we define constructor inside an interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 26-Aug-2025 8K+ Views

Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Can we define a constructor inside an interface in Java? No, constructors cannot be defined inside an interface in Java. Constructors are special methods that are used to initialize objects of a class, and since interfaces cannot be instantiated, they do not have constructors. Why can constructors not be defined in an interface? There are several reasons why constructors cannot be defined ...

Read More

Java program to print downward triangle star pattern

Manisha Chand
Manisha Chand
Updated on 22-Aug-2025 1K+ Views

In this article, we will understand how to print a downward triangle star pattern using Java. In this pattern, the face of the triangle will be in a downward direction, and the base will be in an upward direction. We will learn through two examples: one where the user inputs the number of rows, and another where the number of rows is predefined in the program. Problem Statement Write a Java program to print a downward triangle star pattern. Below is a demonstration of the same. Input: Enter the number of rows : 8 Output: The downward triangle star pattern ...

Read More

Java Program to Interchange the Diagonals

Manisha Chand
Manisha Chand
Updated on 22-Aug-2025 443 Views

In this article, we will understand how to interchange the diagonal elements of a given matrix in Java. A matrix is a two-dimensional array made up of rows and columns. A matrix with m rows and n columns is called an m × n matrix. Each element in the matrix is represented by a[i][j], which means that the particular element a is present in the i-th row and j-th column. Problem Statement Write a Java program to interchange the elements of the primary and secondary diagonals of a square matrix. Example Scenario ...

Read More

Constructor overloading in enumerations in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 1K+ Views

Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it. Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it. Constructor Overloading in Java Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in ...

Read More

What is the return type of a Constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 7K+ Views

A Constructor in Java is similar to a method, and it is invoked at the time of creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. The main purpose of a constructor is to initialize the instance variables of a class. Return Type of Constructor In Java, constructors do not have a return type, not even void. They are used to initialize the object when it is created. A constructor doesn’t have any return type. ...

Read More

How to implement a constructor reference with one or more arguments in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 20-Aug-2025 10K+ Views

In this article, we are going to learn about implementing a constructor reference with an argument or multiple arguments in Java. Constructor references in Java provide us with a concise way of creating new objects via method references. These are going to enable us to reference a constructor without running the constructor itself so the code looks cleaner and it will be more readable. What is a Constructor Reference? A method reference may also be used with Java 8 constructors. A constructor reference may be defined by employing the class name and a new keyword. The constructor reference may be ...

Read More

How to create a constructor reference for an array in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 20-Aug-2025 4K+ Views

In Java, a constructor refers to a special method within a class that is always executed automatically while creating an instance of the class using the new keyword. Its main purpose is initializing the object by providing its fields with initial values so that it is ready for use. A constructor shares the name of the class itself, has no return type, and can be overloaded so that there will be different ways of initializing the objects. If you don’t provide one, Java inserts a default no-argument constructor automatically. What is a Constructor Reference in Java? Constructor reference is like ...

Read More
Showing 1251–1260 of 4,498 articles
« Prev 1 124 125 126 127 128 450 Next »
Advertisements