Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can follow an <code>if</code> statement with a single statement without using curly braces (but do you really want to?), but your problem is more subtle. Try changing:</p> <pre><code>if (x=y) </code></pre> <p>to:</p> <pre><code>if (x==y) </code></pre> <p>Some languages (like PHP, for example) treat any non-zero value as true and zero (or <code>NULL</code>, <code>null</code>, <code>nil</code>, whatever) as <code>false</code>, 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 <code>(x=y)</code> is the value of <code>y</code>, not <code>true</code> or <code>false</code>.</p> <p>You can see this in action with this simple example:</p> <pre><code>if (1) System.out.println("It's true"); if (true) System.out.println("It's true"); </code></pre> <p>The first statement will cause compilation to fail because <code>1</code> cannot be converted to a boolean.</p> <p><strong>Edit:</strong> 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 <code>realOther</code> after you assign it in those statements. This will not work as the scope of <code>realOther</code> is limited to the <code>if</code>/<code>else</code> statement. You need to either move the declaration of <code>realOther</code> above your <code>if</code> statement (which would be useless in this case), or put more logic in your <code>if</code> statement):</p> <pre><code>if (other instanceof Square) ((Square) other).doSomething(); else ((Rectangle) other).doSomethingElse(); </code></pre> <p>To assist further we would need to see more of your <strong>actual</strong> code.</p> <p><strong>Edit:</strong> Using the following code results in the same errors you are seeing (compiling with <code>gcj</code>):</p> <pre><code>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"); } } </code></pre> <p>Note that adding curly braces to that <code>if</code>/<code>else</code> statement reduces the error to a warning about unused variables. Our own <a href="https://stackoverflow.com/users/13531/mmyers">mmyers</a> points out:</p> <blockquote> <p><a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html" rel="nofollow noreferrer">http://java.sun.com/docs/books/jls/third_edition/html/statements.html</a> says: "Every local variable declaration statement is immediately contained by a block." The ones in the <code>if</code> statement don't have braces around them, therefore they aren't in a block.</p> </blockquote> <p>Also note that my other example:</p> <pre><code>((Square) other).doSomething() </code></pre> <p>compiles without error.</p> <p><strong>Edit:</strong> 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 <em>actually</em> trying to do?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload