Basic doubt regarding memory allocation

Hi

Please go through the program given

import java.util.Date;

class test {

public static void main(String[] args) {

Date date1 = new Date("16-Aug-2004");

System.out.println("date1 = "+d);

System.out.println(date1);

Date date2 = new Date();

c = d;

c .setMonth(11);

System.out.println("date1 = "+date1);

System.out.println("date2 = "+date2);

}

}

I am using jdk141_02. I need to use a date field ( take it as date1)for some sort of validation without changing the the original variable value. So i am creating a temporary variable date2 and copying the value of date1 to date2. Now i am changing date2. But to my amusement it is seen that changes in date2 is getting reflected in date1 also.Does this mean that date1 and date2 are sharing the same memory location. If so what is the use of allocating a new memory to date2 by the line

Date date2 = new Date();

Also it is found that if i am using

Date date2 = new Date(date1.getTime())

instead of

Date date2 = new Date();

the problem is solved and different memory locations are allocated for date1 and date2 and that the changes in date2 is not getting reflected in date1

Please comment on this

[1285 byte] By [Sreekanth_Balu] at [2007-9-30 21:33:54]
# 1

What are the variables d and c referring to?

I think you mean d is referring variable of Date1 object and c is of Date2.

In that case, c will refer to the object of Date1 as it is referring to Date1 object and not Date2 , as u are assinging the address of Date1 object to variable c (i,e c=d, where d is reference of Date1 object).

What u say?

Srinivas

ownjava at 2007-7-7 3:05:14 > top of Java-index,Administration Tools,Sun Connection...
# 2

Hi Srinivas

Sorry that i've made some mistakes in the variable names in the program that i had sent. Here i've corrected it

--

import java.util.Date;

class test {

public static void main(String[] args) {

Date date1 = new Date("16-Aug-2004");

System.out.println("date1 = "+date1);

System.out.println(date1);

Date date2 = new Date();

date2 = date1;

date2.setMonth(11);

System.out.println("date1 = "+date1);

System.out.println("date2 = "+date2);

}

}

I am using jdk141_02. I need to use a date field ( take it as date1)for some sort of validation without changing the the original variable value. So i am creating a temporary variable date2 and copying the value of date1 to date2. Now i am changing date2. But to my amusement it is seen that changes in date2 is getting reflected in date1 also.Does this mean that date1 and date2 are sharing the same memory location. If so what is the use of allocating a new memory to date2 by the line

Date date2 = new Date();

Also it is found that if i am using

Date date2 = new Date(date1.getTime())

instead of

Date date2 = new Date();

the problem is solved and different memory locations are allocated for date1 and date2 and that the changes in date2 is not getting reflected in date1

--

In your reply did you mean to say that the statement

date2 = date1;

Will make date2 to point to the same memory location as date1. If so then how will we be able to copy a date object to another date object efficiently for the purose of using the second object as a temporary variable.Is there any other method by which we can copy the value of date1 to date2.

Regards

Sreekanth

Sreekanth_Balu at 2007-7-7 3:05:14 > top of Java-index,Administration Tools,Sun Connection...
# 3
Any Object reference variable will point to the address of the object, so changes to that reference will effect the object values. I guess we can go for primitve variables like int,double and assign values of object and store separately as temparary variables....Srinivas.
ownjava at 2007-7-7 3:05:14 > top of Java-index,Administration Tools,Sun Connection...
# 4
Thanks Srinivas
Sreekanth_Balu at 2007-7-7 3:05:14 > top of Java-index,Administration Tools,Sun Connection...