hi ^_^
I want to ask what is the difference between (System.out.println(B);) and (System.out.println(new string(B));) ?!
actually it gives me the same output in my code!
so then >>what is the use of [new string(array)] in general ???
thank U :)))

public class CopyArray {

    public static void main(String[] args) {
char A[]={'j','a','v','a'};
        char B[] =new char [4];
        System.arraycopy(A, 0, B,0, 4 );
        System.out.println(B);

    }

}
---------------------------------------------------------------
 public class CopyArray {

    public static void main(String[] args) {
char A[]={'j','a','v','a'};
        char B[] =new char [4];
        System.arraycopy(A, 0, B,0, 4 );
        System.out.println(new string(B));

    }

}

Recommended Answers

All 2 Replies

In this particular case it doesn't matter, but some methods won't accept an array of bytes, only a String, as parameter, which is when you want to use this.

An array of chars is one thing, a String is a differemt thing.
Arrays of chars are not very often used - all you can do is iterate through the array to process the chars one at a time
Strings are used all the time - they have dozens of valuable methods, and wil also allow you to get the individual chars that make up the String.

new String(anArrayOfChars) takes a not-very-useful array of chars and creates a new String that you can use with all the methods of the String class (and many other methods that that use Strings as parameters)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.