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

The Archive Base Latest Questions

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

When i run the following example i get the output 0,2,1 class ZiggyTest2{ static

  • 0

When i run the following example i get the output 0,2,1

class ZiggyTest2{

        static int f1(int i) {  
            System.out.print(i + ",");  
            return 0;  
        } 

        public static void main(String[] args) {  
            int i = 0;  
            int j = 0;  
            j = i++;   //After this statement j=0 i=1
            j = j + f1(j);  //After this statement j=0 i=1
            i = i++ + f1(i);  //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0
            System.out.println(i);  //prints 2?
        } 
    } 

I don’t understand why the output is 0,2,1 and not 0,2,2

  • 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-05-27T17:11:27+00:00Added an answer on May 27, 2026 at 5:11 pm
     i = i++ + f1(i);  
    

    i++ means i is now 2. The call f1(i) prints 2 but returns 0 so i=2 and j=0

    before this i = 1 , now imagine f1() called and replaced with 0

    so

    i = i++ + 0;
    

    now it would be

    i = 1 + 0 // then it will increment i to 2 and then (1 +0) would be assigned back to `i`
    

    In Simpler words (from here @ Piotr )

    “i = i++” roughly translates to

    int oldValue = i; 
    i = i + 1;
    i = oldValue; 
    

    Another such Example :

    • The same fundamental works here
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when I try to run sample example of Google
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
I'm following the ASP.Net MVC TaskList example video and on clicking Run in Visual
When I run the following code to test copying a directory, I get a
In the following example class Employee { var $name; var $city; protected $wage; function
I have the following CoffeeScript code example: class TestClass constructor: () -> @list =
When I sbt run the following code, package com.example import java.io.ObjectInputStream import java.io.ObjectOutputStream import
When I run the code below I get the following error. C:\Documents and Settings\BOS\Desktop\test>java
Can not run following SQL from ant's sql task : BEGIN DBMS_AQADM.CREATE_QUEUE_TABLE( queue_table =>
I run the following command in Emacs unsuccessfully C-u How can you clear the

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.