I assume you want to map every value in the arr1 array to an int, and the result should be an int[] (not one int). Stream the array, map the elements to an int, and then invoke toArray. Like,
int[] numArr = Arrays.stream(arr1).mapToInt(Integer::valueOf).toArray();
Note that invoking valueOf in this way is still an example of parsing the String to an int.
I assume you want to map every value in the arr1 array to an int, and the result should be an int[] (not one int). Stream the array, map the elements to an int, and then invoke toArray. Like,
int[] numArr = Arrays.stream(arr1).mapToInt(Integer::valueOf).toArray();
Note that invoking valueOf in this way is still an example of parsing the String to an int.
Using Streams:
int[] intArray = Stream.of(arr1).mapToInt(Integer::parseInt).toArray();
String.valueOf does not have a byte[] argument, so it will be processed as Object and the toString() method will be called, since arrays doesn't implements this method, Object.toString() will be process in the array and it's result varies from each instance.
If you want to convert byte[] to String use the constructor String(byte[]) or String(byte[] bytes, Charset charset)
byte[] test = {-51};
byte[] test2 = {-51};
byte[] test3 = {-51};
System.out.println(new String(test));
System.out.println(new String(test2));
System.out.println(new String(test3));
Result:
Í
Í
Í
If you want to view the content of the array use the Arrays.toString(byte[])
byte[] test = {-51};
byte[] test2 = {-51};
byte[] test3 = {-51};
System.out.println(Arrays.toString(test));
System.out.println(Arrays.toString(test2));
System.out.println(Arrays.toString(test3));
Result:
[-51]
[-51]
[-51]
The numbers you're seeing are the hash codes of the array objects.
To see the contents of the arrays, use Arrays.toString():
System.out.println(Arrays.toString(test));
System.out.println(Arrays.toString(test2));
System.out.println(Arrays.toString(test3));
According to the Java documentation, String.valueOf() returns:
if the argument is
null, then a string equal to"null"; otherwise, the value ofobj.toString()is returned.
So there shouldn't really be a difference except for an additional method invocation.
Also, in the case of Object#toString, if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.
public static void main(String args[]) {
String str = null;
System.out.println(String.valueOf(str)); // This will print a String equal to "null"
System.out.println(str.toString()); // This will throw a NullPointerException
}
Differences between String.valueOf(Object) and Object.toString() are:
1) If string is null,
String.valueOf(Object) will return "null", whereas Object::toString() will throw a null pointer exception.
public static void main(String args[]){
String str = null;
System.out.println(String.valueOf(str)); // it will print null
System.out.println(str.toString()); // it will throw NullPointerException
}
2) Signature:
valueOf() method of String class is static. whereas toString() method of String class is non static.
The signature or syntax of string's valueOf() method is given below:
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
public static String valueOf(Object o)
The signature or syntax of string's toString() method is given below:
public String toString()