DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005
    Posts
    15

    A simple ambiguity regarding the thing that in java everything is passed by copy??

    Hi,

    I have read everywhere 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

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I don't know what you've been reading. In Java, all parameters are passed by reference EXCEPT the primitive data types (int, char, byte, etc.). All non-primitive data type memory reference is indirect - the "data" for the object is stored "on the heap" while the variable points to a location "on the stack" which points to the object on the heap. What you observed is correct.

  3. #3
    Join Date
    Mar 2004
    Posts
    635
    You're both sort of right. What java technically passes is a copy of the memory address (reference) of the object.

    So any changes you make on an object inside a function are shown outside of it, however, assigning a new object to that reference wont be seen.

    the variable "object" will not change after this method call because you're assigning a new object to the copied reference. (i'm not very good at explaining this am i?)
    Code:
    MyObject object = new MyObject("13");
    public void stuff(MyObject obj)
    {
        obj = new MyObject("42");
    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Yes - I have to correct what I said before. The Java method of passing variables is "pass by VALUE" - where the value of all but primitive data types is a memory location (a reference), so the passing of an object is "as if it were" "pass by reference".
    Last edited by nspils; 06-06-2006 at 09:46 AM.

Similar Threads

  1. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  2. Simple tool to help you create Java class
    By zfqjava in forum java.announcements
    Replies: 2
    Last Post: 01-08-2002, 04:26 AM
  3. JAVADOTNET !!!
    By Dharmesh in forum .NET
    Replies: 4
    Last Post: 10-01-2001, 03:47 PM
  4. Simple question: MouseOver Java Code for Netscape
    By Craig in forum Architecture and Design
    Replies: 1
    Last Post: 04-22-2001, 01:04 PM
  5. Simple Java DOM question
    By Lenin in forum XML
    Replies: 0
    Last Post: 03-21-2001, 04:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links