0

I'm currently learning Java and I had a question regarding some code that we say in class today, I was wondering if anybody could tell me why the following code outputs 5 and 33 as opposed to just two 33's. Aren't we assigning the value of d2 to d1 in the modify function?

public class AClass {
Date d1 = new Date (5L);
Date d2 = new Date (33L);
void modify ( Date x, Date y) {
    x=y;
}
public static void main (String[] args) {
    new AClass();
}
AClass () {
    modify(d1,d2);
    System.out.println(d1.getTime());
    System.out.println(d2.getTime());
}

}

David W
  • 17
  • 5
  • This question is closed, but for comparison, consider what would happen if `modify` were re-written to state `d1=d2`. – RogueBaneling Nov 21 '14 at 04:48
  • I realize the question has been closed, but I would appreciate if someone could clear the question in my mind: Is this because we are passing the date objects by value? – David W Nov 21 '14 at 05:01
  • Because `x=y;` changes which object the variable `x` references. It doesn't modify either of the objects themselves, or change the variables `d1` or `d2`. – Dawood ibn Kareem Nov 21 '14 at 05:01
  • I see, so to modify the object themselves, would I have to go with `d1 = d2` as opposed to `x = y` – David W Nov 21 '14 at 05:07
  • That would have a similar effect in terms of objects. Think of a variable as like a little sticky label, that you attach to an object. When you write `variable = something`, you're taking that label, and putting it on a different object. So if you write `d1 = d2`, then `d1` and `d2` will refer to the same object afterwards. To actually modify an object, you generally need to call some kind of method on the object. Some types of objects don't actually have any way of modifying the object once it's been created, for example, `String` objects can't be modified. Objects like that are ... – Dawood ibn Kareem Nov 21 '14 at 06:12
  • ... called "immutable". `Date` objects, although technically not immutable, is not really designed to be modified once they're created. – Dawood ibn Kareem Nov 21 '14 at 06:13
  • Thank you good sir, you've been incredibly patient! – David W Nov 21 '14 at 06:16
  • I've just seen your other question - about `x = 7` and so on. It's worth noting that primitive variables (`int`, `long`, `double` etc) work completely differently from object variables like these ones. – Dawood ibn Kareem Nov 21 '14 at 06:23
  • I've come to learn that primitive types are passed-by-value while objects are passed-by-reference. However,in terms of object variables being different from primitive types, what does that mean in a practical sense? Seems like whether things are passed by value or by reference, the original value still says the same,( I mean that either a copy of the value is change or the reference to which it points is changed, correct?) Also, referring specifically to the question above, the modify method due to its scope just affects the parameters as opposed to the actual objects themselves correct? – David W Nov 21 '14 at 06:59

0 Answers0