0

I read a few articles and similar question in Stackoverflow; however, I did not quite get an answer to my question. Here is the code:

public class CoinFlipping {

    Random random = new Random();

    Boolean head = null;

    public void flip(Boolean b){
        b = random.nextBoolean();
    //      head = b; 
    }

    public static void main(String [] args){
        CoinFlipping cf = new CoinFlipping();
        cf.flip(cf.head);
        System.out.println("Head: "+cf.head);
    }

    }

I refer to this arcticle: Is Java "pass-by-reference" or "pass-by-value"? I can understand why that piece of code behaves as it does. When we call new dog() we basically create a new Dog object. I get it. However, the example that I provided seems to be confusing a bit. I am not creating an object in this case (aren't I?). I am changing a value. Then why in this case when I print results, I get null and not true or false?

11
  • b = random.nextBoolean(); creates a new reference, so modifies the value of b, not the value referenced by b. Commented Jul 17, 2014 at 17:18
  • @AntonH: b does not "reference a value." b has a value which is a reference to an object (or a null reference). The assignment statement gives b a new value (i.e., a reference to a new object). Commented Jul 17, 2014 at 17:23
  • @jameslarge I was trying to simplify, so that OP would see what was wrong. But too much, apparently. Commented Jul 17, 2014 at 17:28
  • there are answers on the linked question that directly address this. see stackoverflow.com/a/40507/217324 closing this as a duplicate. Commented Jul 17, 2014 at 17:29
  • @AntonH Hello, so am I getting this right? When I pass any objects to methods, following happens: It looks up the value of the object through the reference, copies it and this copy is assigned to the local var (argument)? In this case I have another question. Is there any ways to pass a reference to method, so that I could take this reference, access the object and change it internal state? Is there a way to actually have a reference to an original object but not to copy inside of the method? Commented Jul 17, 2014 at 17:45

2 Answers 2

2

The parameter b is passed by value, even though b is itself a reference. Thus, when you call flip, the flip method treats b as a local variable that is a copy of what you pass into it. Changing this copy doesn't change cf.head.

If b were a reference to a mutable object, you could change the object b refers to, and that change would be seen by the rest of the program after flip returns. But a change to b itself is a change to a copy, and any such change will not be seen by the rest of the program. (Boolean is not mutable.)

Sign up to request clarification or add additional context in comments.

Comments

1

To answer your question:

Then why in this case when I print results, I get null and not true or false?

I have created a step-by-step walktrough of your code to show you the pointers (references) of the specific variables and answer why it prints out null in the end:

1. Step: New CoinfFlipping object is created with default field values

CoinFlipping cf = new CoinFlipping();

Inside the CoinFlipping class:

Random random = new Random();
Boolean head = null;

cf ---- points to ----> CoinFlipping object
cf.random ---- points to ----> Random object
cf.head ---- points to ----> null


2. Step: Calling the flip method with cf.head as parameter on the cf object

cf.flip(cf.head);

When evoking this method, a variable b is created inside the scope of the flip method. The pointer (reference) from cf.head is copied into the variable b. So cf.head and b point then at the same thing.

public void flip(Boolean b){

cf ---- points to ----> CoinFlipping object
cf.random ---- points to ----> Random object
cf.head ---- points to ----> null
b ---- points to ----> null


3. Step: Inside the flip method b is being reassigned

b = random.nextBoolean();

cf ---- points to ----> CoinFlipping object
cf.random ---- points to ----> Random object
cf.head ---- points to ----> null
b ---- points to ----> a boolean value - either true or false


4. Step: Back in the main method to print out cf.head

System.out.println("Head: "+cf.head);

cf ---- points to ----> CoinFlipping object
cf.random ---- points to ----> Random object
cf.head ---- points to ----> null
b ---- points to ----> a boolean value - either true or false

As you can see, cf.head is still pointing to null, therefore null is being printed.

Solution:
To change this, you could let the method return a boolean value instead and assign that to cf.head in order to make cf.head point to a new boolean value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.