# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Print Byte Array in Java | In this post we will discuss how to print byte array as string in Java programming language. The byte array will contain N number of byte values ranging between -128 and 127.
To print byte arrays, we can use loops (for loop, for-each loop, while loop, do-while loop) or in-built methods like Arrays.toString(). Here we will see the following ways to print byte array in Java:-
- Using the Arrays.toString() method
- Using the Arrays.deepToString() method
- Using for-each loop
- Using for loop
- Using a while loop
How To Print Byte Array As String In Java using Arrays.toString()
Java has provided an Arrays class in java.util package which contains array manipulation methods for the task like searching, sorting, copying, and e.t.c. It also contains the toString() method which converts an array to a string.
Following is the method syntax of the Arrays.toString() method which can be used to convert byte array to string in Java:- public static String toString(byte[] a)
Java Program to Print Byte Array As String using Arrays.toString()
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
byte[] byteArray = { 10, 20, 90, 5, 127, -50 };
System.out.println(Arrays.toString(byteArray));
}
}
Output:-
[10, 20, 90, 5, 127, -50]
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
byte[] byteArray1 =
new byte[] { (byte) 500, (byte) 400, (byte) 300, 100 };
System.out.println(Arrays.toString(byteArray1));
System.out.println(Arrays.toString(
new byte[] { (byte) 500, (byte) 400, (byte) 300, 100 }));
byte[] byteArray2 = new byte[3];
byteArray2[0] = 10;
byteArray2[1] = (byte) 650;
byteArray2[2] = (byte) -800;
System.out.println(Arrays.toString(byteArray2));
}
}
Output:-
[-12, -112, 44, 100]
[-12, -112, 44, 100]
[10, -118, -32]
Print Byte Array As String In Java using Arrays.deepToString()
The Arrays.toString() can print only a one-dimensional byte array as a string. But if we have a multi-dimensional byte array then we can take the help of another method provided by the Java Arrays class:- deepToString().
Following is the syntax of the deepToString() method:- public static String deepToString(Object[] a)
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
byte[][] byteArray = { { 10, 20, 90 }, { 5, 127, (byte) 350 } };
System.out.println(Arrays.deepToString(byteArray));
}
}
Output:-
[[10, 20, 90], [5, 127, 94]]
Print Byte Array in Java using for-each loop
We can also use a for-each loop to print byte array in Java. The for-each loop will iterate the byte array, fetch each and every element of the array, and then we can display them.
public class Main {
public static void main(String args[]) {
byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
System.out.println("Byte array: ");
for (byte val : byteArray) {
System.out.print(val + " ");
}
}
}
Output:-
Byte array:
10 20 90 5 127 94
Print Byte Array in Java using for loop
For index-based iteration of the array, we can use for loop. The array elements
public class Main {
public static void main(String args[]) {
byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
System.out.println("Byte array: ");
for (int i = 0; i < byteArray.length; i++) {
System.out.print(byteArray[i] + " ");
}
}
}
Output:-
Byte array:
10 20 90 5 127 94
The Arrays.toString() method and Arrays.deepToString() directly converts an array to a string. And the for-each loop iterates the array. But before printing, if we want to modify the array then we can’t use any of the above three methods. In that case, we can use for loop, while loop, or do-while loop.
public class Main {
public static void main(String args[]) {
byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
System.out.println("Byte array: ");
byte sum = 0;
for (int i = 0; i < byteArray.length; i++) {
// Cumulative Sum
sum += byteArray[i];
byteArray[i] = sum;
System.out.print(byteArray[i] + " ");
}
}
}
Output:-
Byte array:
10 30 120 125 -4 90
Print Byte Array in Java using a while loop
public class Main {
public static void main(String args[]) {
byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
System.out.println("Byte array: ");
byte i = 0;
while (i <= byteArray.length) {
System.out.print(byteArray[i] + " ");
i++;
}
}
}
Output:-
Byte array:
10 20 90 5 127 94
In the while loop first, we declare & initialize then variable outside the loop. Later used within the loop, and getting updated after that.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!