• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays question  RSS feed

 
Kasun Liyanage
Greenhorn
Posts: 23
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!



i cannot understand how they have put Arrays.toString() like that, without first creating an Arrays object. Are java.util' classes are always used that way? Please explain.

Thanks a lot.
 
Haina Minawa
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Liyanage wrote:

i cannot understand how they have put Arrays.toString() like that, without first creating an Arrays object. Are java.util' classes are always used that way? Please explain.



Here is the Java doc for Arrays.toString(Object[]) method:


public static String toString(Object[] a)

Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(), unless a is null, in which case "null" is returned.

Parameters:
a - the array whose string representation to return
Returns:
a string representation of a
 
Mohamed Sanaulla
Bartender
Posts: 3184
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you would have to see what String.split() method accomplishes and this would clear your doubt to some extent.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot create an object of the Arrays class.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Liyanage wrote:i cannot understand how they have put Arrays.toString() like that, without first creating an Arrays object. Are java.util' classes are always used that way? Please explain.


To address this specifically, the Arrays.toString() method is a static method, so it gets called without an instance. See the java.util.Arrays javadoc and Tutorial for more.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, it is kind of like the Math class. you don't instantiate it you just call its static methods. and yes there are several utilities classes that follow this pattern, they are a convenience.
 
Kasun Liyanage
Greenhorn
Posts: 23
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys. Gonna checkout more about classes with static methods. Appreciate it.

-Kasun
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!