Object Oriented Programming Articles

Page 227 of 589

Why do we use import statement in Java? Where it should be included in the class?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 5K+ Views

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.A program that demonstrates this in Java is given as follows:Exampleimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Apple");       l.add("Mango");       l.add("Cherry");       l.add("Orange");       l.add("Pear");       System.out.println("The LinkedList ...

Read More

Use a quantifier to find a match in Java

Arushi
Arushi
Updated on 11-Mar-2026 155 Views

One of the quantifiers is the plus(+). This matches one or more of the subsequence specified with the sequence.A program that demonstrates using the quantifier plus(+) to find a match in Java is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("o+");       Matcher m = p.matcher("o oo ooo");       System.out.println("The input string is: o oo ooo");       System.out.println("The Regex is: o+ ");       System.out.println();       while (m.find()) {          System.out.println("Match: " ...

Read More

Make a class final in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 5K+ Views

A class can be made final by using the final keyword. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.A program that demonstrates a final class in Java is given as follows:Examplefinal class A {    private int a = 15;    public void printA() {       System.out.println("Value of a = " + a);    } } public class Demo {    public static void main(String args[]) {       A obj = new A();       obj.printA();    } }OutputValue of a = 15Now ...

Read More

Why Abstract Class is used in Java?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.A program that demonstrates an abstract class in Java is given as follows:Exampleabstract class Animal {    abstract void sound(); } class Cat extends Animal {    void sound() {       System.out.println("Cat Meows");    } } class Dog extends Animal {    void sound() {       System.out.println("Dog Barks");    } } class Cow extends Animal { ...

Read More

How to import all classes in Java?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 13K+ Views

All the classes in a package can be imported using the import statement along with the character *. For example - All the classes in the package java.util can be imported using import java.util.*;A program that demonstrates this in Java is given as follows:Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       Stack stack = new Stack();       stack.push("Apple");       stack.push("Mango");       stack.push("Guava");       stack.push("Pear");       stack.push("Orange");       System.out.println("The stack elements are: " + stack);    } }OutputThe stack elements are: ...

Read More

Use static Import for sqrt() and pow() methods in class Math in Java

Fendadis John
Fendadis John
Updated on 11-Mar-2026 1K+ Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class methods sqrt() and pow() in the package java.lang are static imported. A program that demonstrates this is given as follows:Exampleimport static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo {    public static void main(String args[]) {       double num = 4.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));       System.out.println("The ...

Read More

Initializer for final static field in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

The final static field variable is a constant variable. There is only one copy of this variable available. It is mandatory to initialize the final static field variable explicitly as the default value for it is not provided by the JVM. Also, this variable cannot be reinitialized.A program that initializes the final static field variable using a static initialization block is given as follows:Examplepublic class Demo {    final static int num;    static {       System.out.println("Running static initialization block.");       num = 6;    }    public static void main(String[] args) {       ...

Read More

Finding all words that start with an 'a' in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 729 Views

All the words that start with a can be found in a string by using regular expressions in Java. The regular expressions are character sequences that can be used to match other strings using a specific pattern syntax. The regular expressions are available in the java.util.regex package which has many classes but the most important are Pattern class and Matcher class.A program that finds all words that start with an 'a' is using regular expressions is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) throws Exception {       String str ...

Read More

A Reluctant qualifier in Java Regular Expressions

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 237 Views

The reluctant qualifier starts with the shortest string size as possible. If a match is found by the engine, the process continues to find more matches otherwise the engine adds a character to the searched string section and tries again. This continues until a match is obtained or the string is used up.The regex "B+?" is used to find the match in the string "SkyIsBlue".A program that demonstrates this is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String regex = "B+?";       String str = ...

Read More

Use a character class in Java Regular Expressions

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 234 Views

A character class is set of characters that are enclosed inside square brackets. The characters in a character class specify the characters that can match with a character in an input string for success. An example of a character class is [a-z] which denotes the alphabets a to z.A program that demonstrates a character class in Java Regular Expressions is given as follows:Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("[a-z]+");       Matcher m = p.matcher("the sky is blue");       System.out.println("The input string ...

Read More
Showing 2261–2270 of 5,881 articles
« Prev 1 225 226 227 228 229 589 Next »
Advertisements