Java中有没有void(带小v,方法返回值)可以参与的语句?
例如,似乎甚至不可能执行 voidMe() instanceof Void
where:
void voidMe(){...}
。
(出于好奇,这是一个理论问题。)
最佳答案
是的——一个简单的方法调用:
voidMe();
这是一个声明。
如果你指的是一个表达式,那么
voidMe()
仍然是一个表达式 - 只是一个结果什么都没有的表达式。来自 section 15.1 of the JLS :
When an expression in a program is evaluated (executed), the result denotes one of three things:
A variable (§4.12) (in C, this would be called an lvalue)
A value (§4.2, §4.3)
Nothing (the expression is said to be void)
...
An expression denotes nothing if and only if it is a method invocation (§15.12) that invokes a method that does not return a value, that is, a method declared void (§8.4). Such an expression can be used only as an expression statement (§14.8), because every other context in which an expression can appear requires the expression to denote something.
突出显示的部分基本上表明唯一使用表达式(例如voidMe()
)是方法调用。没有任何“更大”的表达式可以将 voidMe()
作为其一部分。
关于java - Java中有 "void"可以参与的语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328877/