Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

char[] printing

 
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code block doesn't output what I expect.



It outputs:


I was expecting:


Any ideas why I'm getting the String value for the last two but not when I output "String password: " + password.toCharArray() ? (I was expecting to get the [C@23223dd8 version for the last two.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The System.out.println() method is overloaded to give different output for a char[]. It prints the individual chars rather than calling myArray.toString(). The gory details are to be found in the documentation for PrintStream#print(char[]).
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it wrong that I wished toString() in Java outputted every object as json?  Aka... (if using Google's Gson).. new Gson.toJson(object)?  

Specifically the default implementation, not where they've been overwritten by a class.
 
Jeanne Boyarsky
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The System.out.println() method is overloaded to give different output for a char[]. It prints the individual chars rather than calling myArray.toString(). The gory details are to be found in the documentation for PrintStream#print(char[]).


Thanks Campbell.
 
Jeanne Boyarsky
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:Is it wrong that I wished toString() in Java outputted every object as json?  Aka... (if using Google's Gson).. new Gson.toJson(object)?  

Specifically the default implementation, not where they've been overwritten by a class.


Yes! I'd far rather things fail closed. I wouldn't want all my fields to get dumped with toString() by default.
 
Scott Selikoff
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True but what value is Object28274772918374 anyway?
 
Jeanne Boyarsky
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:True but what value is Object28274772918374 anyway?


Not a data leakl
 
Get me the mayor's office! I need to tell him about this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic