-3

I was studying the Java tutorials here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

However a doubt appeared:

Why do I need the new String()? Is there any potential problem of using directly System.out.println(copyTo)? I thought that under the hood the println already had the toString method that converts to strings.

   class ArrayCopyOfDemo {
      public static void main(String[] args) {
        
        char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e',
            'i', 'n', 'a', 't', 'e', 'd'};
            
        char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);
        
        System.out.println(new String(copyTo));
      }
   }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
DTK
  • 95
  • 1
  • 9
  • 6
    Have you tried changing it to just `System.out.println(copyTo)` and looking at the result that is being printed? – OH GOD SPIDERS Jan 26 '21 at 11:38
  • 1
    @OHGODSPIDERS what does it show? [There's no difference for `char[]`](https://ideone.com/hj1owZ), because `PrintStream` has an overload for `char[]`. – Andy Turner Jan 26 '21 at 11:53

3 Answers3

3

There is no need for new String(copyTo) there. copyTo would suffice.

In general, you don't want to invoke println on an array. For example:

System.out.println(new int[]{1, 2, 3});

wouldn't print [1, 2, 3] or 123, but rather something like [I@243432. This is because there is no special overload for int[] (or other array types, except for char[]), so println(Object) is used. This invokes toString() on the parameter; but arrays don't have useful toString() implementations.

However, the PrintStream.println(char[]) method does work somewhat sensibly, and produces an output as if the char[] were converted to a String first (but note that the toString() method isn't used to produce this: no String is created from the array).

 System.out.println(new String(copyTo));
 System.out.println(copyTo);

Output:

caffein
caffein

I suspect it's just an oversight/bug in the documentation that new String is used. I filed a bug report with Oracle to get it fixed.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

yes you're right. System.out.println() first cast passed object to String by String.valueOf()‍‍ and this method calls toString‍‍‍‍‍‍‍‍ method of object. so you can print copyTo without new String().

  • This explanation is somewhat misleading, as `String.valueOf()` has also an overloaded variant for `char[]`. Try `System.out.println(String.valueOf(copyTo));` vs `System.out.println(copyTo.toString());` to see the difference. – Gyro Gearless Jan 26 '21 at 12:22
  • thank you @GyroGearless. you'r right. i missed that "copyTo" is array. should i delete this? – Ali Choopani Jan 26 '21 at 12:31
0

The new String() initializes the new object of the String with the same sequence of characters as the argument passed, in this case, the argument is copyTo.
Firstly, the required number of characters are getting copied into the array copyTo. And this array is passed to the function String. The String function will create a word from the array copyTo. And the keyword new is used to create a new instance(object) of the String as String is a class in Java.

You can do the same thing using System.out.println(copyTo); as well. This will only work for the array of the type char.

Ankush Chavan
  • 1,043
  • 1
  • 6
  • 20