Java相当于Ruby的`code if条件`

Jos*_*rín 1 java

之前可能已经提出过这个问题,但我似乎无法在谷歌找到任何有用的东西(可能我找错了),也不在这里.

code if conditionJava 中Ruby的等价物(如果有的话)是什么?

Ruby中的示例(取自TutorialsPoint):

$debug=1
print "debug\n" if $debug
Run Code Online (Sandbox Code Playgroud)

在Java而不是做

if (conditional) {
    doSomething();
}
Run Code Online (Sandbox Code Playgroud)

我想做点什么

doSomething() if (conditional);
Run Code Online (Sandbox Code Playgroud)

如果我做

if (conditional) doSomething();
Run Code Online (Sandbox Code Playgroud)

我得到一个警告,建议反转if.

Ell*_*sch 5

你不能用Java做到这一点,它不是合法的语法.

你可以做,

if (conditional) doSomething();
Run Code Online (Sandbox Code Playgroud)

要么

if (conditional) {
    doSomething();
}
Run Code Online (Sandbox Code Playgroud)

甚至改变签名doSomething()采取a boolean然后

doSomething(conditional);
Run Code Online (Sandbox Code Playgroud)

但是如果不编写预编译器,则无法在语言中添加所需的语法.

有关更多信息,请参阅JLS-14.9.该if声明.