wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Programmer Certification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Programmer Certification" Watch "Programmer Certification" New topic
Author

Programmer Certification

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
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
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://aspose.com/file-tools
 
subject: Programmer Certification