• 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

Questions about char arrays

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can anyone explain the following to me?


When i run these three strings i get the following outputs:

[Ljava.lang.String;@c2f0bd7
abc
[I@64b25680

In other words, why does char[] work but not the others? I realize you should use arrays.toString or print the arrays out in a loop, but there seems to be some intricate difference here between char and other arrays i do not grasp. Also, if i add something to sysout, e.g. System.out.println(testChar + "\t"); it does not work either. Why?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of this.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link, but it doesnt really explain why this works with char arrays but not any others? Why is char[] special? How is it special? Neither is there anything about why syntax

System.out.println(testChar + "\t");

would not work?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Gibbins wrote:Thanks for the link, but it doesnt really explain why this works with char arrays but not any others? Why is char[] special? How is it special? Neither is there anything about why syntax



Basically, the link is saying that the PrintStream class, which is what the System.out instance is, has an overloaded method for char arrays. I guess the designers wanted the capability to treat char arrays like strings -- just like in the C language.

Tom Gibbins wrote:
System.out.println(testChar + "\t");

would not work?



That doesn't work as you expected because the concatenation does a toString() in order to do the concat. And the toString() method for char arrays doesn't return the string that you want.

Henry
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is related to the concept of overloading methods.
There are several println methods that take different types as arguments.
There is println(Object), there is println(String) and there is println(char[]).
If you call an overloaded method, always the most specific one is called.
If you pass String, println(String) will be called. Not println(Object).
The same is true for char[].
There is nothing special about char[]. You can see the difference in behaviour because there exists a method that takes char[] as an argument.
There is no separate method for String[] so in this case the most specific method will be println(Object).
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic