Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:29:26+00:00 2026-05-11T03:29:26+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:29:26+00:00Added an answer on May 11, 2026 at 3:29 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.