1

I'm not sure what the differences between the two code samples below are. (The first sample uses Picture.java from the standard library):


//chooses a photo from your computer and reads RGB values from it
Picture mypic = new Picture(FileChooser.pickAFile());
Pixel pixelArray [] = mypic.getPixels();
pixelArray[0].setRed(255);
Pixel currentPixel = pixelArray[0];
currentPixel.setRed(0);
pixelArray[0].getRed()

output: 0 <== changes in variable currentPixel changes the value in pixelArray[0] without explicit declaration.


int array[] = new int[4];
array[0] = 1;
int firstElement = array[0];
firstElement = 9;
array[0]

output: 1 <== changes in variable firstElement does not change the value in array[0]


Why does the first code sample output 0 instead of 255, and why does the second code sample output 1 instead of 9?

8
  • The second is operating on a primitive value, the first on a reference type. Commented Jun 24, 2017 at 3:52
  • Thanks for the fast response. I sort of understand what you're saying, but not enough to point out the difference in code if I were asked to. Does it have to do with the Pixel class and the fact that I have to use methods on it? Commented Jun 24, 2017 at 3:56
  • Basically yes. An int is a fixed 32-bit quantity, a reference type is any Object (or sub-class, including Pixel). Commented Jun 24, 2017 at 3:58
  • While you say Picture is from the "standard library", the link you point to defines a Picture class that doesn't define getPixels. So you seem to be using a different Picture class. Commented Jun 24, 2017 at 4:01
  • ok, so just for comparison: how would I store a value in currentPixel and change it without affecting pixelArray[]? Commented Jun 24, 2017 at 4:03

2 Answers 2

3

currentPixel is a reference to pixelArray[0], not a copy (ie. they point to the same object). On the other hand, firstElement is a copy of array[0]. In most cases when you assign an object to a variable, it will be a reference rather than a copy. If you want a new Pixel you will have to create one by calling the constructor.

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

Comments

3

The first one is passing the object around, an instance of Pixel. This is being done under the covers by using the address of the object in memory. Therefore anything you do to the object, is done to the object, and not a copy of the object. This is called 'by reference'.

In the second example int firstElement = array[0]; is taking the value at the memory location of array[0] and copying it to your new memory location firstElement. The two are not the same location in memory so when you update firstElement you are NOT updating array[0]. This is called 'by value'.

EDIT:

To answer your question: There is no such thing as an array method.

What you are getting is an 'Array offset'.

An array is a linear chunk of memory. It is split into equal sizes to handle the values that you ask it to (4 bytes for 32-bit Integer, 2 bytes for a 16-bit integer, 1 byte for a char, etc...). Addresses used to be 32-bit, now 64-bit. So when referencing objects array[0] is the base of the memory chunk array[1] is the base + sizeof(int), and so on.

1 Comment

Okay, so let's see if I'm understanding this clearly... the new Picture is an instance, and the instance variables referring to Picture are mypic, pixelArray, and currentPixel. So anything that is associated with the Picture instance is a reference? (I'm not sure how pixelArray and currentPixel are instances and not instance variables)

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.