Hi, I got a doubt in the output of the following program: import java.util.Date; class CJdate { public static void main(String a[]) { Date d1 = new Date(99,11,31); Date d2 = new Date(99,11,31); amethod(d1,d2); System.out.println("d1 is :"+ d1+"\n d2:"+ d2); } static void amethod(Date p1,Date p2) { p1.setYear(100); p2=p1; } }
when i compiled the following code , i got the output as d1 is : Sun Dec 31 00:00:00 PST 2000 d2 is : Fri Dec 31 00:00:00 PST 1999
I am not able to understand why the value of d2 is not changed as d1.Thanks in advance.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
here Date d1=new Date(99,11,31); Date d2=new Date(99,11,31); //implies two date objects are created method(d1,d2) // implies d1 and d2 are passed by reference static void method(Date p1,Date p2) { p1.setYear(100); // implies if p1 data changes reflects there in d1 p2=p1; // implies p2 is referring to p1 as that of d1 . p2 lost reference to which d2 is referring . now p2 is referring to d1 referring object. no changes has been made in d2 i hope it is clear suresh }
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Originally posted by gouru: here Date d1=new Date(99,11,31); Date d2=new Date(99,11,31); //implies two date objects are created method(d1,d2) // implies d1 and d2 are passed by reference static void method(Date p1,Date p2) { p1.setYear(100); // implies if p1 data changes reflects there in d1 p2=p1; // implies p2 is referring to p1 as that of d1 . p2 lost reference to which d2 is referring . now p2 is referring to d1 referring object. no changes has been made in d2 i hope it is clear suresh }
Thanks! suresh.I got it.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.