java.util.Random.nextBytes() Method


Advertisements


Description

The nextBytes(byte[] bytes) method is used to generate random bytes and places them into a user-supplied byte array.

Declaration

Following is the declaration for java.util.Random.nextBytes() method.

public void nextBytes(byte[] bytes)

Parameters

  • bytes −This is the non-null byte array in which to put the random bytes.

Return Value

NA

Exception

NA

Example

The following example shows the usage of java.util.Random.nextBytes()

Live Demo
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

java_util_random.htm

Advertisements
E-Books Store