| Author |
Regarding Collections
|
Simrat Singh
Greenhorn
Joined: Mar 31, 2007
Posts: 6
|
|
I m not a profetional java programmer, I m a student. There is something in collections which is bothering me. The topic which is bothering me goes as follows In collections when we use any class to create a Datastructure, then we use its add method to add any new element. For example ArrayList al = new ArrayList(); al.add("simrat"); al.add(new Integer(1)); Now how wud ArrayList class wud come to know tht which object am i passing to it n how much space in memory is to be reserved 4 it in memory.
|
 |
Siddhartha Ghosh
Greenhorn
Joined: Jul 07, 2005
Posts: 16
|
|
The JVM decides on how much space is to be reserved in memory for creating an object when you actually create an object by the new keyword! For eg, This is when actual memory is allocated in the Heap for your Object. However, when you add an object to a collection by calling the add method, you are actually passing a reference to that object, ( Not the real object ) already lying around in the heap, and this reference is of a size known to the JVM. Thus, no matter what the real size of the object might be in the heap, the collection happily adds the reference of that object.
|
Siddhartha "Maverick" Ghosh <br />Sun Certified Java Programmer 1.4
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Simrat,
ArrayList al = new ArrayList(); al.add("simrat"); al.add(new Integer(1)); Now how wud ArrayList class wud come to know tht which object am i passing to it n how much space in memory is to be reserved 4 it in memory.
There is nothing like sizeof in Java unlike C, C++. We have something called length with arrays and size in case of collections which says how many items are there in the collection. Java comes with generics now and better you use parameterized collection object that is type safe as : Compiler now makes you assured that you are not going to add anything else but String and Integer to alist1 and alist2 accordingly. This is the typesafe way to work with java collections. So far your question is concerned, what you add to the list would be Object in your case and because every object in java is Object so if you add String it is casted to Object and if you add Integer it is casted to Object. No Problemo. And return type is also Object and you must cast it to the appropriate type otherwise ClassCastException is thrown at runtime; Try it with your code what you have posted: First line is ok but the second line will give you error becase the second object in you list is Integer and you are assigning it to String so ClassCastException. This is the trouble while you work with untypesafe collection; because you may not know, at what position what object is there; as in you case index 0 String and index 1 it is Integer; Do I miss something? Let me know! Thanks and Regards, cmbhatt
|
cmbhatt
|
 |
Simrat Singh
Greenhorn
Joined: Mar 31, 2007
Posts: 6
|
|
My dear friend i agree with you. but when we are writing the following code ArrayList al = new ArrayList(); al.add("simrat"); al.add(new Integer(1)); then what exaclty happening is this Object obj = new Integer(1); Now as Object is the super class of Integer, it can hav a refference to its subclass but using the superclass object we can only access methods and variables defined in the super class. so when we write System.out.println(al.get(1)); By using which method of Object class it is picking up the values present at index 1.
|
 |
Siddhartha Ghosh
Greenhorn
Joined: Jul 07, 2005
Posts: 16
|
|
My Dear Simrat, Unless you are using Generics, whenever you invoke the method You actually get back an object, nothing but a plain and simple object right? Now what happens is that when you are actually calling System.out.println(), your object gets converted into it's String equivalent, and this is where the magic happens and your question is answered! See what happens is that in order to convert your object to a string, the JVM internally invokes the toString() method that all Objects have. However, at runtime, due to dynamic binding the actual method that gets invoked is the overriden toString() method in the Integer class, and this method knows how to convert your integer to a nice looking string representation. Hence, you think that you are accessing the Integer, while in essense you only have an Object.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
My dear friend i agree with you. but when we are writing the following code ArrayList al = new ArrayList(); al.add("simrat"); al.add(new Integer(1)); then what exaclty happening is this Object obj = new Integer(1); Now as Object is the super class of Integer, it can hav a refference to its subclass but using the superclass object we can only access methods and variables defined in the super class. so when we write System.out.println(al.get(1)); By using which method of Object class it is picking up the values present at index 1.
Think about what you are asking! Where do you use the reference of the Object class to access the element of the collection. You used a1 and that is reference variable of ArrayList. So that is fine to call get on that. Thanks and Regards, cmbhatt
|
 |
 |
|
|
subject: Regarding Collections
|
|
|