pass by reference(Simple)

Given the following code:

                                import java.util.Date;
                                     
                                public class Example {
                                        public static void main(String args[]) {
                                                Date d1 = new Date (99, 11, 31);
                                                Date d2 = new Date (99, 11, 31);
                                                method(d1, d2);
                                                System.out.println("d1 is " + d1
                                                                + "\nd2 is " + d2);
                                        }
                                     
                                        public static void method(Date d1, Date d2) {
                                                d2.setYear (100);
                                                d1 = d2;
                                        }
                                }

                           Which one or more of the following correctly describe the behavior
                           when this program is compiled and run?

                           a) compilation is successful and the output is:

                                d1 is Fri December 31 00:00:00 GMT 1999
                                d2 is Fri December 31 00:00:00 GMT 1999

                           b) compilation is successful and the output is:

                                d1 is Fri December 31 00:00:00 GMT 1999
                                d2 is Sun December 31 00:00:00 GMT 2000

                           c) compilation is successful and the output is:

                                   d1 is Sun December 31 00:00:00 GMT 2000
                                   d2 is Sun December 31 00:00:00 GMT 2000

                           d) the assignment 'd1 = d2' is rejected by the compiler because the
                           Date class cannot overload the operator '='.

                           e) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the
                           compiler because the Date class cannot overload the operator '+'.


Since object are passed by reference, why the correct answer here is b)?

Thanks
embertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rainmalCommented:
The problem here is that even though you hear of Java "Passing by reference". what really happens is that "Java never passes method parameters by reference" - Core java1.2

The variables d1 and d2 are exact copies of the original variables.

You can chang the value/state of the object passed to the method if you have acess to its mutator methods.

So what you need to is probably something like this.

public static void method(Date d1, Date d2) {
                d2.setYear (100);
                d1.setYear(d2.getYear());
        }

BTW : Mbormann----I'm back man....now we hunt together like old times.See you around!!
0
JodCommented:
It is not a great example though as this question is very out of date. You would get compilation warnings because Date.setYear and the constructors used above are deprecated.

Dates are only meant to be manipulated using 'long' time valuse which measure the time in milliseconds from a specified date.

This would be a better example:

public class Example {
        public static void main(String args[]) {
                int d1 = 3;
                int d2 = 3;
                method(d1, d2);
                System.out.println("Not in method: d1 is " + d1
                                + ", d2 is " + d2);
        }
       
        public static void method(int d1, int d2) {
          d1 += 2;      
          System.out.println("In method: d1 is " + d1
                                + ", d2 is " + d2);          
                 
        }
}

The output from this is:

In method: d1 is 5, d2 is 3
Not in method: d1 is 3, d2 is 3

which is not at first what you expect...but makes it very clear that d1 has been copied.
0
hsaqallahCommented:
Java NEVER passes by reference (contrary to what appears). In the case of primitive types (int, float, ..etc) a copy is passed. In case of objects, a COPY of the REFERENCE is passed, not the reference itself. Let's take the two cases:
1- When d2 reference was passed, a copy of that reference was given to 'method1'. Now, the original reference and its copy, both are pointing to the same object in memory. When calling a member method (e.g. setYear()) the effect will take place on the original object because you're using a copy of the original reference.
2- When d1 reference is passed, again you have a copy of that reference. However, you tried to change the copy NOT the original reference by using d1 = d2. Now your COPY is pointing to the original D2 object in the memory. Hence, as long as you're working within the scope of that change (i.e. the scope of method1), any methods called through d1 will affect the original d2 object. (try using d1.setYear(150) after d1 = d2). When the method returns, the copy of d1 reference is discarded and the original d1 object won't feel anything!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
Better explanation than Core Java.The best I have seen for a long,long time.

Why dont u write a book?
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.