Question

I have read the forum and gone through some links and found that Java is all pass by value and i think i do understand that to how the references thing work but not how primitives work. It is said that for both primitives and object, pass by value works in the same manner.

I created a sample program

public class BirthDate {
int lear;
int year; // Instance variable
public static void main(String [] args) {
    BirthDate bd = new BirthDate();
    bd.lear=200;
    bd.year=300;
    int a=56;
    bd.showYear(a,bd);
    System.out.println(a);
    System.out.println(bd.lear);
}
public void showYear(int a, BirthDate bd) {
        a=59;
        System.out.println(bd.lear);
        bd.lear=400;
        System.out.println(bd.lear);
        System.out.println(a);
}

}

Output of the above program is 200 400 59 56 400

Now, if the bits were passed in case of both primitives and reference then why the change in reference value in showYear method reflected the change in original bd object (value changed to 400) and not a primitive data type i.e. a.

Can anyone please suggest?

Was it helpful?

Solution

You pass the reference of the BirthDate by value, so your method refers to the same BirthDate as in your main method.

However you then change the member of that object (lear) from 200 to 400.

Because you've passed the reference to the original object, it's not been copied and you're dealing with the same object at all times.

OTHER TIPS

When you are passing BirthDate bd you are passing a reference to this object.

If in your show year you will do:

public void showYear(int a, BirthDate bd) {
        bd = new BirthdayDate();
}

This bd = new BirthdayDate(); is equivalent to a= 59; in the nature. This is important to understand.

You are not going to change the bd outside of it- you are just reassigning different reference.

However your:

bd.lear=400;

Accesses the object that is pointed by this reference and changes its value. This way you are editing an object. This can also be seen as just an assignment of a primitive- but it happens inside the place that is pointed by bd.

They are all pass by value. For reference, object reference is passed by value. When you say-

bd.showYear(a,bd);

You are passing the address of the bd to showYear method. Let's break it down-

BirthDate bd = new BirthDate(); // Let's say a new object bd is created at address 10.

I changed the parameter bd to bd2 below so we wouldn't mistake it with the Brithday bd above

public void showYear(int a, BirthDate bd2) { // Parameter bd2 is pointing to address 10       

    bd2.lear=400; // bd2 followed the address 10 and changed bd's leap property. This doesn't change the fact that bd still is at address 10.
}

You can change in a method the fields of the Object, which the reference refers to, but you cannot reassign it to another Object. Primitives are though "immutable" and you cannot change their value in a method. That is the way java parameters behave in a method.

In both cases you are passing value only. U pass the value of Object ID into new reference Variable bd which is local in that case. So when u call

     bd.lear=400; 

it changes the value of lear variable of the Object. Now when function finishes , stack is destroyed. So is reference variable bd. But since main function bd contains id of the object , object is not destroyed.

Coming to primitive type , u changed the value of local variable a of showYear function in this

   a=59;

it prints it and is destroyed with stack of the object. But main functions local variable is still present with value 56.

Make a part of Object and then try

  bd.a=59; 

you will see value changing. Read about Local variables, instance variables and class variables..!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top