Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Hi,Can any one help me to understand this code.Am a beginner.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Aggressive
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Hi,Can any one help me to understand this code.Am a beginner.

    class W
    {
    public static void main(String[] args)
    {
    int i=0;
    int j= i++ + test(i) + i;
    System.out.println(i);
    System.out.println(j);
    }
    static int test(int i)
    {

    return i++;
    }
    }


    Am getting 1 and 2 as its output.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hi,Can any one help me to understand this code.Am a beginner.

    There are two println statements. The first prints a 1 and the second prints a 2.
    To see what the code is doing add some more println statements that print out the values of all variables before and after every statement where the variables values are changed.
    System.out.println("b i="+i);
    i = <code that changes i>
    System.out.println("a i="+i);


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Aggressive
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi,Can any one help me to understand this code.Am a beginner.

    I want to know how exactly this increment operators are working in test method.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hi,Can any one help me to understand this code.Am a beginner.

    Did you do as I suggested and add println statements to print out the values of the variables so you can see what each statement is doing?

    Also if you want to see how some operator works, write a short simple program that uses that operator and use println statements to print out the result so you can see what the operator does.

    Also see the tutorial:
    http://docs.oracle.com/javase/tutori...bolts/op1.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Aggressive
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi,Can any one help me to understand this code.Am a beginner.

    Ya i did try,but i dint get whats getting passed through test() method and its output from test method.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hi,Can any one help me to understand this code.Am a beginner.

    Please post the print outs and ask any questions about what a specific statement did that you do not understand.
    whats getting passed through test() method and its output from test method.
    Print out what is passed to test() and what is returned by test().

    Also try this to see how post++ works:
          int k = 0;
          System.out.println("k++="+k++);  // k++=0
          System.out.println("after k++ k="+k);  // after k++ k=1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  2. [SOLVED] Gotten easy code by proffessor that I don't understand, do you?
    By matitorn in forum Java Theory & Questions
    Replies: 7
    Last Post: October 9th, 2012, 01:30 PM
  3. (beginner) if else code.
    By dumb1101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2012, 02:47 AM
  4. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  5. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM