Java 更改布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20647969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change boolean Values?
提问by user1871869
I have a question about boolean values in Java. Let's say I have a program like this:
我有一个关于 Java 中布尔值的问题。假设我有一个这样的程序:
boolean test = false;
...
foo(test)
foo2(test)
foo(Boolean test){
test = true;
}
foo2(Boolean test){
if(test)
//Doesn't go in here
}
I noticed that in foo2, the boolean test does not change and thereby doesn't go into the if statement. How would I go about changing it then? I looked into Boolean values but I couldn't find a function that would "set" test from true to false. If anyone could help me out that would be great.
我注意到在 foo2 中,布尔测试不会改变,因此不会进入 if 语句。那我该怎么改呢?我查看了布尔值,但找不到可以将测试从真“设置”为假的函数。如果有人可以帮助我,那就太好了。
采纳答案by Elliott Frisch
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 -
您将原始布尔值的值传递给您的函数,没有“引用”。所以你只是在你的foo
方法中隐藏了这个值。相反,您可能想要使用以下之一 -
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".
其中,也输出“测试中”。
回答by rgettman
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
.
您将参数命名为与实例变量相同的名称。在这里,参数是引用的那个,而不是实例变量。这称为“遮蔽”,其中test
作为参数名称的简单名称遮蔽了也称为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
.
在 中foo
,您将参数更改test
为true
,而不是未更改的实例变量test
。这也解释了为什么它不进入if
块foo2
。
To assign the value, get rid of the parameter on foo
, or use this.test
to reference the instance variable.
要分配值,请去掉 on 的参数foo
,或使用this.test
来引用实例变量。
this.test = true;
and
和
if (this.test)
回答by AnthonyMDev
Your foo
method changed the value of test
to true. It looks like what you want is to use instance variables for each function.
您的foo
方法将 的值更改test
为 true。看起来您想要的是为每个函数使用实例变量。
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.
这样,您的方法只会更改该方法test
内部的值,但您的公共test
参数保留一个false
值。
回答by Adrian Shum
You need to be aware that:
你需要知道:
- In Java, arguments are pass-by-value.
- Boolean, the wrapper type of boolean, is immutable.
- 在 Java 中,参数是按值传递的。
- Boolean 是 boolean 的包装类型,是不可变的。
Because of 1 and 2, you have no way to change the state of the Boolean pass in the method.
由于 1 和 2,您无法更改方法中布尔传递的状态。
You mostly have 2 choice:
您通常有 2 个选择:
Choice 1:Have a mutable holder for boolean like:
选择 1:有一个布尔值的可变持有者,例如:
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:
选择 2:一个更合理的选择:从你的方法中返回值:
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
这种方法是可取的,因为它更适合普通的 Java 编码实践,并且通过方法签名提示调用者它将根据您的输入返回一个新值
回答by Victor Choy
Here is a good explanation.
这是一个很好的解释。
http://www.javadude.com/articles/passbyvalue.htm
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.
Java 有指针,指针的值是传入的。实际上没有办法将对象本身作为参数传递。您只能将指针(值)传递给对象。
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;
}
})