• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array printing error

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic