-3

I have the following code, there is a class P as follows:

class P {
  char c;

  P (char c){
    this.c = c;
  }
}

Then in the program there is:

P p = new P('a');

void m1 (P p, char z){
  p = new P(z); 
}

m1(p, 'd');

My question is after method m1 is executed, then would my initial object p (to which has 'a'), will change its reference and will point to another p, which have whatever z holds. Or will I have two objects p, one which has 'a' and the other z (whatever z holds). And if I have two object, why would that be? I mean I am really confused… m1(p, 'd'); takes my p, which I initialized at the beginning and it should now make it point to another object?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3710255
  • 53
  • 1
  • 6
  • You can answer the first bit by just trying it. – Oliver Charlesworth Jun 06 '14 at 07:03
  • Also, why is it confusing? new creates a new object; if you invoke it twice you'll get two objects. – Oliver Charlesworth Jun 06 '14 at 07:04
  • 4
    as java is pass by value, even though the `p` is being new'd in the `m1` constructor, when it returns where p is pointing to will not have changed. – Scary Wombat Jun 06 '14 at 07:04
  • -1 Your second code fragment is not valid. You cannot call `m1(p, 'd');` without being in a method. – Duncan Jones Jun 06 '14 at 07:04
  • Your question is quite confusing to me, what I can say in java is for same object you can have multiple reference, just a mere assignment results in pointing to another reference. – Bilbo Baggins Jun 06 '14 at 07:05
  • Yes, but i don't understand why, i see i have 2 objects, but why is that happening? What happened when i use "new" to an already exciting object? – user3710255 Jun 06 '14 at 07:05
  • It is confusing because m1 takes in this case an existing object and then it makes a new one.. then why take an existing one as parameter in the method anyway..here is my confusion – user3710255 Jun 06 '14 at 07:07
  • Read [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) question – chancea Jun 06 '14 at 07:11
  • The method parameter is pointless, yes - but that doesn't change how Java handles it. You should also differentiate between variables, references, and objects. There's no concept of "use new to an already existing object". You're creating a new object, then assigning the resulting reference to a *variable* which previously referred to a different object - that's all. – Jon Skeet Jun 06 '14 at 07:11
  • So the variable will hold the reference to that object, in other words, the variable holds the memory location where the values are stored? Im i correct? – user3710255 Jun 06 '14 at 07:19
  • Why you guys vote the question negative? i mean its from programming exam and i see already few ppl gave wrong answer so can't be such a bad question after all, right::))?? – user3710255 Jun 06 '14 at 07:31

2 Answers2

1

Bear in mind that Java is pass-by-value. So your m1 method receives a copy of the value of p, but it cannot change the outer p itself.

void m1 (P p, char z){
  p = new P(z); // this only changes the method argument 'p', not the outer 'p'
}

void someOtherMethod()
  P p = new P('a');
  m1(p, 'z');
  // p has not changed
}

Inside the m1 method, you briefly create a new P object. As soon as your method finishes, that object is eligible for garbage collection as there are no live references to it.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

Java is JUST PASS-BY-VALUE.

In your case, imagine we got a class TV:

public class Tv {

       boolean on;
       int channel;
       String model;

       Tv (String model) {
           this.model = model;
       }

       public void turnOn(){
           this.on = true;
       }
    }

so now we order a Tv on amazon - have patience and wait 4-5 Days. meanwhile we buy JUST a remote Control for our Tv. Since there is no Tv there to control,we can play with our remote control, but nothing will happen. In Java you would say "create a reference rcTv, which refers to a Tv.

   Tv rcTv;  // our Tv remote control

still there is no Tv there to control. Now door is knocking, Yippee! our Tv is there. In java you say you have now an instance of Tv.

    new Tv(loewe)  // create an instance of an TV-Object

but what is the purpose of a Tv if you cannot control it (turn it on/off...)!!! Now we have to assign our remote control to our tv. in java you say reffering to an instance of an object

   rcTv = new Tv(loewe); // make rcTv to refer to our Tv-Object

you can even have another remote control

   Tv rcTv2   // new Remote control (reference)

an assign it (copy) the value of our previous remote control

   rcTv2 = rcTv;  // make rcTv2 to control the same Tv as rcTv

now both of them are referring to the same Tv model Loewe. We write a method to change the model of a Tv.

Tv changeModel (Tv tvarg, String model){
  return tvarg = new Tv(model); 
}

create third remote control referring to the same tv like the two others

Tv rcTv3 = rcTv;    // create another Tv remote-control

now you can cotnrol your Loewe-Tv with all three RCs. change the model of the last RC.

rcTV3 = changeModel (rcTv, "samsung") 

you could assume after this call rcTv will refer to a new Tv-Instance (samsung).

BUT as mentioned above JAVA -> JUST PASS BY-VALUE The point is you gave the method a reference (for c/c++-Folk a pointer) which can referring to an TV-Object.

as long as JAVA IS JUST PASS BY VALUES, your reference (rcTV) is copied into the method argument (tvarg) (read cloning your rcTv to tvarg). Now both of them will refer to the same Tv -> Loewe

But after

tvarg = new Tv(samsung);

you have created a new TV-object and simultaneously you force tvarg now to refer to that Tv-object and return it back. So after

rcTV3 = changeModel (rcTv, "samsung") 

your rcTv will still refer to Tv-Loewe and your rcTv3 will refer to Tv-Samsung.

regards

arash javanmard
  • 1,362
  • 2
  • 17
  • 37