Array and String are very closely related, not with anything else but with popularity. Many times we need to
convert an array to String or
create a array from String, but unfortunately there is no direct way of doing this in Java. Though you can convert array to String by simply calling their
toString() method, you will not get any meaningful value. If you convert an integer array to String, you will get something like
I@4fee225 due to default implementation of
toString() method form
java.lang.Object class. First
I shows the type of the array and content after
@ is hash code value in hexadecimal. How valuable that is? This is not what I want to see, I was interested on contents rather than hashcode. Fortunately Java provides a utility class called
java.util.Arrays, which provides several utility method for arrays in Java. For example, here we have method to
sort array, search elements using binary search, fill array,
methods to check if two arrays are equal or not, copy range of values from one array to another, and much needed
toString() and
deepToString() method to convert both one dimensional and multi-dimensional array to String. These method provides content view of array, for example, when you convert an integer array
{1, 2, 3, 4, 5, 6, 7} to String, you will get
[1, 2, 3, 4, 5, 6, 7] instead of
[I@2ab6994f . In this article, we will see examples to convert different types of array to String e.g.
int,
char,
byte,
double,
float,
Object and
String array itself. We will also learn
how to convert two dimensional array to String in Java.