quick ArrayList question

is it me or does the get() method return the address of an object instead of a copy of an object

like for example

public class z 
{
	public static void main(String[] args)
	{
		
		ArrayList list = new ArrayList();
		list.add(new Bob());
		
		System.out.println("Before Change");
		
		System.out.println( ( (Bob)list.get(0) ).getX() );
		
		Bob testObject = (Bob)list.get(0);
		testObject.a();
		
		System.out.println("After Change");
		System.out.println( ( (Bob)list.get(0) ).getX() );
	}
}
class Bob
{
	private int x =0;
	public Bob()
	{
		
	}
	public void a()
	{
		x++;
	}
	public int getX()
	{
		return x;
	}
}

Considering this code, if testObject is only a copy of whats in get(0), then if i do anything to testObject, wouldn’t the element in get(0) stay the same??? but when i execute the code, the element at get(0) becomes changed when I didn’t use any set methods at all.

Another example with iterator.




public class test
{
	ArrayList a = new ArrayList();
	public test()
	{

		a.add(new bob());
	}	
	public ArrayList getList()
	{
		return a;
	}
}
class Bobtester
{
	public static void main(String[] args)
	{
		test t = new test();
		ArrayList b = t.getList();
		Iterator bi = b.iterator();
		while(bi.hasNext())
		{
			bob c = (bob)bi.next();
		}
		System.out.println(b);
		System.out.println(t.getList());
	}
}
class bob
{
	int x=0;
	public void at()
	{
		x++;
	}
	public String toString()
	{
		return Integer.toString(x);
	}
}

Basically ArrayList b is suppose to be a copy of ArrayList a, but when I remove each element from ArrayList b, each element is also removed from ArrayList a…why is that???

Welcome to object oriented programming. Enjoy your stay.

Java doesn’t have pointers, though it is “okay” (sort of) to think of them as such if it helps you understand. Java has references instead. The easiest way to think of a reference is like this:

  • Imagine you have a box full of stuff. Light bulbs, toys, spare parts, tools, whatever. The box only changes when you add something into it.
  • Imagine now that you label each item in the box with a number. That number tells you which object you’re referring to. You can always write in all your record books that you need part XYZ for some job. There’s only one part though. You have to add another one if you need more.
  • Imagine that when you have no more use for a part, you rip the label off. Your wife (being very tidy and hating all the junk you keep around) checks your box every once in awhile to see if there’s any stuff without a label. If she finds anything, she throws it out. (Men and their junk, eh?)

The labels are like references. They allow you to always keep track of the specific instance of the object. The objects in the box are just like your Java objects. Your wife in this scenario, is the garbage collector. And finally, the box is an object too, so it just happens to be an ArrayList.

Make sense? :slight_smile:

lol thats funny cause my Comp Sci teacher is telling me that get() returns only a copy.

but i dont understand something

in my previous 2 examples, the original list gets changed

but when i did another test, this time with Strings, the original list did NOT get changed, why is that?

ArrayList b = new ArrayList();
		b.add("hey");
		String tester = (String)b.get(0);
		tester = tester.substring(0,1);
		System.out.println((String)b.get(0));

The list didn’t change. Only the object inside the list. Remember that.

[quote]but when i did another test, this time with Strings, the original list did NOT get changed, why is that?
[/quote]
Read the JavaDocs for substring. It specifically states, “Returns a new string that is a substring of this string.” So you now have two objects. One that is the original string, and one that is a single letter.

Consider that “tester” is just a reference. What happens when you reassign the reference?

P.S. Always remember that Java Strings are immutable. You can’t change them. Any method called on the string will always return a new string or data about the string. See the JavaDocs for more info.

Copy of the reference maybe. Making copies in Java is usually explicit… you construct a new object yourself or call .clone(). Unless a library method is documented as making a copy, it probably doesn’t and simple uses a reference to the original object. Sometimes it is the same for objects that you pass in to methods. e.g. be careful when doing someObject.setSomeProperty(myX); myX.myField = somethingElse, because you might alter the property held in someObject when you don’t intend to. One of the reasons that Java uses immutable strings and immutable wrapper classes for all the primative types is to prevent that sort of problem.

ok thanks i kind of understand

Damn, JB. you just said everything I was going to 8)