2

I running to a situation and I am very confused. Please help me out. Let's say I have a code like this.

MyClass obj1 = null;

List<MyClass> testList = new ArrayList<MyClass>();

testList.add(obj1);//after this line of code, testList will have a "null" at first node

obj1 = new MyClass();//after this line of code, testList still have a "null"
                     //at first node...
                     //What I want is that testList's first node will become a new 
                     //MyClass object

Here is the steps in my understanding (probably incorrect...):

  1. obj1 stores the pointer that points to nothing...
  2. testList stores the pointer that points to the memory that holds the "new ArrayList()"
  3. testList add the pointer of obj1 in its collection.
  4. obj1 stores the pointer that points to a "new MyClass()"
  5. therefore, the obj1 in the testList should automatically points to the "new MyClass()"

Sorry I am new to programming... Any help is appreciated!

MPelletier
  • 16,256
  • 15
  • 86
  • 137
user2296188
  • 139
  • 8
  • Do the `new` call before adding the object to the list. Look up `java pass by value` to answer all your questions. – Sotirios Delimanolis Apr 18 '13 at 17:39
  • 1
    `testList.add(obj1);` // testList[0] and obj1 both reference the same object. `obj1 = new MyClass();` // Change obj1 to reference a new object. This will not cause all of the other references to the original object to be changed. – mbeckish Apr 18 '13 at 17:42
  • Thank you very much... I think I got it now... – user2296188 Apr 18 '13 at 17:44
  • The third step adds the VALUE of the pointer (er, "reference") to the list. Any modifications of the pointer itself after that point have no effect on the value stored in the list. (Though, of course, adding a null pointer to many types of composite objects is invalid.) – Hot Licks Apr 18 '13 at 17:54
  • Keep in mind that a "reference" (or "pointer") is just a (very large) integer whose value represents the storage location of something. When you pass a reference/pointer as a parameter, you're passing that numeric value, and that's what gets stored if, say, you're adding an object to a list. No link to the original reference variable is even passed, only the value. – Hot Licks Apr 18 '13 at 18:01

5 Answers5

2

Here is the explination why testList still have a "null" at first node after these piece of code

testList.add(obj1);//after this line of code, testList will have a "null" at first node

obj1 = new MyClass();//after this line of code, testList still have a "null"
                     //at first node...
                     //What I want is that testList's first node will become a new 
                     //MyClass object

Step 1

MyClass obj1 = null;

This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.

Step 2

List<MyClass> testList = new ArrayList<MyClass>();

A list is created testList which can hold objects of MyClass types

Step 3

testList.add(obj1);//after this line of code, testList will have a "null" at first node

testList first node will now refer to an null But not an MyClass Object.

step 4

obj1 = new MyClass();

Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable obj1.

So now how will the list gets updated it is still holding an null But not an MyClass Object.

So now if you want to make testList's first node to become a new MyClass object

Then write this lone of code after obj1 = new MyClass();

testList.set(0, obj1);

So now the full code after that would be

MyClass obj1 = null;

List<MyClass> testList = new ArrayList<MyClass>();

testList.add(obj1);//after this line of code, testList will have a "null" at first node

obj1 = new MyClass();

testList.set(0, obj1);

Thats all about your problem from my understanding.

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
1
MyClass obj1 = null;

A reference variable named obj1 of type MyClass refers to nothing(NULL).

List<MyClass> testList = new ArrayList<MyClass>();

We declare a reference variable called testList of type List and assign it to some newly created ArrayList object in heap.

testList.add(obj1);

List testList's first element is assigned the same reference which obj1 holds currently viz. NULL.

obj1 = new MyClass();

We created a new MyClass object in heap and assigned obj1 with its reference. But we haven't assigned any new reference to the List testList's first element, which was assigned the NULL reference, so it still points nowhere.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

When you add objects to ArrayList objects will not be added to the ArrayList but the pointers which point to the object will be added.

So if you do something like this:

Object obj1 = new Object() // [location: x1234]
list.add(obj1); // first index in list points to location x1234
obj1 = new Object(); // [location: x2345];

Now the array has pointer which is still pointing to the old location.

Same is the case with null. Though you are changing the link of obj1 to new location the array still points to old location which is null.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
threadfin
  • 116
  • 3
0
  1. obj1 points to nothing, the pointer is non existent
  2. you create a new list, testList points to that list
  3. you add nothing to the list, because your the point of obj1 is null therefor non existent
  4. your create new object, obj now has a valide pointer

Your list is still empty.

worcin
  • 129
  • 5
0

Java is strictly pass by value.

Much of your confusion comes from the fact that

testList.add(obj1); // makes you think that you've added a pointer to the list

and now if you modify the pointer as

obj1 = new MyClass(); // it would reflect at testList.get(0) as well.. NO!!

This would've been the case if Java were "pass by reference" but since it's not testList.add(obj1); actually translates to testList.add(null); copying the value and setting the first indexed position of the Array (backing this ArrayList) as null.

Java doesn't work like C and C++ work with pointers. It was one of its design goals to simplify things by EDIT: removing pointers, their arithmetic, multiple inheritance, operator overloading etc. just to name a few.

To make it more clear if you modify your program as follows:

MyClass obj1 = new MyClass("Object 1"); // modify the constructor to take an id
List<MyClass> testList = new ArrayList<MyClass>();
testList.add(obj1);
obj1 = new MyClass("Object 2");
System.out.println(testList.get(0).toString()); // implement toSting() to id MyClass

testList would contain "Object 1" not "Object 2".

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Actually, the vast majority of C/C++ parameters are value parameters as well. With C/C++ you have the *option* of passing reference parameters, but that's not the norm for simple C code, and only becomes common in C++ code that makes heavy use of more complex objects. (The ability to do pointer arithmetic has nothing to do with passing by reference vs value.) – Hot Licks Apr 18 '13 at 18:08
  • @HotLicks It was a general statement that Java doesn't support pointers, their arithmetic, multiple inheritance etc. etc. **to simplify things** as compared to C/C++. – Ravi K Thapliyal Apr 18 '13 at 18:33
  • Though that is a bit of revisionist history. Remember, Java was invented to program set-top boxes, and the "simplification" was just to produce a minimalist syntax/semantics to get that job done. The "philosophy of Java" was "discovered" later. Early versions of C were even simpler. – Hot Licks Apr 18 '13 at 18:43
  • I apologize if I've touched a nerve here.. but I have immense respect for all the languages. It's just that I'm not that good at C/C++ now! Peace :) – Ravi K Thapliyal Apr 18 '13 at 18:49