output is 10. I think output is 1. As small is a local to a main method. So it pass by value. ow the answer is 10. Can any one explain? ==========================================================================
What will be the output? A1 14:0-answer A2 0:14 A3 0:0 A4 Compiler Error
The first one: keep in mind that all arrays are objects, so you're passing it by reference. Even if the element type is a primitive, the array itself is an object.
The first evaluation of i is 5, so you will be setting arr. The ++ suffix will then set i to 6. The expression on the right is evaluated like (++i) + (i++). First thing, i is incremented to 7. Then you add that to i, which is 7, and assign that result to arr. Finally you increment i to 8, with no effect on arr. So you end up with 7+7 which is 14.
Again you're setting arr and increment i to 6. Then it's (i++) + (i++). First operand is 6. Then i is incremented to 7. Second operand is 7. Finally i turns to 8, but the sum is 6+7 which is 13.
you have to understand what you are passing, though. you are passing the value of the reference (for what are thought of as objects, but are really object references). So, "small" holds basically the way to get to the object, not the object itself. It's like the variable "small" holds your home address, but isn't your actual house.
so, when you make this call:
you are saying "take the way to get to the object, and hand that to the method". so, in the method, you have a copy of the way to get to the object. or, you give someone a photocopy of your home address.
the method then says "go to the object i'm talking about, and change something". naturally, if i tell you to go to my house and paint it blue, it doesn't matter that you just had a copy of my address, you still went to my house.
if, in the method, you did this:
you change the address written down. If after I give you my address, you write someone else's address replacing mine, then you're not gonna paint my house when you go to that address.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors