Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualfrob

Posted 08 November 2012 - 11:36 PM

In java all objects are basically pointers. You allocate the memory with new, and the memory is freed by the garbage collector.

Foo myFoo = new Foo();

myFoo is called an object, but is actually a pointer to an object.

Java passes objects as references passed by value.

You can modify the contents of the object and they will remain modified when you return. For example:

TestFunc ( Foo bar ) //1
{
bar.setName ("replaced name"); //2
bar = new Foo(); //3
bar.setName("new object"); //4
}

Foo myFoo = new Foo(); //5
myFoo.setName("original"); //6
TestFunc(myFoo); //7
myFoo.name.equals("replaced name") // 8


Starting with line 5, you create a new object. This allocates the object, setting the myFoo pointer. Let's say it creates an object at memory address 32. Line 6 sets the name string of the object at memory address 32. Line 7 calls the function; the pointer is passed by value --- that is, you can modify the thing being pointed to (the object at address 32), but you do not modify the pointer, it will still point to memory address 32. Line 1 makes a new pointer, called bar, that is initialized to point to address 32, which is the pass-by-value. Line 2 modifies the name of the object stored at memory address 32. Then line 3 creates a new object, say at memory address 70, and points the new bar pointer to the new memory address. Line 4 modifies the name of the object stored at memory address 70. When we return to line 8, since myFoo was passed by value, it still points to memory address 32, so the object was modified within the function, but it was not replaced by the function.

To modify your integers you cannot pass them by value and have those values change, but you can create an object that contains three integers and modify those members from within the function.

#2frob

Posted 08 November 2012 - 11:33 PM

In java all objects are basically pointers. You allocate the memory with new, and the memory is freed by the garbage collector.

Foo myFoo = new Foo();

myFoo is called an object, but is actually a pointer to an object.

Java passes objects as references passed by value.

You can modify the contents of the object and they will remain modified when you return. For example:

TestFunc ( Foo bar ) //1
{
bar.setName ("replaced name"); //2
bar = new Foo(); //3
bar.setName("new object"); //4
}

Foo myFoo = new Foo(); //5
myFoo.setName("original"); //6
TestFunc(myFoo); //7
myFoo.name.equals("replaced name") // 8


Starting with line 5, you create a new object. This allocates the object, setting the myFoo pointer. Let's say it creates an object at memory address 32. Line 6 sets the name string of the object at memory address 32. Line 7 calls the function; the pointer is passed by value --- that is, you can modify the thing being pointed to (the object at address 32), but you do not modify the pointer, it will still point to memory address 32. Line 1 makes a new pointer, called bar, that is initialized to point to address 32, which is the pass-by-value. Line 2 modifies the name of the object stored at memory address 32. Then line 3 creates a new object, say at memory address 70, and points the new bar pointer to the new memory address. Line 4 modifies the name of the object stored at memory address 70. When we return to line 8, since myFoo was passed by value, it still points to memory address 32, so the object was modified within the function, but it was not replaced by the function.

#1frob

Posted 08 November 2012 - 11:31 PM

In java all objects are basically pointers. You allocate the memory with new, and the memory is freed by the garbage collector.

Foo myFoo = new Foo();

myFoo is called an object, but is actually a pointer to an object.

Java passes objects as references passed by value.

You can modify the contents of the object and they will remain modified when you return. For example:

TestFunc ( Foo bar ) //1
{
bar.setName ("replaced name"); //2
bar = new Foo(); //3
bar.setName("new object"); //4
}

Foo myFoo = new Foo(); //5
myFoo.setName("original"); //6
TestFunc(myFoo); //7
myFoo.name.equals("replaced name") // 8


Starting with line 5, you create a new object. This allocates the object, setting the myFoo pointer. Let's say it creates an object at memory address 32. Line 6 sets the name string of the object at line 32. Line 7 calls the function. The pointer is passed by value --- that is, you can modify the thing being pointed to (the object at address 32), but you do not modify the pointer, it will still point to memory address 32. Line 1 makes a new pointer, called bar, that is initialized to point to address 32, which is the pass-by-value. Line 2 modifies the name of the object stored at memory address 32. Then line 3 creates a new object, say at memory address 70, and points the new bar pointer to the new memory address. Line 4 modifies the name of the object stored at memory address 70. When we return to line 8, since myFoo was passed by value, it still points to memory address 32, so the object was modified within the function, but it was not replaced by the function.

PARTNERS