Found 2616 Articles for Java

Differences between abstract class and concrete class in Java

Mahesh Parahar
Updated on 08-Dec-2023 10:35:21

10K+ Views

In Java, abstraction is achieved using Abstract classes and interfaces. An abstract class contains abstract methods which a child class. Following are the important differences between abstract class and a concrete class. Sr. No. Key Abstract Class Concrete Class 1 Supported Methods Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract. 2 Instantiation Abstract class can not be instantiated using new keyword. Concrete class can be instantiated using new keyword. 3 Abstract Method Abstract class may or may not have abstract methods. Concrete clas can not have an abstract method. 4 Final Abstract class can not be declared as ... Read More

Differences between abstract class and interface in Java

Mahesh Parahar
Updated on 08-Dec-2023 10:45:00

5K+ Views

In Java, abstraction is achieved using Abstract classes and interfaces. Both contains abstract methods which a child class or implementing class has to implement. Following are the important differences between abstract class and an interface. Sr. No. Key Abstract Class Interface 1 Supported Methods Abstract class can have both an abstract as well as concrete methods. Interface can have only abstract methods. Java 8 onwards, it can have default as well as static methods. 2 Multiple Inheritance Multiple Inheritance is not supported. Interface supports Multiple Inheritance. 3 Supported Variables final, non-final, static and non-static variables supported. Only static and final variables are permitted. 4 Implementation Abstract class can implement an interface. Interface can not implement an interface, it can extend ... Read More

Accepting date strings (MM-dd-yyyy format) using Java regex?

Maruthi Krishna
Updated on 21-Feb-2020 10:39:39

3K+ Views

The following is the regular expression to match the date in the dd-MM-yyyy format.^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$To match a date in a string in that format.Compile the above expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingDate {    public static void main(String[] args) {       String date = "01/12/2019";   ... Read More

Posix character classes p{Upper} Java regex

Maruthi Krishna
Updated on 21-Nov-2019 08:16:31

174 Views

This class matches the upper case alphabetic characters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Posix_LowerExample {    public static void main( String args[] ) {       //Regular expression to match upper case letters       String regex = "^\p{Upper}+$";       //Getting the input data       Scanner sc = new Scanner(System.in);       System.out.println("Enter 5 input strings: ");       String input[] = new String[5];       for (int i=0; i

Posix character classes p{Lower} Java regex

Maruthi Krishna
Updated on 21-Nov-2019 08:14:19

191 Views

This class matches the lower case alphabetic characters i.e. a to z.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Posix_LowerExample {    public static void main( String args[] ) {       //Regular expression to match lower case letters       String regex = "^\p{Lower}+$";       //Getting the input data       Scanner sc = new Scanner(System.in);       System.out.println("Enter 5 input strings: ");       String input[] = new String[5];       for (int i=0; i

How to remove non-ASCII characters from strings

Maruthi Krishna
Updated on 21-Nov-2019 08:10:22

919 Views

The Posix character class \p{ASCII} matches the ASCII characters and the meta character ^ acts as negation.i.e. The following expression matches all the non-ASCII characters."[^\p{ASCII}]"The replaceAll() method of the String class accepts a regular expression and a replacement-string and, replaces the characters of the current string (matching the given pattern) with the specified replacement-string.Therefore, You can remove the matched characters by replacing them with the empty string “, using the replaceAll() method.Example 1import java.util.Scanner; public class Exp {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       String regex = ... Read More

How to match the regex meta characters in java as literal characters.

Maruthi Krishna
Updated on 21-Nov-2019 08:08:32

199 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.The filed LITERAL of the enables literal parsing of the pattern. i.e. all the regular expression metacharacters and escape sequences don’t have any special meaning they are treated as literal characters. Therefore, If you need to match the regular expression metacharacters as normal characters you need to pass this as a flag value to the compile() method along with the regular expression.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String[] ... Read More

How match a string irrespective of case using Java regex.

Maruthi Krishna
Updated on 21-Nov-2019 08:06:13

150 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. Therefore, if you pass as flag value to the compile() method along with your regular expression, characters of both cases will be matched.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine(); ... Read More

How to extract numbers from a string using regular expressions?

Maruthi Krishna
Updated on 21-Nov-2019 08:03:39

14K+ Views

You can match numbers in the given string using either of the following regular expressions −“\d+” Or, "([0-9]+)"Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: ");       String data = sc.nextLine();       //Regular expression to match digits in a string       String regex = "\d+";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Creating a Matcher object       Matcher ... Read More

How to replace multiple spaces in a string using a single space using Java regex?

Maruthi Krishna
Updated on 21-Nov-2019 07:59:21

11K+ Views

The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.Match the input string with the above regular expression and replace the results with single space “ ”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAllExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = ... Read More

Advertisements