Change boolean Values?

后端 未结 5 1748
礼貌的吻别
礼貌的吻别 2020-12-11 16:40

I have a question about boolean values in Java. Let\'s say I have a program like this:

boolean test = false;
...
foo(test)
foo2(test)

foo(Boolean test){
  t         


        
相关标签:
5条回答
  • 2020-12-11 16:50

    Here is a good explanation.

    http://www.javadude.com/articles/passbyvalue.htm

    Java has pointers, and the value of the pointer is passed in. There's no way to actually pass an object itself as a parameter. You can only pass a pointer (value) to an object.

    And my solution

       public static class MutableBoolean {
            public boolean value;
    
            public MutableBoolean(boolean value) {
                this.value = value;
            }
        }
    

    usage:

    MutableBoolean needStop = new MutableBoolean(false);
    call( new Listener(needStop){
       void onCallback(){
          needStop.value = true;
       }
    
    })
    
    0 讨论(0)
  • 2020-12-11 16:55

    You're passing the value of a primitive boolean to your function, there is no "reference". So you're only shadowing the value within your foo method. Instead, you might want to use one of the following -

    A Holder

    public static class BooleanHolder {
      public Boolean value;
    }
    
    private static void foo(BooleanHolder test) {
      test.value = true;
    }
    
    private static void foo2(BooleanHolder test) {
      if (test.value)
        System.out.println("In test");
      else
        System.out.println("in else");
    }
    
    public static void main(String[] args) {
      BooleanHolder test = new BooleanHolder();
      test.value = false;
      foo(test);
      foo2(test);
    }
    

    Which outputs "In test".

    Or, by using a

    member variable

    private boolean value = false;
    
    public void foo() {
      this.value = true;
    }
    
    public void foo2() {
      if (this.value)
        System.out.println("In test");
      else
        System.out.println("in else");
    }
    
    public static void main(String[] args) {
      BooleanQuestion b = new BooleanQuestion();
      b.foo();
      b.foo2();
    }
    

    Which, also outputs "In test".

    0 讨论(0)
  • 2020-12-11 17:09

    You named your parameter the same as an instance variable. Here, the parameter is the one referenced, not the instance variable. This is called "shadowing", where the simple name test as a parameter name shadows the instance variable also called test.

    In foo, you changed the parameter test to true, not the instance variable test, which was unchanged. That explains why it doesn't go into the if block in foo2.

    To assign the value, get rid of the parameter on foo, or use this.test to reference the instance variable.

    this.test = true;
    

    and

    if (this.test)
    
    0 讨论(0)
  • 2020-12-11 17:12

    You need to be aware that:

    1. In Java, arguments are pass-by-value.
    2. Boolean, the wrapper type of boolean, is immutable.

    Because of 1 and 2, you have no way to change the state of the Boolean pass in the method.

    You mostly have 2 choice:

    Choice 1: Have a mutable holder for boolean like:

    class BooleanHolder {
       public boolean value;  // better make getter/setter/ctor for this, just to demonstrate
    }
    

    so in your code it should look like:

    void foo(BooleanHolder test) {
      test.value=true;
    }
    

    Choice 2: A more reasonable choice: return the value from your method:

    boolean foo(boolean test) {
      return true;   // or you may do something else base on test or other states
    }
    

    the caller should use it like:

    boolean value= false;
    value = foo(value);
    foo2(value);
    

    This approach is preferrable as it fit better with normal Java coding practices, and by the method signature it gives hint to the caller that it is going to return you a new value base on your input

    0 讨论(0)
  • 2020-12-11 17:14

    Your foo method changed the value of test to true. It looks like what you want is to use instance variables for each function.

    boolean test = false;
    ...
    foo(test)
    foo2(test)
    
    foo(Boolean test){
      this.test = true;
    }
    foo2(Boolean test){
      if(this.test)
       //Doesn't go in here
    }
    

    This way, your method only changes the value of test inside of that method, but your public test parameter stays with a false value.

    0 讨论(0)
提交回复
热议问题