Java - StringUtils.join () returning a pointer

I tried to join the elements of an array using the StringUtils.join(array, separator) method from the Apache community library. Can someone explain to me why I can not get the resulting string and only a pointer to its location if I use an array of primitive types such as int[] ? See the sample code below:

 public static void main(String[] args){ String[] s = new String[]{"Anna","has", "apples"}; System.out.println(StringUtilities.join(s, " ")); int[] a = new int[]{1,2,3}; System.out.println(StringUtilities.join(a, " ")); Integer[] b = new Integer[]{1,2,3}; System.out.println(StringUtilities.join(b, " ")); } 

Works only with Integer arrays. I realized that arrays of primitives are internally handled differently than ArrayList or other higher-order objects, but then why is it possible (one topic more or less, but maybe another question) to create an instance of HashMap<String, int[]> without any warnings, exceptions? Are arrays internally wrapped in another object? Only for cards? From what I read from the document, you cannot parameterize a map, set, list of arrays, etc. With primitive types that I understand, but then ... I find this a bit confusing. Any reasonable explanation would be appreciated. Thanks.

+5
source share
2 answers

Take a look at the signature for int arrays of StringUtils#join :

 join(byte[] array, char separator) 

You called join using

 StringUtils.join(a, " "), 

using string instead of char. Try using

 StringUtils.join(a, ' ') 

instead.

It so happened that your call matched a different signature:

 join(T... elements), 

therefore, your arguments are interpreted as two objects, an integer array and a string with a space character. When creating a result string, the method concatenated the string representation of an integer array with a string.

+6
source

An array of ints (primitive) is an object.

I suspect that the implementation does not support primitive arrays and calls toString() on them, which leads to something like [ I@659e0bfd (which is converted to a one-dimensional (only one [ ) I nts array and its "memory location").

+4
source

All Articles