Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2006, 11:31 PM   #1 (permalink)
j.gohel
Code Monkey
 
Join Date: Apr 2005
Posts: 68
j.gohel is on a distinguished road
A simple ambiguity regarding the thing that in java everything is passed by copy??

Hi,

I have read that java passes-by-copy everything.But the behaviour of the following code i am not able to understand:


Code:
import java.util.*;

public class  Test1
{
	public static void main(String[] args) 
	{
		ArrayList list  = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
		func(list);
		System.out.println("Arr1 ---> " + list);

	}

	static void func(ArrayList arr)
	{
		arr.add("4");
		arr.add("5");
		arr.add("6");
		System.out.println("Arr2 --> " + arr);

	}
}

It o/p is:

C:\JIGNESH_GOHEL\JigneshG\JavaProgs>java Test1
Arr2 --> [1, 2, 3, 4, 5, 6]
Arr1 ---> [1, 2, 3, 4, 5, 6]

But it should be somewhat like this as per my understanding(correct me if am wrong)

Arr2 --> [4, 5, 6]
Arr1 ---> [1, 2, 3]

So can i get the explaination why this is happening??
It seems the arraylist object is passed by reference
j.gohel is offline   Reply With Quote
Old 06-05-2006, 03:22 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
That's a very good question. In Java, *everything* is pass-by-value (or pass-by-copy). The misconception comes from not understanding exactly what is being passed.

In Java, if you pass a primitive to a method, you pass the value as opposed to the actual reference to the variable. If you pass an Object, you pass the value of the *reference* (as opposed to the actual reference). This is a very confusing point, and regularly confuses programming experts.

Consider this code:

Code:
public class test{

  static public void main(String[] args){
     int a = 99;
     System.out.println(a);
     foo(a);
     System.out.println(a);
  }

   static public void foo(int a){
      System.out.println(a);
      a = 10;
      System.out.println(a);
   }
}
Now, the output would be:
Quote:
99
99
10
99
I trust you understand why.

Now, let's look at this second example:

Code:
public class test{

  static public void main(String[] args){
     String a = "Hello";
     System.out.println(a);
     foo(a);
     System.out.println(a);
  }

   static public void foo(String a){
      System.out.println(a);
      a = "World";
      System.out.println(a);
   }
}
The output would be:

Quote:
Hello
Hello
World
Hello
This is how you expected it to work, yes?

What you did in your example was actually perform operations on the ArrayList. You will find you can modify an object passed to a method to your hearts content, but you cannot modify the reference, because you are only working on a *copy* of the reference, not the *actual* reference.

Consider it this way - I create object "foo". I then pass a copy of the memory reference to a method. That method now knows where "foo" is stored in memory. I can perform all sorts of operations on that object. But when I exit the method, "foo" still references that object. I cannot destroy or manipulate that reference. It retains the changes I made to the *Object*, but doesn't reflect any changes I made to the *reference*.

Go ahead, you'll find that you cannot make that ArrayList you manipulated equal a new ArrayList from within the method. If you set it equal to a new ArrayList, when you exit the method, you'll find that "list" still equals the old ArrayList.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembelly code register addressing toblerone Assembly 5 05-22-2006 02:53 AM
Java Resources Belisarius Java 0 03-28-2005 02:03 PM


All times are GMT -8. The time now is 10:27 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting