Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array printing error

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Venkat Bobba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, now I understand the difference. There is another method String.valueOf(char[] data) which I overlooked. Thanks much.

Venkat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic