Results 1 to 6 of 6
  1. #1
    t.prasanna is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Unhappy Simple code, but confusing

    Java Code:
    class C{  
    static int f1(int i) {  
    System.out.print(i + ",");  
    return 0;  
    }  
    public static void main (String[] args) {  
    int i = 0;  
    i = i++ + f1(i);  
    System.out.print(i);  
    }}

    When the above code compile and run, it give 1,0 (I am confused how?)
    when it pass the value its passing as 1 then the value should remain same when it returns zero.
    and it should be like i = 1 + 0;

    As per my idea it should give 1,1

    Can any one explain clearly how it works here ?

  2. #2
    t.prasanna is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Thumbs up

    I got it how it work, if some one not clear can check the below link:p

    coderanch.com/t/244938/Programmer-Certification-SCJP/post-increment-confusion

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    i = (i++) + f1(i);
    Try that instead

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    i = i++ + f1(i);
    remember that java support linear algorythm from up to down
    it gives 1.0 because
    i is 1 here and f1(i); is zero :)
    See yourself
    As per my idea it should give 1,1
    Have no idea where you take double as 0.1 here ?
    Your program works just as it written... what problem you can see in it?
    Last edited by Webuser; 01-21-2009 at 02:24 AM.

  5. #5
    helloworld is offline Member
    Join Date
    Sep 2008
    Posts
    11
    Rep Power
    0

    Default

    Hi, what's the significanc of

    return 0;

    ?
    Thanks!

  6. #6
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Thumbs up

    Inline comments how it process

    class C{
    static int f1(int i) {
    //argument i=1
    System.out.print(i + ",");
    //prints 1,
    return 0;
    }
    public static void main (String[] args) {
    //Program starts here
    int i = 0;
    //i value is 0
    i = i++ + f1(i);
    /**i++ is evaluated , then i=1 and 1 is passed
    *as parameter to f1(),there it prints----> 1,
    *then returns 0 to statement i=i++ +f1(i)
    *here,i=0+0
    *which is equal to 0 and then prints 0
    *so finally 1,0
    */

    System.out.print(i);
    }}
    One Life!!! Y Serious??? :)

Similar Threads

  1. Simple code. Could you help pls..?
    By Iliyas in forum New To Java
    Replies: 8
    Last Post: 12-26-2008, 04:17 AM
  2. Simple code help
    By rednose in forum New To Java
    Replies: 11
    Last Post: 11-30-2008, 07:02 AM
  3. Please help me this is simple bit of code
    By BlitzAcez in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 06:52 AM
  4. Confusing with Frameworks
    By hisouka in forum New To Java
    Replies: 0
    Last Post: 08-05-2008, 06:52 AM
  5. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •