Found 34486 Articles for Programming

Use boolean value to stop a thread in Java

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

756 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

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

Initializer for final static field in Java

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

779 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

Obtain the hash code for a string in Java

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

167 Views

The hashCode() method is used to obtain the hash code for a string. This method does not accept any parameters as it is a default method and it returns a hash code value.A program that demonstrates the hashCode() method in Java is given as follows:Example Live Demoimport java.io.*; public class Demo {    public static void main(String args[]) {       String str = new String("The sky is blue");       System.out.println("The string is: " + str);       System.out.println("The Hashcode for the string is: " + str.hashCode());    } }OutputThe string is: The sky is blue The ... Read More

Using the finalize() method in Java Garbage Collection

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

997 Views

When a garbage collector determines that no more references are made to a particular object, then the finalize() method is called by the garbage collector on that object. The finalize() method requires no parameters and does not return a value.A program that demonstrates the finalize() method in Java is given as follows:Example Live Demoimport java.util.*; public class Demo extends GregorianCalendar {    public static void main(String[] args) {       try {          Demo obj = new Demo();          System.out.println("The current time is: " + obj.getTime());          obj.finalize();       ... Read More

Override the toString() method in a Java Class

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

5K+ Views

A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.A program that demonstrates this is given as follows:Example Live Democlass Student {    private int rno;    private String name;    public Student(int r, String n) {       rno = r;       name = n;    }    public String toString() {       return rno + " " + name;    } } public class Demo {    public static void main(String[] args) {       Student ... Read More

Use static Import for sqrt() and pow() methods in class Math in Java

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

729 Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class methods sqrt() and pow() in the package java.lang are static imported. A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo {    public static void main(String args[]) {       double num = 4.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));       ... Read More

Static import the Math Class Methods in Java

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

15K+ Views

Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.The Math class method sqrt() in the package java.lang is static imported.A program that demonstrates this is given as follows:Example Live Demoimport static java.lang.Math.sqrt; public class Demo {    public static void main(String[] arg) {       double num = 36.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));    } }OutputThe number is: 36.0 The square root ... Read More

Advertisements