File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array printing  error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array printing  error" Watch "Array printing  error" New topic
Author

Array printing error

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
There is a printing error in my code. I want to print my sorted array. When I use System.out.println(afterSort) the output is fine, but when I add some String in the System.out.print(" " + afterSort), it prints some weird chars. Any idea what this happens? Thank you for your replies.



Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

When I use System.out.println(afterSort) the output is fine, but when I add some String in the System.out.print(" " + afterSort), it prints some weird chars.


First, the chars are not weird. The output basically is telling you that it is a char array, at a particular hashcode.

The reason you are getting it is because there is no special handling of char arrays for string concats. The char array is simply converted to a string with the toString() method, which by default, converts it to the type at a hashcode address.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Originally posted by Henry Wong:
The reason you are getting it is because there is no special handling of char arrays for string concats. The char array is simply converted to a string with the toString() method, which by default, converts it to the type at a hashcode address.

Thanks a lot for your reply!

I've only started learning Java a few months ago. There's still soo much more to learn and discover! The hint you gave has pointed to the right direction.



The trick is to add 'String.valueOf(afterSort)' to convert it into a String.

Thanks again.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
to restate and review:

System.out is a PrintStream

PrintStream.print(x) is polymorphically overloaded: it has many implementations that take different parameter types (as x).

Your two examples were calling two different versions of it:
print(String)
and
print(Object)

The behavior of inline string concatenation and the print(Object) method are different. The former invokes toString(), the later String.valueOf(Object).
[ August 08, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Originally posted by Bill Shirley:
to restate and review:
Your two examples were calling two different versions of it:
print(String) and print(Object)

The behavior of inline string concatenation and the print(Object) method are different. The former invokes toString(), the later String.valueOf(Object).


Thanks a lot for the links! Your explanations are very clear.

There are many new Java stuffs to learn everyday...
Venkat Bobba
Greenhorn

Joined: May 02, 2008
Posts: 2
Hi,
I am relatively new to Java. Regarding the String concatenation with objects, I have read this in a Java book. During concatenation,if one operand is a String, the other operand gets converted to a String. For objects this means it calls String.valueOf(object) which in turn calls object.toString().
So System.out.println("After sort :" + afterSort); should call String.valueOf(afterSort) and that should call afterSort.toString().
So the result should be the same as System.out.println(afterSort).
Are arrays treated differently? Are they not objects?

Thanks
Venkat
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

With the String concatenation, it will regard afterSort as an Object. Therefore, String.valueOf(afterSort) will indeed call afterSort.toString().

When using only afterSort, it will not be treated as an Object but as a char[]. Therefore, the overloaded String.valueOf method will be called which will construct a new String using the char[]. That is then printed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Venkat Bobba
Greenhorn

Joined: May 02, 2008
Posts: 2
Oh, now I understand the difference. There is another method String.valueOf(char[] data) which I overlooked. Thanks much.

Venkat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array printing error