This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays.toString() returns what kind of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays.toString() returns what kind of String" Watch "Arrays.toString() returns what kind of String" New topic
Author

Arrays.toString() returns what kind of String

Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61



Here output at line 6 is [4,5,6,7,8,9] .What kind of String is this??
Why exception arises at next line.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

Abhishek KumarSoni wrote:

Here output at line 6 is [4,5,6,7,8,9] .What kind of String is this??
Why exception arises at next line.



As you can tell, the Arrays.toString() method returns a formatted string that represents the array. It is surrounded by square brackets, and the members of the array are separated by commas.

And of course, it generates an exception because "[4,5,6,7,8,9]" can't be parsed. The Integer.parseInt() method expects a single integer only.

Henry


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

Joined: Nov 05, 2013
Posts: 61

According to me this function is not properly designed and output is useless outpur should be 456789 not [4,5,6,7,8,9].I could use this in parseInt function or everywhere a string object is required
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Abhishek KumarSoni wrote:According to me this function is not properly designed and output is useless outpur should be 456789 not [4,5,6,7,8,9].I could use this in parseInt function or everywhere a string object is required

The toString method for all array types outputs the contents in the same way - all the elements separated by commas and the whole thing enclosed by square brackets.
This is a good design because it produces consistent results for all array types and it is obvious the output was generated from a collection of some sort.
If you want the output in a different format then you need to write your own method.


Joanne
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays.toString() returns what kind of String
 
Similar Threads
doubt in some programs and question in java
array index in java
Integer
What would be the output of following piece of code and why?
split() method