JAVA - void 和 boolean 方法有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11370306/
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
JAVA - what is the difference between void and boolean methods?
提问by moster67
I am completely new to JAVA. I am writing a wrapper-library in JAVA to make some functions available in Basic-like language.
我对 JAVA 完全陌生。我正在用 JAVA 编写一个包装库,以便在类似 Basic 的语言中提供一些功能。
I got stock at a certain point when I noted that some code were not executed in the JAVA-library although the compiler did not complain (using Eclipse). I resolved it finally by replacing the code as follows:
当我注意到一些代码没有在 JAVA 库中执行时,我在某个时候得到了库存,尽管编译器没有抱怨(使用 Eclipse)。我最终通过替换代码解决了它,如下所示:
public void VideoQuality(int vQuality) //did not work
into
进入
public boolean VideoQuality(int vQuality) //works
Here are the complete code-snippets:
以下是完整的代码片段:
public void VideoQuality(int vQuality) //did not work
{if (vQuality==16) {
vidQuality=16;
}
else if (vQuality==-16) {
vidQuality=-16;
}
else if (vQuality==0) {
vidQuality=0;
}
else
vidQuality=-16;
vitamioExt.setVideoQuality(vidQuality);
}
public boolean VideoQuality(int vQuality) //works
{if (vQuality==16) {
vidQuality=16;
}
else if (vQuality==-16) {
vidQuality=-16;
}
else if (vQuality==0) {
vidQuality=0;
}
else
vidQuality=-16;
vitamioExt.setVideoQuality(vidQuality);
return true;
}
I think voidcorresponds to a sub in Visual Basic while booleancorresponds to a function.
我认为void对应于 Visual Basic 中的 sub ,而boolean对应于一个函数。
I found it odd however that the following code worked using void
但是我发现以下代码使用void工作很奇怪
public void setVolume(float leftVolume,float rightVolume)
{
vitamioExt.setVolume(leftVolume, rightVolume);
}
I am surely missing something very obvious but I can't see why the void-code would not work while the boolean-code worked.
我肯定遗漏了一些非常明显的东西,但我不明白为什么void-code 在boolean-code 工作时不起作用。
Maybe it depends how I call the code?
也许这取决于我如何调用代码?
Anyone who can shed some lights?
谁能点亮一些灯?
EDIT: to clarify what was not working, I meant that the code:
编辑:为了澄清什么不起作用,我的意思是代码:
vitamioExt.setVideoQuality(vidQuality);
did not execute in the void-snippet.
没有在 void-snippet 中执行。
EDIT2: vidQuality was declared in a different part of the code. I just posted the snippets since the problems were with those and variables were all functioning.
EDIT2:vidQuality 是在代码的不同部分声明的。我刚刚发布了片段,因为问题出在这些片段上,并且变量都在运行。
EDIT3: At the end, I guess I must have called the void-snippet erroneously although the compiler did not compile. In either case, both snippets should execute although of course the void-snippet would be the right one to use since I did not expect a return-value.
EDIT3:最后,我想我一定是错误地调用了 void-snippet,尽管编译器没有编译。在任何一种情况下,两个片段都应该执行,尽管 void-snippet 当然是正确的使用,因为我不期望返回值。
回答by T.J. Crowder
The only difference between
之间的唯一区别
public void VideoQuality(int vQuality)
and
和
public boolean VideoQuality(int vQuality)
is that the former doesn't return a value, and the latter does (specifically, a boolean
value). That's the full extent of the difference.
是前者不返回值,而后者返回值(特别是boolean
值)。这就是差异的全部范围。
That means, for instance, that with the void
version of VideoQuality
:
这意味着,例如,void
版本为VideoQuality
:
boolean x = VideoQuality(10); // Will not compile
VideoQuality(10); // Will compile
...because you can't assign the result of a void
function to a variable.
...因为您不能将void
函数的结果分配给变量。
If you used the boolean
version of VideoQuality
:
如果您使用以下boolean
版本VideoQuality
:
boolean x = VideoQuality(10); // Will compile
VideoQuality(10); // Will compile
...because although you canassign the result of a function that returns boolean
to a boolean
variable, you don't haveto. You can ignore the return value if you like. (Usually that's not good practice, but sometimes it's okay.)
......因为尽管你可以指定一个函数,返回的结果boolean
的boolean
变量,你不具备对。如果您愿意,可以忽略返回值。(通常这不是好的做法,但有时也可以。)
I think void corresponds to a sub in Visual Basic while boolean corresponds to a function.
我认为 void 对应于 Visual Basic 中的 sub ,而 boolean 对应于一个函数。
Loosely speaking, yes. void
indicates that the function has no return value, like Sub
in VB. Anything else (boolean
, int
, Foo
, whatever) indicates that A) The function has a return value, and B) It is of the given type. So that's like Function
in VB.
粗略地说,是的。void
表示该函数没有返回值,就像Sub
在 VB 中一样。任何其他 ( boolean
, int
, Foo
, 无论如何) 表示 A) 该函数有一个返回值,并且 B) 它是给定的类型。这就像Function
在 VB 中一样。
回答by ICoffeeConsumer
Functions are declared with a type, much like regular variables, but a function's declared type is called its return type, which says "this function will return as a boolean when called." For example:
函数是用类型声明的,很像常规变量,但函数的声明类型称为它的返回类型,它表示“此函数在调用时将作为布尔值返回”。例如:
boolean alive = true;
boolean isDogAlive() {
return alive;
}
If this function is called, it will return true, so you could say
如果调用此函数,它将返回 true,因此您可以说
if (! isDogAlive()) { // if it returns false
System.out.println("Oh no!");
} else {
System.out.println("Yay!");
}
Functions are declared voidif they don't return anything. You shouldn't need to give a method a boolean return type unless you actually need to get the true/false value. Void should work fine for your situation.
如果函数不返回任何内容,则函数被声明为void。除非您确实需要获取真/假值,否则您不需要为方法提供布尔返回类型。Void 应该适合您的情况。