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 8334577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:23:55+00:00 2026-06-09T03:23:55+00:00

This is my first post here so please forgive any protocol errors. My question

  • 0

This is my first post here so please forgive any protocol errors.

My question is simply trying to understand what is happening with the following Java code. I fully understand that the use of parentheses would clarify everything, but the resulting output seems to fly in the face of convention regarding Java order of operations.

public class Tester
{
   public static void main(String[] args)
   {
    int total=9, num=13;
    if (total>4 || ++num>15 && total>0)
    {
           System.out.println("short");
    }
    System.out.println(num);
   }
}

The output is:
short
13

It is obvious the ++num did not execute. If strict order of operations had been observed it should have been the first thing to happen. It didn’t. So next is the &&. If the && is done by order of precedence over the ||, then the same…the ++num should happen first. It didn’t. So, to me it seems the output was determined with the || executed first, shortciruiting the ++num and then, working with the &&, resulted in the short being printed. Were the order of operation rules simply ignored and the Boolean expression executed left to right? Is the increment operator causing the irregular behavior?

Thanks for any insight as to what is actually going on with this code.

  • 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. Editorial Team
    Editorial Team
    2026-06-09T03:23:57+00:00Added an answer on June 9, 2026 at 3:23 am

    This has nothing to do with precedence/associativity (which deals with how expressions are parsed)… this has to do with Java evaluation order — which is left to right. You appear to be misunderstanding how short-circuit logic works.

    The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.


    Now, let’s attempt to evaluate this expression incrementally…

    total > 4 || ++num > 15 && total > 0
    

    Since total > 4 evaluates to true, the condition evaluates to true and the if branch is taken immediately rather than evaluating the rest of the conditional.

    If you change total to equal 4, then the left-operand (total > 4) of the short-circuit OR is false and thus it evaluates the right-operand (++num > 15 && total > 0).

    If you change num to equal 15, then the short-circuit AND left-operand (++num > 15) evaluates to true and thus it finally evaluates the AND right-operand (total > 0) to determine whether the conditional is true. If total > 0 is false, then the conditional is also false.

    Below is the code rewritten for clarity to highlight the flow.

    if (total > 4) {
      System.out.println("short");
    } else {
      if (++num > 15) {
        if (total > 0) {
          System.out.println("short");
        }
      }
    }
    System.out.println(num);
    

    You can read more on Java conditional operators in the relevant Java Tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[please forgive formatting errors-- first post here and I tried earnestly to do it
This is my first post, so please forgive me if this question has been
Okay this is my first post here so please forgive me if i mess
This is my first post here so go easy. I am trying to build
This is my first time here so I hope I post this question at
This is my first post on stackoverflow, so please excuse me if my question
This is my first post to this website. Here is my question: I want
This is my first post in any forum so please bear with me. I
This is my first post in here and I wouldn't post a question here
this is my first post. please be kind :) i'm trying to get picture

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.