Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

In this code why a new String object is created, than not using println(copyTo)?  RSS feed

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this code why a new String object is created, than not using println(copyTo)?.What is the difference?

Varuna
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

println() method which is a memebr of PrintStream class can take both char array as well as String object as a parameter. So whether you use char array (in this case copyTo) or String object the output is same. The only difference is the type of parameter passed to the println method.

Gautam.
 
Varuna Seneviratna
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam!

What was in my mind was what made anybody without straightly using copyTo in the println(), create a String object.This was from the Sun java Tutorials.


Varuna
 
Marshal
Posts: 59681
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real problem is that arrays don't have overridden toString() methods. Have a look at the Object class, Java Language Specification about arrays, and the )]String class constructor.

Now work out what that Java Tutorial method would print if you didn't create the new String! Try it with
System.out.println(new String(copyTo));
and
System.out.println(copyTo);
The idea of that example is to show you how the arraycopy method works, so they miss out the bit about new String!
 
Campbell Ritchie
Marshal
Posts: 59681
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If one of my links doesn't work, copy and paste this:

http://java.sun.com/javase/6/docs/api/java/lang/String.html#String(char[])
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it would print the same thing. There's an overloaded version of println() that accepts a char[] argument and prints the characters as if they were a String.

The reason the code makes a copy of some -- not all! -- of the characters is because it is demonstrating how the System.arraycopy() method is used.

The reason that the programmer creates the new String is harder to imagine. The overloaded println(char[]) has been present since the very first JDK release. Perhaps the coder was simply new to Java? Nobody's perfect, even Sun Micro employees.
 
Campbell Ritchie
Marshal
Posts: 59681
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean; I had expected to get the [C@ab123456 format! It is a bit pointless creating a new String object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!