Object Oriented Programming Articles

Page 212 of 589

Java regex program to verify whether a String contains at least one alphanumeric character.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Following regular expression matches a string that contains at least one alphanumeric characters −"^.*[a-zA-Z0-9]+.*$";Where, ^.* Matches the string starting with zero or more (any) characters.[a-zA-Z0-9]+ Matches at least one alpha-numeric character..*$ Matches the string ending with zero or more (ant) characters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "^.*[a-zA-Z0-9]+.*$";     ...

Read More

BinaryOperator Interface in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

The BinaryOperator interface represents an operation upon two operands of the same type, producing a result of the same type as the operands.Following are the methods −Modifier and TypeMethod and DescriptionmaxBy(Comparator

Read More

Java regex program to add space between a number and word in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

You can form matched groups in the regular expression by separating the expressions with parenthesis. In following regular expression the first group matches digits and the second group matches the English alphabet −(\d)([A-Za-z])In short, it matches the part in the input string where a digit followed by an alphabet.Since the expression $1 indicates Group1 and $2 indicates Group2, if you replace The above Java regular expression with $1 $2, using the replace() method (of the String class) a space will be added between number and a word in the given input string when a number is followed by a word.Exampleimport ...

Read More

How to get Exception log from a console and write it to external file in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) and performing division between them. We are wrapping this code in try block with three catch blocks catching ArithmeticException, InputMismatchException and, ArrayIndexOutOfBoundsException. In each of them we are invoking the writeToFile() method.This method accepts an exception object, and appends it to a file using the write() method of the Files ...

Read More

Can we to override a catch block in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling method.When a piece of code in a method throws an exception (compile time) we must either handle it by wrapping it within the try-catch pair or, throw it (postpone) to the calling method using the throws keyword else a compile time error occurs.In the following Java example the code in ...

Read More

How to check if a file is readable, writable, or, executable in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

In general, whenever you create a file you can restrict/permit certain users from reading/writing/executing a file.In Java files (their abstract paths) are represented by the File class of the java.io package. This class provides various methods to perform various operations on files such as read, write, delete, rename, etc.In addition, this class also provides the following methods −setExecutble() − This method issued to set the execute permissions to the file represented by the current (File) object.setWritable() − This method is used to set the write permissions to the file represented by the current (File) object.setReadable() − This method is used ...

Read More

Out of memory exception in Java:

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Whenever you create an object in Java it is stored in the heap area of the JVM. If the JVM is not able to allocate memory for the newly created objects an exception named OutOfMemoryError is thrown.This usually occurs when we are not closing objects for long time or, trying to act huge amount of data at once.There are 3 types of errors in OutOfMemoryError −Java heap space.GC Overhead limit exceeded.Permgen space.Example 1public class SpaceErrorExample {    public static void main(String args[]) throws Exception {       Float[] array = new Float[10000 * 100000];    } }OutputRuntime exceptionException in ...

Read More

Checked Vs unchecked exceptions in Java programming.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Checked exceptionsA checked exception is an exception that occurs at the compile time, these are also called as compile-time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after the execution of the complete program.Exampleimport java.io.File; import java.io.FileInputStream; public class Test {    public static void main(String args[]){       System.out.println("Hello");       try{         ...

Read More

Shadowing of static methods in Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

When superclass and the subclass contain the same instance methods including parameters, when called, the superclass method is overridden by the method of the subclass.Exampleclass Super{    public void sample(){       System.out.println("Method of the Super class");    } } public class MethodOverriding extends Super {    public void sample(){       System.out.println("Method of the Sub class");    }    public static void main(String args[]){       MethodOverriding obj = new MethodOverriding();       obj.sample();    } }OutputMethod of the Sub classMethod shadowingWhen superclass and subclass contain the same method including parameters and if they are ...

Read More

How to import classes from within another directory/package in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 7K+ Views

In Java classes and interfaces related to each other are grouped under a package. The package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in the java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set a classpath for the JAR file holding the required package.Import the required class from the package using the import keyword. While ...

Read More
Showing 2111–2120 of 5,881 articles
« Prev 1 210 211 212 213 214 589 Next »
Advertisements