Home » Java programming language

Java Random nextBytes() Method with Examples

Random Class nextBytes() method: Here, we are going to learn about the nextBytes() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

Random Class nextBytes() method

  • nextBytes() method is available in java.util package.
  • nextBytes() method is used to generate the next bytes randomly and put them into the given user-defined array (by).
  • nextBytes() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • nextBytes() method does not throw an exception at the time of copying bytes.

Syntax:

    public void nextBytes(byte[] by);

Parameter(s):

  • byte[] by – represents the byte array to contain random bytes.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void nextBytes(byte[] by) method of 
// Random

import java.util.*;

public class NextBytesOfRandom {
 public static void main(String args[]) {
  // Instantiates Random object
  Random ran = new Random();

  // Instantiates user defined 
  // array (by_arr)
  byte[] by_arr = new byte[20];

  // By using nextBytes() method isto
  // create random bytes and put them
  // in the user-defined byte array
  ran.nextBytes(by_arr);

  // Display 
  System.out.println("ran.nextBytes(by_arr): " + by_arr);
 }
}

Output

ran.nextBytes(by_arr): [[email protected]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.