This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Date Format related Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Date Format related" Watch "Date Format related" New topic
Author

Date Format related

Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
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;
}
}

this my program which is printing

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

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

here we are assigning d2 value to the d1, so why it is printing different values.

Please Explain

Thanks
Sheetal
Tirthankar Dutta Chaudhuri
Ranch Hand

Joined: Feb 26, 2005
Posts: 48
if I am not wrong it shows that the dates are passes by value to the function . We are changing the value of date2 in the method so it remainschanged but d1 rather a copy of d1 is made to refer the d2 . So its not reflecting the change .
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
It has nothing to do with DateFormat.
In ur code, u have assigned d1 to d2 inside a method, since the variables d1 and d2 are passed by value and not reference, any changes to the reference wont affect the real variables(defined in the main method).
So u get such kinda output.
What gets changed is the year and not the date reference.

Hope its clear
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
ok, the value which is changed here is the value of year only, right? then after that in the method we are assigning the d2 to d1, so it should effect d1 also, bt we are getting 1999 only instead of 2000 in d1.

Why so?

i think am getting confused here, please explain me,

Sheetal
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Changes made to an object reference inside a method do not reflect outside the method, unless the method returns the object, which is not the case in your example.
i.e.
public static void method(Date d1, Date d2) {

d2.setYear (100);

d1 = d2;
}
}
In this case, d1 is assigned the reference value of d2. This is valid only within the method body. Similarly, if you set d1 or d2 to null inside the method, it would be valid only within the scope of the method.

Changes made to the state of an object are reflected even after the method returns!
d2.setYear (100);
In this case, the change is reflected even after the method returns, since you are only changing the state of the object and not the object itself!

Hope that helps!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Date Format related
 
Similar Threads
What is the output and Why
Question on Pass By Reference
static method
Date question
Programmer Certification