Java byte Array

It is used to store byte data type values only. With the following examples you can learn how to declare byte array, how to assign values to byte array and how to get values from byte array.

byte Array Example
/* byte Array Example */
/* Save with file name ByteArray.java */

public class ByteArray
{
	public static void main(String args[])
	{
		//BYTE ARRAY DECLARATION
		byte b[];
		//MEMORY ALLOCATION FOR BYTE ARRAY
		b = new byte[4];
		//ASSIGNING ELEMENTS TO BYTE ARRAY
		b[0] = 20;
		b[1] = 10;
		b[2] = 30;
		b[3] = 5;
		//BYTE ARRAY OUTPUT
		System.out.println();
		System.out.println("byte Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<b.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + b[i]);
		}
	}
}
                        

In this example you can learn how to assign values to byte array at the time of declaration.

byte Array Example 2
/* byte Array Example 2 */
/* Save with file name ByteArray2.java */

public class ByteArray2
{
	public static void main(String args[])
	{
		//BYTE ARRAY DECLARATION AND ASSIGNMENT
		byte b[] = {20,10,30,5};
		//BYTE ARRAY OUTPUT
		System.out.println();
		System.out.println("byte Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<b.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + b[i]);
		}
	}
}
                        

In this example you can learn how to declare byte array with other byte variables.

byte Array Example 3
/* byte Array Example 3 */
/* Save with file name ByteArray3.java */

public class ByteArray3
{
	public static void main(String args[])
	{
		//BYTE ARRAY DECLARATION
		byte b[], a; //b IS AN ARRAY a IS NOT AN ARRAY
		//MEMORY ALLOCATION FOR BYTE ARRAY
		b = new byte[4];
		//ASSIGNING ELEMENTS TO BYTE ARRAY
		b[0] = 20;
		b[1] = 10;
		b[2] = 30;
		b[3] = 5;
		a = 100;
		//BYTE ARRAY OUTPUT
		System.out.println();
		System.out.println("byte Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("a value is : "+a);
		System.out.println();
		for(int i=0;i<b.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + b[i]);
		}
	}
}
                        

In this example you can learn how to assign byte array to other byte array.

byte Array Example 4
/* byte Array Example 4 */
/* Save with file name ByteArray4.java */

public class ByteArray4
{
	public static void main(String args[])
	{
		//BYTE ARRAY DECLARATION
		byte[] a, b; //a AND b ARE ARRAY VARIABLES
		//MEMORY ALLOCATION FOR BYTE ARRAY
		b = new byte[4];
		//ASSIGNING ELEMENTS TO BYTE ARRAY
		b[0] = 20;
		b[1] = 10;
		b[2] = 30;
		b[3] = 5;
		//ASSIGNING b ARRAY TO a ARRAY VARIABLE
		a = b;
		//BYTE ARRAY OUTPUT
		System.out.println();
		System.out.println("byte Array Example");
		System.out.println("==================");
		System.out.println();
		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();
		System.out.println("a array values");
		for(int i=0;i<b.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + a[i]);
		}
	}
}