Java – 1-line IF statements in java

conditionaljava

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?

Best Solution

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?