简体   繁体   中英

String.valueOf() behavior with char[] in List<char[]>

I was working with char[] and Collection with the below code :-

    char[] ch = { 'a', 'b', 'c' };
    List<char[]> chList = new ArrayList<char[]>();
    chList.add(new char[]{'d','e','f'});
    chList.add(new char[]{'g','h','i'});
    String chSt = String.valueOf(ch);
    String chListSt = chList.toString(); 
    System.out.println(chSt);  // outputs  abc
    System.out.println(chListSt); // outputs [[C@8288f50b, [C@b6d2b94b] instead [def, ghi]

Now what I observed above is :-

    String chSt = String.valueOf(ch);

I know the above code behaviour is correct for char[] in String.valueOf() , so for the above code abc is printed. Now consider the next line.

    String chListSt = chList.toString(); 

Also for the above code I know the toString() for List is defined in AbstractList and in the code of this overriden toString() I found buffer.append(next); which calls String.valueOf() method on the char[] which corresponds to next here.

So it should also print like [def, ghi] , as in the direct case with char[] in the line String chSt = (String.valueOf(ch));

Why is this change in behaviour is there in both the cases while same method String.valueOf() is called on the char[] ?

You're seeing the difference of calling different overloads of String.valueOf . Here's a simpler example:

public class Test {
    public static void main(String[] args) {
        char[] chars = { 'a', 'b', 'c' };
        System.out.println(String.valueOf(chars));  // abc
        Object object = chars;
        System.out.println(String.valueOf(object));  // [C@...
    }
}

The call you've found in StringBuffer or StringBuilder is just going to call String.valueOf(Object) - which then calls toString() on the array (after checking the reference isn't null). Arrays don't override toString in Java, hence the output you're getting.

Code String chListSt = chList.toString(); simply called toString() implementation of List. List implementation calls toString() implementation on the elements.

Your current list has array objects in the list as an elements, so you are getting an array.toString() representation on the console which results hexadecimal code printed for char[].

Instead of char[] try to save string in the array. It will have same performance and results will be readable.

char[] ch = { 'a', 'b', 'c' };

The above one is a literal and hence valueof() method gives it value.

chList.add(new char[]{'d','e','f'});
chList.add(new char[]{'g','h','i'});

These lists have objects stored in them as a result you are getting their hashcodes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM