1-line IF statements in java

Is it possible to have IF statements without curly braces in Java, for example:

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

This is possible in PHP, and I'm not sure if something is wrong.

Explanation: Here is my actual code that I use:

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

But I have errors like "syntax token on realOther, delete this token" and "realOther cannot be resolved" among others.

What am I doing wrong?

+5
source share
13 answers

Yes, you can follow the instructions ifwith a single statement without the use of curly braces (but do you really want this?), But your problem is more subtle. Try changing:

if (x=y)

in

if (x==y)

(, PHP) true zero ( NULL, NULL, nil, ) false, . Java (, ) . , (x=y) y, true false.

:

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

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

, 1 .

: ( , ) , realOther , . , realOther if/else. realOther if ( ), if ):

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

, .

: , ( 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");
    }
}

, if/else . mmyers :

http://java.sun.com/docs/books/jls/third_edition/html/statements.html: " ". if , .

, :

((Square) other).doSomething()

.

: , ( ), , , . , ?

+14

:

int x = 5;
int y = 2;
int z = 0;

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

, , . == = if.

, , , if-then-else. , if else. , .

if-else, Java (else statement if). if:

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

, , , x == y. , .

if (x == y)
  z = y * x;
  w = x * y - 5;

, w = x * y - 5 if. , .

if (x == y) {
  z = y * x;
  w = x * y - 5; // this is obviously inside the if statement now
}
+4

JavaTM:

, . 7, 4:

:, {}. , :

if (condition) //AVOID! THIS OMITS THE BRACES {}!
    statement;
+3

, , terniary

  final int myVal = predicate ? consequent : alternative;

  final int myVal;
  if(predicate)
    myVal = consequent;
  else
    myVal = alternative;
+3

: , :

:

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

- , :

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

, realOther. , , , .

, , ( if) , , "realOther" .

Java , ( ) " , , , ".

, Java . , , if, realOther. if Java-, , realOther .

, setWidth setHeight ( setHeight). IF, Java setWidth ? Java ( , ).

:

, :

// class Rectangle extends Square
// Both implement setHeight, Rect also implements setWidth.
public void reSize(Square other, int h, int w) {
    other.setHeight(h);

    if (realOther instanceof Rectangle) {
        ((Rectangle)realOther).setWidth(w);
}

, , , , , , .

, . , / - , OO, - ...

+3

( /, @Sean), .

, ( , , ), (.. , /while/ ).

+2

- ?

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

//...

realOther.doSomething();

, Java, PHP, realOther if. other? Object? , ? (Shape ?) , - :

Shape realOther = null;
if(other instanceof Shape)
  realOther = other;

//...

if(realOther != null)
  realOther.doSomething();
+2

, if-else, :

if (true) System.out.println("true"); else System.out.println("false");

, , ==.

0

, - , , .

, :

if (x==y)
   z=x*y;
else
  System.out.println("Values weren't equal);
  z=y-x;

! "z" x * y. , , , . , , .

0

, , - , . , , , , .

0

... " " Square realOther " if, , if."

realOther Object, realOther = (Square) ; /= () ;

- .

0

, . - , .

0

, else else:

z = (x == y)? x*y : y-z;
0

All Articles