0

Still in the phase of learning oop concepts in Java. In the concurrency example available in oracle tutorial here ((please bear with me and have a quick look at it, it is easy)), producerconsumerexample class triggers the two threads of producer and consumer which both should exchange data through the Drop object drop. And within this object they are supposed to wait for/get notified by each other.

public class ProducerConsumerExample {
    public static void main(String[] args) {
        Drop drop = new Drop();
        (new Thread(new Producer(drop))).start();
        (new Thread(new Consumer(drop))).start();
    }
}

What I do not understand is that the drop object is passed to each thread by the producerconsumerexample class, which gives us two new local Drop objects, also named drop, one in each of the two threads. To my understanding, this is because passing in Java is by value, not reference. Thus, each thread now has its own version of the Drop object, so how come they are still supposed to share data through the same original Drop object, since each thread has its own version ?!

Somebody please help, l would really really really appreciate it.

xyz
  • 5,228
  • 2
  • 26
  • 35
  • https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Mat Jun 25 '17 at 18:15
  • objects are passed by reference. – Alexei Kaigorodov Jun 25 '17 at 18:57
  • @Alexei and by value for primitives? –  Jun 25 '17 at 23:55
  • @basheer yes. Strictly speaking, references are also values, so you can hear "everything in java is passed by value". Take into account that objects themselves are never considered as variables (unlike C++), has no names and so cannot be passed as parameters at all - only references can. – Alexei Kaigorodov Jun 26 '17 at 06:57

2 Answers2

0

if you talk about this example , and drop variable - it's object , in java all objects pass by reference not by value. Producer and Consumer work with same object. For primitives pass by value

public class ProducerConsumerExample {
    public static void main(String[] args) {
        Drop drop = new Drop();
        (new Thread(new Producer(drop))).start();
        (new Thread(new Consumer(drop))).start();
    }
}
xyz
  • 5,228
  • 2
  • 26
  • 35
0

It's bit different. As you said, Java is pass by value and not by reference but when we pass the value of an object, we are passing the reference to it.

At the beginning it can be a misunderstanding because they decided to call the location of an object a "reference".

So Procuder and Consumer save the same reference, so they are working on the same object.

For example:

class Foo {
     Bar x;
     public Foo(Bar v) { this.x = v; }
     public String getString() { return x.y; }
}
class Bar {
     String y;
     public Bar() {}
}
class Test {
     public static void main(String[] args) {
           Bar x1 = new Bar();
           x1.y = "Test1";
           Foo x2 = new Foo(x1);

           /* the method getString() returns "Test1" */
           System.out.println(x2.getString());

           /* If I change x1.y, x2.getString() returns a different element */
           x1.y = "Test 2";
           System.out.println(x2.getString());

     }
}
cirosomma
  • 71
  • 7