The nextBytes(byte[] bytes) method is used to generate random bytes and places them into a user-supplied byte array.
Following is the declaration for java.util.Random.nextBytes() method.
public void nextBytes(byte[] bytes)
bytes −This is the non-null byte array in which to put the random bytes.
NA
NA
The following example shows the usage of java.util.Random.nextBytes()
package com.tutorialspoint; import java.util.*; public class RandomDemo { public static void main( String args[] ) { // create random object Random randomno = new Random(); // create byte array byte[] nbyte = new byte[30]; // put the next byte in the array randomno.nextBytes(nbyte); // check the value of array System.out.println("Value of byte array: " + nbyte); } }
Let us compile and run the above program, this will produce the following result.
Value of byte array: [B@addbf1