Object Oriented Programming Articles

Page 289 of 589

Display the amount of free memory in the Java Virtual Machine

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 444 Views

In order to display the amount of free memory in the Java Virtual Machine, we use the freeMemory() method. It is a method of the java.lang.Runtime Class. It returns the amount of free memory in the Java Virtual Machine. If we call the gc method, there is a possibility of increase of free memory.Declaration − The java.lang.Runtime.freeMemory() method is declared as follows −public long freeMemory()Let us see a program to display the amount of free memory in the Java Virtual Machine −Examplepublic class Example {    public static void main(String[] args) {       // print statement at the ...

Read More

Get number of available processors in Java

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 6K+ Views

In order to get the number of available processors in Java, we use the availableProcessors() method. The java.lang.Runtime.availableProcessors() method returns the number of processors which are available to the Java virtual machine. This number may vary during a particular call of the virtual machine.Declaration − The java.lang.Runtime.availableProcessors() method is declared as follows −public int availableProcessors()Let us see a program to get the number of available processors in Java −Examplepublic class Example {    public static void main(String[] args) {       // print statement at the start of the program       System.out.println("Start...");       System.out.print("Number of ...

Read More

Generate Random Integer Numbers in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 7K+ Views

In order to generate Random Integer Numbers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to generate random integer numbers in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       System.out.println(rd.nextInt());    } }Output27100093Note - The output might vary on Online Compilers.

Read More

Generate Random bytes in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 8K+ Views

In order to generate random bytes in Java, we use the nextBytes() method. The java.util.Random.nextBytes() method generates random bytes and provides it to the user defined byte array.Declaration − The java.util.Random.nextBytes() method is declared as follows −public void nextBytes(byte[] bytes)where bytes is the byte array.Let us see a program to generate random bytes in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random();       byte[] arr = new byte[7];       rd.nextBytes(arr);       System.out.println(arr);    } }Output[B@15db9742Note − The output might vary ...

Read More

Roll a six-sided die 6000 times in Java

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 688 Views

In order to roll a six sided die 6000 times in Java, we need to the nextInt() statement with decision making statements.The nextInt() method returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to roll a six sided die 6000 times −Exampleimport java.util.Random; public class Example {    public static void main(String args[]) {       Random rd = new Random(); // random number generator       int freq[] = new int[6]; // creating an array to compute frequency of ...

Read More

Convert a Queue to a List in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 1K+ Views

In order to convert a Queue to a List in Java, we can create an LinkedList and pass the Queue as an argument in the parameterized constructor of an ArrayList. This can be done as follows −Queue q = new LinkedList(); List l = new ArrayList(q);The quickest way is used to LinkedList in the first place which can be used both as a List and a Queue. This can be done as follows −Queue q = new LinkedList(); List l = (List) q;Let us see a program to convert a queue to a list −Exampleimport java.util.LinkedList; import java.util.List; import java.util.Queue; ...

Read More

How to schedule tasks in Java to run for repeated fixed-delay execution, beginning after the specified delay

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 1K+ Views

One of the methods in the Timer class is the void schedule(TimerTask task, long delay, long period) method. This method schedules tasks for repeated fixed-delay execution, beginning after the specified delay.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.Declaration −The java.util.Timer.schedule(TimerTask task, long delay, long period) is declared as follows −public void schedule(TimerTask task, long delay, long period)Here, task is the task to be scheduled, delay is the delay ...

Read More

Dump the content of an array in Java

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 395 Views

An array can be easily printed by using the method java.util.Arrays.toString() in Java. This method returns a string representation of the array contents that is enclosed in square brackets. If the array is null, then this method returns null.A program that demonstrates this is given as follows −Exampleimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       String str[] = {"John", "Harry", "Sally", "Emma", "Peter"};       System.out.println("The array content is:");       System.out.println(Arrays.toString(str));    } }OutputThe array content is: [John, Harry, Sally, Emma, Peter]Now let us understand the above program.The string ...

Read More

Replace All Elements Of ArrayList with with Java Collections

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 2K+ Views

In order to replace all elements of ArrayList with Java Collections, we use the Collections.fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.Declaration −The java.util.Collections.fill() method is declared as follows −public static void fill(List

Read More

Swap elements of ArrayList with Java collections

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 10K+ Views

In order to swap elements of ArrayList with Java collections, we need to use the Collections.swap() method. It swaps the elements at the specified positions in the list.Declaration −The java.util.Collections.swap() method is declared as follows −public static void swap(List list, int i, int j)where i is the index of the first element to be swapped, j is the index of the other element to be swapped and list is the list where the swapping takes place.Let us see a program to swap elements of ArrayList with Java collections −Exampleimport java.util.*; public class Example {    public static void main ...

Read More
Showing 2881–2890 of 5,881 articles
« Prev 1 287 288 289 290 291 589 Next »
Advertisements