Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

500 articles

Types of quantifiers in Java regex

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 934 Views

If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.Greedy quantifiers − Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");     ...

Read More

How to Get a slice of a primitive array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 977 Views

You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Exampleimport java.util.Arrays; public class SlicingAnArray {    public static int[] sliceArray(int array[], int startIndex, int endIndex ){       int size = endIndex-startIndex;       int part[] = new int[size];       //Copying the contents of the array       for(int i=0; iarray[i]);       part = stream.toArray();       //Copying the contents of the array       for(int i=0; i

Read More

Matching Nonprintable Characters using Java regex

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

There are 7 common non printable characters used in general and each character has its own hexadecimal representation.NamecharactersHexa-decimal representationbell\a0x07Escape\e0x1BForm feed\f0x0CLine feed0x0ACarriage return\r0X0DHorizontal tab\t0X09Vertical tab\v0X0BExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "\x09";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Matching the compiled pattern in the String       Matcher matcher = ...

Read More

Why do we need generics in Java?

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

Reference typesAs we know a class is a blue print in which we define the required behaviors and properties and, an interface is similar to class but it is a Specification (containing abstract methods).These are also considered as datatypes in Java, unlike other primitive datatypes a literal of these kind of types points/refers to the location of the object. They are also known as reference types.GenericsGenerics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the ...

Read More

Is Java matcher thread safe in Java?

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

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions.Matcher classA Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.The Instances of this class are not safe ...

Read More

What are parametrized constructors in Java?

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

A constructor is similar to method and it is invoked at the time 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 and, have no return type.Parameterized constructorsA parameterized constructor accepts parameters with which you can initialize the instance variables. Using parameterized constructor, you can initialize the class variables dynamically at the time of instantiating the class with distinct values.Syntaxpublic class Sample{    Int i;    public sample(int i){       this.i = i;    } }Examplepublic class Test {    String ...

Read More

How to create a default constructor in Java?

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

Default constructor (No-arg constructor)A no-arg constructor doesn’t accepts any parameters, it instantiates the class variables with their respective default values (i.e. null for objects, 0.0 for float and double, false for Boolean, 0 for byte, short, int and, long).There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.Rules to be rememberedWhile defining the constructors you should keep the following points in mind.A constructor does not have return type.The name of the constructor is same as the name of the class.A constructor cannot be abstract, final, static and Synchronized.You can use the access specifiers ...

Read More

What are various ways to compare time values in Java?

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

The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.Exampleimport java.time.LocalTime; public class Test {    public static void main(String args[]) {         LocalTime Time1 = LocalTime.of(10, 15, 45);       LocalTime Time2 = LocalTime.of(07, 25, 55);             Boolean bool1 = Time1.isAfter(Time2);         Boolean bool2 = Time1.isBefore(Time2);       if(bool1){          System.out.println(Time1+" is after "+Time2);       }else if(bool2){          System.out.println(Time1+" is ...

Read More

How to compare two dates in String format in Java?

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

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string.Parse the date string using the parse() method.The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two datesExampleOnce you create date objects from strings you can compare them using either of these methods as shown below −import java.text.ParseException; import ...

Read More

How to parse date sting to date in Java?

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

You can parse a string containing a data to date value using the following ways −The constructor SimpleDateFormat class accepts a String value representing the desired date format and creates this object. You can parse the date string using the parse() method of this class.The parse() method of the LocalDate class accepts a String value representing a date and returns a LocalDate object.The DateUtils provides utility to format date you can find it in apache.commons package. The parseDate() method of the DateUtils class accepts a format string and a date string as parameters and returns a Date object.The parse() method of ...

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