Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.toString() returns what kind of String

 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Chrome Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Chrome Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic