- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9313 Articles for Object Oriented Programming
131 Views
In general, the ? quantifier represents 0 or 1 occurrences of the specified pattern. For example - X? means 0 or 1 occurrences of X.The regex "t.+?m" is used to find the match in the string "tom and tim are best friends" using the ? quantifier.A program that demonstrates this is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("t.+?m"); Matcher m = p.matcher("tom and tim are best friends"); System.out.println("The input string is: tom and tim are best ... Read More
52 Views
The find() method finds the subsequence in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex packageA program that uses the find() method to find a subsequence in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("cool"); Matcher m = p.matcher("Java is cool"); System.out.println("Subsequence: cool"); System.out.println("Sequence: Java is cool"); if (m.find()) System.out.println("Subsequence found"); ... Read More
120 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:Example Live Demoimport 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
4K+ Views
An object can acquire the properties and behaviour of another object using Inheritance. In Java, the extends keyword is used to indicate that a new class is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the class.A program that demonstrates the extends keyword in Java is given as follows:Example Live Democlass A { int a = 9; } class B extends A { int b = 4; } public class Demo { public static void main(String args[]) { B obj = new B(); ... Read More
694 Views
The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.A program that demonstrates null in Java is given as follows:Example Live Demopublic class Demo { public static void main(String args[]) { String str = null; System.out.println("Value of str is: " + str); } }OutputValue of str is: nullNow let us understand the above program.In the main() method, the String str is initialized ... Read More
183 Views
A greedy qualifier repeats the specified token as many times as possible and then the engine backtracks and the greedy qualifier gives up matches to eventually find the required match.The regex "(\w+)(\d)(\w+)" is used to find the match in the string "EarthHas1Moon".A program that demonstrates this is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { String str = "EarthHas1Moon"; String regex = "(\w+)(\d)(\w+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); m.find(); ... Read More
492 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:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) throws Exception { String ... Read More
757 Views
A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called.A thread can be stopped using a boolean value in Java. The thread runs while the boolean value stop is false and it stops running when the boolean value stop becomes true.A program that demonstrates this is given as follows:Exampleclass ThreadDemo extends Thread { public boolean stop = false; int i = 1; public void run() { while (!stop) { try { ... Read More
3K+ Views
The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread.There are three static variables for thread priority in Java i.e. MIN_PRIORITY, MAX_PRIORITY and NORM_PRIORITY. The values of these variables are 1, 10 and 5 respectively.A program that demonstrates this is given as follows:Example Live Demopublic class ThreadDemo extends Thread { public void run() { System.out.println("Running..."); } public static void main(String[] args) { ThreadDemo thread1 = new ThreadDemo(); ThreadDemo thread2 = new ... Read More
585 Views
A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called.The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread.A program that demonstrates changing the thread priorities using the method setPriority() in Java is given as follows:Example Live Demopublic class ThreadDemo extends Thread { public void run() { System.out.println("Running..."); } public static void main(String[] args) { ThreadDemo thread1 ... Read More