3

Can anybody explain the difference between the following two programs? They look equivalent to me but they generate different outputs. What's the reason?

Program 1: Outputs incorrect value; i=1

public class Test1{
    public static void main(String[] args) {
        int[] values = new int[] { 2, 3, 5, 1 };

        int i = 0;
        for (Integer integer : values) {
            i =+ integer.intValue();
        } // for loop ends
        System.out.println("i=" + i);
    }
} 

Program 2: Outputs expected value; i=11"

public class Test2{
    public static void main(String[] args) {
        int[] values = new int[] { 2, 3, 5, 1 };

        int i = 0;
        for (Integer integer : values) {
            i = i + integer.intValue();
        } // for loop ends
        System.out.println("i=" + i);
    }
} 
18
  • 10
    Who is voting all the correct answers down? Commented Jul 5, 2013 at 6:56
  • 2
    Yes, it's really annoying :-/ Commented Jul 5, 2013 at 6:56
  • 1
    Yeah that's some serious bull. Commented Jul 5, 2013 at 6:57
  • 4
    You could have debugged this with a simple System.out.println or some time with a debugger or javap -c to really see what's going on. Commented Jul 5, 2013 at 7:05
  • 3
    Whoever approved this minor edit? Dammit people, it's too minor and introduced same fake edits to even get through the minimum edit length Commented Jul 5, 2013 at 7:13

8 Answers 8

17

Obviously, the major difference is within this line:

i =+ integer.intValue();

Perhaps it was intended to use i += instead of i =+ in the first program. In your version the first program just assigns a value every iteration so it's final result it the last value in array (which is 1). Your second program does what it's supposed to do - it adds all the elements in the array and it's result is 11.

Sign up to request clarification or add additional context in comments.

Comments

10

You try to add and assign but you use the wrong operator.

 for (Integer integer : values) {
     i =+ integer.intValue();
 } // for loop ends

This translates to i = (+ integer.intvalue())

Correct would be:

for (Integer integer : values) {
     i += integer.intValue();
 } // for loop ends

Comments

5

You are using =+ instead of +=

You are probably only getting the last value in your array printed out in one, and the sum in the other right?

That's because you're just overwritting the value repeatedly in one, and actually summing the values correctly in the other.

Comments

4

i =+ integer.intValue(); is a reassignment of the unary operation +integer.intValue(). You're overwriting i with every value in the array. The last one is the final result.

Comments

4

Because you are using unary operator '+', which operates on the sign of integer and does not do addition.

So basically what happens is that the variable i will contain the last value int the values array.

EDIT:

Use of Unary plus operator in Java

When the array value is negative, suppose -1 In this case due to the good old maths rule for sign

  • (+)(+) = (+)
  • (+)(-) = (-)
  • (-)(+) = (-)
  • (-)(-) = (+)

So when you do i = (+ (-1)) = -1. The answer remains -1.

So if you keep last value of the array -1 or some other negative value, you will get the same value.

3 Comments

+1 Finally an answer which explains why (in a manner I can understand)
Actually, unary + doesn't do anything (and it most certainly doesn't change the sign of anything).
I have added the explanation for negative values.
3

Always write operator (+,-,| etc...) before = if LHS is to be involved without writing LHS explicitly like

+= ,-=, |=

or as even comparison operators <=, >= ....

Comments

3

is there anything wrong in class test1 code.It seems it should be i + =integer.intValue() instead of =+

Comments

0
    int[] values = new int[] { 2, 3, 5, 1 };
    int i = 0;
    for (Integer integer : values) {
        i =+ integer.intValue();// here i's value  2,3,5 and finally 1. then your out put is 1.
    } // for loop ends
    System.out.println("i=" + i);

But your next case you are adding current value of i with int values in array. So obviously you will get the some of int value in array for i.

    int[] values = new int[] { 2, 3, 5, 1 };
    int i = 0;
    for (Integer integer : values) {
        i = i + integer.intValue();
    } // for loop ends
    System.out.println("i=" + i);

1 Comment

well a comment with "Try with debug, you will learn more by your self." would have been sufficient and your "answer" could be narrowed down to this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.