• 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

why it does print "[ ]" for the ArrayList where as it is some memory value for array?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following code.If we try to print just "ar" it returns a value like "[Ljava.lang.reflect.Array;@10b62c9" where as if we try to print ArrayList "li" it prints "[]".

I know the basic difference between the Array and ArrayList i.e the elemnts they hold, their size and their manipulations.

import java.util.*;
import java.lang.reflect.*;
class Example
{
public static void main(String args[])
{
Array ar[]=new Array[5];
System.out.println(ar);
List li=new ArrayList();
System.out.println(li);
}
}
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you print out something, it automatically invokes the toString() method on the object.

In the array's case, the toString() method only returns a representation of the array's memory address.

In the ArrayList class, however, the toString() method is overridden to return the contents of the list.



(edit: overwritten -> overridden )
[ October 04, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly and Since in your code nothing added to ArrayList object it prints "[]". try it by adding some values to the ArrayList and print it and see the difference.
 
I like tacos! And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic