Download Java byte Array in Formats
QR Code for this Page
Java byte Array
Complete Tutorial on Java primitive byte data type array.

Java byte Array

byte Array in Java

Java byte Array is used to store byte data type values only. The default value of the elements in a byte array is 0.

With the following Java byte array examples you can learn

  • how to declare Java byte array
  • how to assign values to Java byte array
  • how to get values from Java byte array

What does byte mean in Java ?

The byte is one of the primitive data types in Java. This means that the Java byte is the same size as a byte in computer memory: it's 8 bits, and can hold values ranging from -128 to 127. The byte data type comes packaged in the Java programming language and there is nothing special you have to do to get it to work.

How many bits is a byte in Java ?

The byte is equal to 8 bits in Java.

Is byte a keyword in Java ?

The byte is a keyword in Java which designates the 8 bit signed integer primitive type.

How to declare byte Array in Java ?

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

// declares an array of bytes
byte[] byteArray;

How to Initialize byte Array in Java ?

The byte array will be initialized to 0 when you allocate it. All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialized to 0, arrays of booleans are initialized to false and arrays of reference types are initialized to null.

byte[] byteArray = new byte[10];

What is the Initial or Default Value of a byte Array in Java ?

The initial value of a byte array is 0. Because the default value of a byte data type in Java is 0.

What is the length of a byte Array in Java ?

The length is a property of an array object in Java that returns the number of elements in a given array.

What is the Maximum length of a byte Array in Java ?

The maximum length of a byte array in Java is 2147483647. You can store elements upto 2147483647.

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.

Syntax :
1
2
< data type > < variable >[];
< data type >[] < variable >;

Java byte Array Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  Java byte Array Example
    Save with file name ByteArray.java  */
  
public class ByteArray
{
  public static void main(String args[])
  {
    //JAVA BYTE ARRAY DECLARATION
    byte b[];
    //MEMORY ALLOCATION FOR JAVA BYTE ARRAY
    b = new byte[4];
    //ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
    b[0] = 20;
    b[1] = 10;
    b[2] = 30;
    b[3] = 5;
    //BYTE ARRAY OUTPUT
    System.out.println("Java byte Array Example");
    for(int i=0;i<b.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + b[i]);
    }
  }
}

Following Java byte array example you can learn how to assign values to java byte array at the time of declaration.

How to assign values to java byte array at the time of declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  How to assign values to java byte array at the time of declaration Example
    Save with file name ByteArray2.java */
  
public class ByteArray2
{
  public static void main(String args[])
  {
    //JAVA BYTE ARRAY DECLARATION AND ASSIGNMENT
    byte b[] = {20,10,30,5};
    //JAVA BYTE ARRAY OUTPUT
    System.out.println("Java byte Array Example");
    for(int i=0;i<b.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + b[i]);
    }
  }
}

Following Java byte array example you can learn how to declare Java byte array with other Java byte array variables.

How to declare Java byte array with other Java byte array variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*  How to declare Java byte array with other Java byte array variables Example
    Save with file name ByteArray3.java */
  
public class ByteArray3
{
  public static void main(String args[])
  {
    //JAVA BYTE ARRAY DECLARATION
    byte b[], a; //b IS AN ARRAY a IS NOT AN ARRAY
    //MEMORY ALLOCATION FOR JAVA BYTE ARRAY
    b = new byte[4];
    //ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
    b[0] = 20;
    b[1] = 10;
    b[2] = 30;
    b[3] = 5;
    a = 100;
    //JAVA BYTE ARRAY OUTPUT
    System.out.println("Java byte Array Example");
    System.out.println("a value is : "+a);
    for(int i=0;i<b.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + b[i]);
    }
  }
}

Following Java byte array example you can learn how to assign Java byte array to other Java byte array.

How to assign Java byte array to other Java byte array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*  How to assign Java byte array to other Java byte array Example
    Save with file name ByteArray4.java */
  
public class ByteArray4
{
  public static void main(String args[])
  {
    //JAVA BYTE ARRAY DECLARATION
    byte[] a, b; //a AND b ARE ARRAY VARIABLES
    //MEMORY ALLOCATION FOR JAVA BYTE ARRAY
    b = new byte[4];
    //ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
    b[0] = 20;
    b[1] = 10;
    b[2] = 30;
    b[3] = 5;
    //ASSIGNING b ARRAY TO a ARRAY VARIABLE
    a = b;
    //JAVA BYTE ARRAY OUTPUT
    System.out.println("Java byte Array Example");
    System.out.println("b array values");
    for(int i=0;i<b.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + b[i]);
    }
    System.out.println("a array values");
    for(int i=0;i<b.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
  }
}
Download Java byte Array in Formats