• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating and using arrays  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: The code will print i = 1 iArr[0] = 2. I do not understand why it changes the value of int[] iArr, Object references are passed by value.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12558
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are passing in the address of the array by value.

If you give someone the address of your house and said "go here and paint teh bedroom blue", when you go to your house, even though you only gave them a copy of your address, your bedroom will still be painted blue.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
n is not incremented. Why? I do not understand the difference between ingt n and int[] n.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be int n....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12558
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int n delcares n as a primitive.  when you pass it into your method, you pass in the value of the primitive. 

If i have a piece of paper with "7" written on it, I can hand you a new piece of paper and write "7" on it - the value on my piece of paper.  If you erase your "7" and write "8", mine doesn't change.


int [] iArr creates an Object of type Array, that happens to hold int primitives.  With Objects, you pass in the value of the reference - it's like an address. So if I hand you a piece of paper, and copy the address from my slip onto your slip, and tell you to go to that address and paint, the house my slip refers to will have a painted room.

But if you first change the addres on your slip, my slip doesn't change. 
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because iArr is a reference variable that holds an array object.
When you pass the variable to the method, a copy of the variable is passed. The content of a reference variable is an address to an object, and not the object itself.
So, let's take a look in your code:

And about the i variable, it is a variable of a primitive type. For primitives, a copy of the variable contains the value of the primitive, not a reference. So, the caller isn't affected.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand. Let me try:

iArr is a reference variable that holds an array object. When you pass the variable to the method, a copy of the variable is passed. That means after there is the original reference variable and a copy of the original reference variable; both variables point to the same object. You can change the object's state either via the original reference variable or via the copy of the original reference variable.

Is this a correct undersanding?
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:I think I understand. Let me try:

iArr is a reference variable that holds an array object. When you pass the variable to the method, a copy of the variable is passed. That means after there is the original reference variable and a copy of the original reference variable; both variables point to the same object. You can change the object's state either via the original reference variable or via the copy of the original reference variable.

Is this a correct undersanding?


That's right. For mutable objects, that's the idea.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!