Found 9150 Articles for Object Oriented Programming

Generate Random boolean in Java

George John
Updated on 29-Jun-2020 06:14:19

14K+ Views

In order to generate Random boolean in Java, we use the nextBoolean() method of the java.util.Random class. This returns the next random boolean value from the random generator sequence.Declaration −The java.util.Random.nextBoolean() method is declared as follows −public boolean nextBoolean()Let us see a program to generate random boolean in Java −Example Live Demoimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       System.out.println(rd.nextBoolean()); // displaying a random boolean    } }OutputtrueNote − The output might vary on Online Compilers.

Get elapsed time in days in Java

Ankith Reddy
Updated on 26-Jun-2020 09:22:48

332 Views

To get the elapsed time of an operation in days in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in days in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long ... Read More

Compute elapsed time in hours in Java

Arjun Thakur
Updated on 26-Jun-2020 09:23:47

545 Views

To get the elapsed time of an operation in hours in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in hours in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long ... Read More

Get elapsed time in minutes in Java

Chandu yadav
Updated on 26-Jun-2020 09:25:07

3K+ Views

To get the elapsed time of an operation in minutes in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in minutes in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long ... Read More

Compute the elapsed time of an operation in Java

George John
Updated on 26-Jun-2020 09:28:08

250 Views

To compute the elapsed time of an operation in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long start = System.currentTimeMillis();   ... Read More

Compute elapsed time in seconds in Java

Ankith Reddy
Updated on 26-Jun-2020 09:29:32

13K+ Views

To compute the elapsed time of an operation in seconds in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long start = ... Read More

Compare two int arrays in a single line in Java

Arjun Thakur
Updated on 26-Jun-2020 09:30:01

429 Views

Two int arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two int arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new int[] { 45, 12, 90 }, new int[] { 45, 12, 90 });       System.out.println("The two int arrays are equal? ... Read More

Call the run() method of the Timer Task in Java

Chandu yadav
Updated on 26-Jun-2020 09:31:30

799 Views

The java.util.TimerTask.run() method looks onto the action to be performed by the task. It is used to carry out the action performed by the task.Declaration −The java.util.TimerTask.run() method is declared as follows −public abstract void run()Let us see an example program to call the run() method of the Timer Task −Example Live Demoimport java.util.*; class MyTask extends TimerTask {    public void run() {       System.out.println("Running");    } } public class Example {    public static void main(String[] args) {       // creating timer task, timer       TimerTask task = new MyTask();       ... Read More

Cancel the Timer Task in Java

George John
Updated on 26-Jun-2020 09:32:11

6K+ Views

In order to cancel the Timer Task in Java, we use the java.util.TimerTask.cancel() method. The cancel() method returns a boolean value, either true or false. The cancel() method is used to cancel the timer task.Declaration −The java.util.TimerTask.cancel() method is declared as follows −public boolean cancel()The cancel() methods returns true when the task is scheduled for one-time execution and has not executed until now and returns false when the task was scheduled for one-time execution and has been executed already.Let us see a program to illustrate the use of the java.util.TimerTask.cancel() method −Example Live Demoimport java.util.*; class MyTask extends TimerTask {   ... Read More

Schedule a task for repeated fixed delay execution in Java

Ankith Reddy
Updated on 26-Jun-2020 09:34:27

1K+ Views

In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.There are two ways in which a task can be scheduled for repeated fixed-delay execution. They are as follows −Scheduling a task for repeated fixed-delay execution at a specified timeScheduling a task for repeated fixed-delay execution after a specified delayScheduling a task for repeated fixed-delay execution at a specified timeThe void schedule(TimerTask task, Date firstTime, long period) method schedules tasks for ... Read More

Advertisements