Found 9326 Articles for Object Oriented Programming

A Reluctant qualifier in Java Regular Expressions

Rishi Raj
Updated on 30-Jul-2019 22:30:24

116 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

The extends Keyword in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

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

Is null a literal in Java?

Arushi
Updated on 30-Jul-2019 22:30:24

685 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

A greedy qualifier in Java Regular Expressions

Fendadis John
Updated on 30-Jul-2019 22:30:24

181 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

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

Rishi Raj
Updated on 30-Jul-2019 22:30:24

481 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

Use boolean value to stop a thread in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

750 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

Minimum and Maximum Priority Threads in Java

Arushi
Updated on 30-Jul-2019 22:30:24

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

Change Thread Priority in Java

Fendadis John
Updated on 30-Jul-2019 22:30:24

581 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

Initializer for final static field in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

759 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:Example Live Demopublic class Demo {    final static int num;    static {       System.out.println("Running static initialization block.");       num = 6;    }    public static void main(String[] args) {     ... Read More

Get Hash Code for Integer in Java

Arushi
Updated on 30-Jul-2019 22:30:24

2K+ Views

The HashCode for an Integer can be obtained using the hashCode() method in Java. This method does not accept any parameters and it returns a hash code value of the Integer object.A program that demonstrates the hashCode() method in Java is given as follows:Example Live Demoimport java.lang.*; public class Demo {    public static void main(String args[]) {       Integer i = new Integer(60);       System.out.println("The integer value is: " + i);       System.out.println("The Hashcode for the above value is: " + i.hashCode());    } }OutputThe integer value is: 60 The Hashcode for the above ... Read More

Advertisements