Skip to main content

APIzation: Replication Package

[Q#16475457][A#16475547] How to initailize byte array of 100 bytes in java with all 0's

https://stackoverflow.com/q/16475457

How to initialize byte array of 100 bytes in java with all 0's. I want to create 100 byte array and initialize it with all 0's

Answer

https://stackoverflow.com/a/16475547

A new byte array will automatically be initialized with all zeroes. You don't have to do anything. The more general approach to initializing with other values, is to use the Arrays class.

APIzation

import java.util.Arrays;

byte[] bytes = new byte[100];
Arrays.fill( bytes, (byte) 1 );
package com.stackoverflow.api;

import java.util.Arrays;

public class Human16475547 {

  public static byte[] initializeByteArray() {
    byte[] bytes = new byte[100];
    Arrays.fill(bytes, (byte) 1);
    return bytes;
  }
}

package com.stackoverflow.api;

import java.util.Arrays;

/**
 * How to initailize byte array of 100 bytes in java with all 0's
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/16475547">https://stackoverflow.com/a/16475547</a>
 */
public class APIzator16475547 {

  public static void initailizeArray(byte[] bytes) throws Exception {
    Arrays.fill(bytes, (byte) 1);
  }
}