1

Below is the code in which I have few doubts as in JAVA there is no pass by reference or pass by address. Then why is there change in output after calling of methods? And also want to know internal mechanism of JAVA after a method call?

class Language
{
    String languageID;
    String name;

    public Language(){}

    public Language(String id, String name)
    {
        this.languageID = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Language id-->" + languageID + " Name-->" + name;
    }
}

public class SwapDemo {
    public static void swap()
    {
        int a = 10, b= 5;
        System.out.println("Before Swap : A-"+a+" B-"+b);
        a = a+b;
        b = a-b;
        a = a-b;
        System.out.println("After Swap : A-"+a+" B-"+b);
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println("After Swap : A-"+a+" B-"+b);
    }

    public static void swap(Language e1, Language e2)
    {
        Language temp = new Language();
        temp = e1;
        e1 = e2;
        e2 = temp;
    }

    public static Language method(Language addit)
    {
        addit.name = "C++";
        addit = new Language();
        addit.name = "Harshal";
        return addit;
    }

    public static void main(String[] args) {
        Language l1 = new Language("1", "C");
        Language l2 = new Language("2", "Java");

        System.out.println("Before swap of employees");
        System.out.println("Language 1-----> "+l1.toString());
        System.out.println("Language 2-----> "+l2.toString());

        swap(l1, l2);
        System.out.println("After swap of employees");
        System.out.println("Language 1-----> "+l1.toString());
        System.out.println("Language 2-----> "+l2.toString());

        Language t = method(l1);
        System.out.println("Changing "+ l1.name);
        System.out.println("Changing "+ t.name);
    }
}
  • Actually the answer is already here : http://stackoverflow.com/questions/40480/is-java-pass-by-reference?rq=1 – Woody May 16 '13 at 10:29

3 Answers3

0

Java is pass by value. It passes copies of the references to methods. So this means that:

List<String> l = new ArrayList<String>();
someMethod(l);

Some method just got passed a copy of l - which is a reference to an ArrayList on the heap.

public void someMethod(List<String> aList) {
    aList.add("Foo");
}

someMethod can add things to that list, since the local reference aList points at the same object as "l" in the callers scope. However note that :

public void someMethod(List<String> aList) {
    aList = new LinkedList<String>();
}

has no effect on the caller, all this does is make the local varaible "aList" point at a different object on the heap. Remember that aList is a copy of the reference we called the method with, so they both referred to the same heap object, after reassignment they no longer do.

Woody
  • 7,578
  • 2
  • 21
  • 25
0

Figure

I'm Explaining the following lines which will clear your doubt. I assume you know a little beat about Heap and Stack.

        Language t = method(l1);
        System.out.println("Changing "+ l1.name);
        System.out.println("Changing "+ t.name);

Here when you are passing "l1" object to the method you are passing REFERENCE to the object. REFERENCE is a kind of handle that will be used to identify object in HEAP SPACE.

consider,

A a =new A();

here what a will be containing ? Obviously some address which points to the newly created object.

So when you are passing a OBJECT to the method you are passing a reference to the memory location where the object is residing. But inside method if you try to change the object to some other object as you have done in swap(l1,l2) method it will fail because you are changing value of the address, not the actual object.

Therefore, the statement addit.name="C++" will change the contents of the object which is residing at the memory location to which addit points. Therefore here original object has been modified. Hope this clears your first doubt.

Regarding your second doubt, a object is living in its scope. As soon as the method calls gets over all the variables allocated in that method are eligible to be garbage collected. But here what you are doing in, you are returning a reference to the object from the method. So object will not be garbage collected because it has valid references pointing to it. Therefore in the second output it prints the new value. Hope this helps to understand the output.

ATR
  • 2,160
  • 4
  • 22
  • 43
0

The premise of the question is flawed. You cannot "pass an object" in Java. The only types in Java are primitive types and reference types, and thus the only values are primitives and references. "Objects" are not values, and cannot be passed. Objects can only be manipulated through references that point to them. In your code, l1, l2, etc. are references.

newacct
  • 119,665
  • 29
  • 163
  • 224