java - Java中有 "void"可以参与的语句吗?

标签 java jvm

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/

相关文章:

Java 堆被无法访问的对象淹没

java - Wiremock 对同一请求的不同响应

java - 间歇性 NGINX 错误 : client sent invalid chunked body

java - 自动将 Maven 存储库添加到 Nexus

java - 在 Java 多线程环境中分配唯一代理

java - JVM 标志 CMSClassUnloadingEnabled 实际上是做什么的?

java - 创建 SharedPreferences 时出错 - 无法为 SharedPreferences 文件创建目录

java - Proguard 混淆导致 java.lang.IllegalArgumentException : class declares multiple JSON fields named "a"

java - 测量代码单元的内存使用情况

eclipse - 增加 Eclipse 的 JVM 最大堆大小