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.


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

Results 1 to 4 of 4

Thread: method always returns 0

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Thanked 1 Time in 1 Post

    Default method always returns 0

    public static int Count( ArrayList<Student> studentList )
    int j = 0;
    for( Student s : studentList )
    j = j++;
    return j;

    What am I doing wrong?

  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,645 Times in 2,615 Posts

    Default Re: method always returns 0

    Why do you think the value of j is changed from 0? Add some println statements to see what is happening.
    What is in studentList? Is the code in the loop ever executed?
    What is the value of j in the loop?

    Please edit your post and wrap your code with
    to get highlighting and preserve formatting.

  3. #3
    Join Date
    Jun 2012
    Left Coast, USA
    My Mood
    Thanked 97 Times in 88 Posts

    Default Re: method always returns 0

    Quote Originally Posted by bdennin View Post
    ... wrong...
    Four common ways to increment the value of the variable j: in memory:
        j = j + 1;

        j += 1;



    Your statement in the loop is none of these. It is:
        j = j++;

    This has the following effect:

    Since the ++ operator has higher precedence than the '=' operator, it evaluates the expression on the right of the '=' first, then it stores the value of the expression in memory:

    So, here's the drill:
    • Evaluates the expression j++. The value of the expression is the previous value of the variable j in memory

    • This particular expression has a side-effect: It increments the value of j in memory (after the expression has been evaluated) and, therefore, stores the incremented value in memory. At this point you have incremented the value of j


    • Then, applying the '=' operator, it stores the previously obtained value of the expression in memory, thus overwriting the incremented value that you just stored, with the old value.

    Bottom line: It stores the old value in memory each and every time.

    You can easily verify my assertion by running something like the following:
    public class Z {
        public static void main(String [] args) {
            int j = 0;
            for (int i = 0; i < 5; i++) {
                j = j++;
                System.out.printf("i = %d: j = %d\n", i, j);

    i = 0: j = 0
    i = 1: j = 0
    i = 2: j = 0
    i = 3: j = 0
    i = 4: j = 0



  4. #4
    Junior Member
    Join Date
    Jan 2013
    Thanked 0 Times in 0 Posts

    Default Re: method always returns 0

    Hey nice questions and I am agree with Norm that you should edit your post and wrap it with
    to get highlighting and preserve formatting.
    Have a wonderful day!

Similar Threads

  1. Creating Method that returns a casted object type
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2012, 01:23 PM
  2. Replies: 1
    Last Post: February 12th, 2012, 12:01 AM
  3. [SOLVED] isPrime Boolean Method returns true for squares of primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 29th, 2011, 11:46 PM
  4. [SOLVED] Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: July 9th, 2011, 11:14 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM