java中的1行IF语句 [英] 1-line IF statements in java

查看:200
本文介绍了java中的1行IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Java中使用没有大括号的IF语句,例如:

Is it possible to have IF statements without braces in Java, e.g:

if (x == y)
  z = x * y;
else
  z = y - x;

这可能在PHP中,我不确定我是否做错了。

It's possible in PHP, and I'm not sure if I'm doing something wrong.

澄清:这是我正在使用的实际代码:

Clarification: Here's my actual code that i'm using:

if (other instanceof Square)
    Square realOther = (Square) other;
else
    Rectangle realOther = (Rectangle) other;

但我得到的错误如在realOther上的语法令牌,删除此令牌和realOther无法解决除其他外。

But i got errors like "Syntax token on realOther , delete this token" and "realOther cannot be resolved" among others.

我做错了什么?

推荐答案

是的,您可以使用单个语句跟随 if 语句而不使用花括号(但您真的想要吗?),但您的问题更加微妙。尝试更改:

Yes, you can follow an if statement with a single statement without using curly braces (but do you really want to?), but your problem is more subtle. Try changing:

if (x=y)

to:

if (x==y)

某些语言(例如PHP)将任何非零值视为true和零(或 NULL , null nil ,等等) false ,因此条件中的赋值操作有效。 Java只允许在条件语句中使用布尔表达式(返回或求值为布尔值的表达式)。您看到此错误,因为(x = y)的结果是 y 的值,而不是 true false

Some languages (like PHP, for example) treat any non-zero value as true and zero (or NULL, null, nil, whatever) as false, so assignment operations in conditionals work. Java only allows boolean expressions (expressions that return or evaluate to a boolean) inside conditional statements. You're seeing this error because the result of (x=y) is the value of y, not true or false.

您可以在这个简单的例子:

You can see this in action with this simple example:

if (1)
    System.out.println("It's true");

if (true)
    System.out.println("It's true");

第一个语句将导致编译失败,因为 1 无法转换为布尔值。

The first statement will cause compilation to fail because 1 cannot be converted to a boolean.

编辑:我对您更新的示例的猜测(您应该放在问题中而不是作为一个新的答案)是你在这些语句中分配后试图访问 realOther 。这不起作用,因为 realOther 的范围仅限于,如果 / else 声明。您需要将 realOther 的声明移到 if 语句之上(在这种情况下这将是无用的),或者在 if 语句中添加更多逻辑:

My guess on your updated example (which you should have put in the question and not as a new answer) is that you are trying to access realOther after you assign it in those statements. This will not work as the scope of realOther is limited to the if/else statement. You need to either move the declaration of realOther above your if statement (which would be useless in this case), or put more logic in your if statement):

if (other instanceof Square)
    ((Square) other).doSomething();
else
    ((Rectangle) other).doSomethingElse();

为了进一步提供帮助,我们需要查看更多实际代码。

To assist further we would need to see more of your actual code.

编辑:使用以下代码会导致您看到相同的错误(使用 gcj ):

Using the following code results in the same errors you are seeing (compiling with gcj):

public class Test {
    public static void Main(String [] args) {
        Object other = new Square();

        if (other instanceof Square)
            Square realOther = (Square) other;
        else
            Rectangle realOther = (Rectangle) other;

        return;
    }
}

class Rectangle {
    public Rectangle() { }
    public void doSomethingElse() {
        System.out.println("Something");
    }
}

class Square {
    public Square() { }
    public void doSomething() {
        System.out.println("Something");
    }
}

请注意向<$ c $添加大括号c> if / else 语句将错误减少为有关未使用变量的警告。我们自己的 mmyers 指出:

Note that adding curly braces to that if/else statement reduces the error to a warning about unused variables. Our own mmyers points out:


http://java.sun.com/docs/ books / jls / third_edition / html / statements.html
说:每个局部变量
声明语句立即包含在块中的

if 语句中的那些语句在
左右没有大括号,因此它们不在
块中。

http://java.sun.com/docs/books/jls/third_edition/html/statements.html says: "Every local variable declaration statement is immediately contained by a block." The ones in the if statement don't have braces around them, therefore they aren't in a block.

另请注意我的另一个例子:

Also note that my other example:

((Square) other).doSomething()

编译时没有错误。

编辑但我认为我们已经建立了(虽然这是一个有趣的潜入晦涩的边缘情况),无论你想做什么,你都没有这样做正确。那么你实际试图做什么?

But I think we have established (while this is an interesting dive into obscure edge cases) that whatever you are trying to do, you are not doing it correctly. So what are you actually trying to do?

这篇关于java中的1行IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆