java 行if,java中的1行IF语句

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

if (x == y)

z = x * y;

else

z = y - x;

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;

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

What am I doing wrong?

解决方案

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)

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");

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

Edit: 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.

Edit: 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");

}

}

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

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()

compiles without error.

Edit: 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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值