0

I've been looking at samples from Core Java volume I and worked out some minor code.

import java.util.Date;
import java.util.GregorianCalendar;

class Test {
    public static void main(String[] args) {
        Employee[] staff = new Employee[3];
        staff[0] = new Employee("Tony Tester", 40000, 1990, 3, 15);

    System.out.println("initial hireDay: " + staff[0].getHireDay());
    Date d = staff[0].getHireDay();
    double tenYearsInMilliSeconds = 10 * 365.25 * 24 * 60 * 60 * 1000;
    d.setTime(d.getTime() - (long) tenYearsInMilliSeconds);
    System.out.println("d: " + d);
    System.out.println("modified hireDay: " + staff[0].getHireDay());

    System.out.println("initial salary: " + staff[0].getSalary());
    int j = staff[0].getSalary();
    j += 10;
    System.out.println("j: " + j);
    System.out.println("modified salary: " + staff[0].getSalary());
}
}

class Employee {
    private int salary;
    private Date hireDay;

public Employee(String n, int s, int year, int month, int day) {
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    hireDay = calendar.getTime();
}

public int getSalary() {
    return salary;
}

public Date getHireDay() {
    return (Date) hireDay;
}
}

Output:

initial hireDay: Thu Mar 15 00:00:00 EST 1990
d: Fri Mar 14 12:00:00 EST 1980
modified hireDay: Fri Mar 14 12:00:00 EST 1980
initial salary: 40000
j: 40010
modified salary: 40000

In the example, the final value of 'salary' hasn't changed while the final value of 'hireDay' has changed. How does this happen?? Is it that the 'salary' has been passed by value where as 'hireDay' has been passed by reference?? But then, I came across this post where it's been mentioned that all the passing in java is by value. If so, how can the value of 'hireDay' change?? Does this have something to do with the 'Date' class being mutable??

Thanks in advance...

2
  • 1
    possible duplicate of Is Java "pass-by-reference"? Commented Jan 24, 2014 at 16:59
  • did you forget to set the value in Employee ? The value is copied and not the reference. or as @vanza - said duplicate Commented Jan 24, 2014 at 17:03

2 Answers 2

1

Date is mutable, you're getting a reference to the date object and calling setTIme on it. So the thing you change is the thing that the Employee object is pointing at.

The salary is a primitive int, so when you access it with int j = staff[0].getSalary(); you're getting the primitive value and when you change j it doesn't affect what the Employee has.

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

3 Comments

Does that mean mutable objects are passed by reference and not by value??
srk1990: the reference is passed by value. Java is pass-by-value, the references are values. A reference is like a pointer in C. see the link vanza proposed as a dupe for this.
Mutability has nothing to do with it.
1

In the example, the final value of 'salary' hasn't changed while the final value of 'hireDay' has changed.

Technically, the value of hireDay hasn't changed -- the printed contents of the object it points to has.

How does this happen??

You did different things to the variables you got back.

You called a method on the object pointed to by d: d.setTime(...);

You performed assignment (=) on j: j += 10; (which is equal to j = j + 10;)

Different actions produce different effects.

Does this have something to do with the 'Date' class being mutable??

Not really. If you had done an assignment on d, like d = ...;, you would also see that it has no effect on the object pointed to by hireDay in the object.

Comments

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.