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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:15:11+00:00 2026-06-01T00:15:11+00:00

Here is an example that outputs 6 : public static void main(String[] args) {

  • 0

Here is an example that outputs 6:

public static void main(String[] args) {
    int i = 1;
    m(++i);
}

static void m(int i) {
    i = ++i + i++; 
    System.out.println(i);
}

We get 6 because in the m(int i) method at first 3 and 3 are summarized, then i becomes 4 (due to the i++), but after that i from the left part takes the summarized 6 value.

But if the method is changed to the following, we get 7:

static void m(int i) {
    i = ++i + ++i; 
    System.out.println(i);
}

But I expected to see 7 in both cases (I’ve been guided by the fact that unary operations, in this case incrementing, have a higher priority than binary operations). Could someone please provide an explanation (or a reference to an explanation) of the ignored i++ in the first example?

  • 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-01T00:15:12+00:00Added an answer on June 1, 2026 at 12:15 am

    ++i increments i and returns the new value of i.

    i++ increments i and returns the old value of i.

    Expressions are evaluated from left to right, taking operator precedence into account.

    So in ++i + i++, when you start with i == 2, you get: ++i which increments i to 3 and returns 3; then i++ which increments i to 4 and returns 3. Then finally you have i = 3 + 3 so i becomes 6.

    Note that these are funny tricks that are not really relevant to real-world programming.

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

Sidebar

Related Questions

Here is a sample code package org.example; import java.lang.reflect.Method; class TestRef { public void
Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public
Here's some example code: class Obj attr :c, true def == that p '=='
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
I want that find empty tags, here is a example txt =<lol1><><lol2> rgx =
I currently have a csv file that I'm parsing with an example from here:
I'm trying to follow the Auth example for Webapp2 that can be found here:
I'm working on a stored proc that executes some dynamic sql. Here's the example
Here is an (artificial) example of using a function that returns an anonymous struct
For example, my goal is to test the code given here: PHP script that

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.