aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assigning one primitve variable to another primitive variable.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assigning one primitve variable to another primitive variable.." Watch "assigning one primitve variable to another primitive variable.." New topic
Author

assigning one primitve variable to another primitive variable..

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
public class TestPassign
{
public static void main(String... a)
{
int q=12;
System.out.println("value of q is:"+q);
int w=q;
System.out.println("value of w is:"+w);
System.out.println("______________________");
q=34;
System.out.println("value of q is:"+q);
System.out.println("value of w is:"+w);
}
}

output is: value of q is:12
value of w is:12
_________________________________
value of q is:34
value of w is:12

while we are assigning one variable to another variable the bits which represents memory location should be copied.
so they are having same copy.so output in the second case should be
value of q is:34
value of w is:12.

can anyone explain me clearly...?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Vaes Bart
Greenhorn

Joined: Oct 28, 2008
Posts: 13

The explanation is in your post already.
Also [code] tags are your friend
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
my doubt is that:
In case of assigning one ref var to another ref var bit pattern is copied to another ref var.so they are pointing the same object in the heap.

here also bit pattern of primitive var is copied so both of them have to point same memory location.why they are having different copies?
[ October 31, 2008: Message edited by: Ganeshkumar cheekati ]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Originally posted by Ganeshkumar cheekati:

int w=q;

q=34;


output is: value of q is:12
value of w is:12
_________________________________
value of q is:34
value of w is:12

while we are assigning one variable to another variable the bits which represents memory location should be copied.


while you assign one variable to the other the bits contained in the container i.e q in this case , is copied in a new/other container i.e w .
as far as i think it has nothing with memory location!
when you write q=34 again you are filling in the container with new set of values.
w is not referenced by q as w is a seperate container at a diff. memory location than that of q.And in java it is alwaya pass by value.
Same is the case with reference variables.
Hope this explains.



Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi sekhar.
are you sure?
Is it similar for reference variables also?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Yes you are correct.
When you assign one ref. var to the other the value that is contained in the container i.e the ref. var , which contains the paht to get to the object, is copied in the new ref. var. and hence they point to the same object on the heap.

But in your case where you are re-assigning the primitive value to the variable(container),the contents are copied and not the address locations!

Hope this explains.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
really i have surprised with your previous post regarding ref variables.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
anyway thanks a lot shekhar for spending your time for me.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Originally posted by Ganeshkumar cheekati:
hi sekhar.
are you sure?
Is it similar for reference variables also?


Yes same is the case with the reference variables.
As far as i think ref. var are the containers which contain a path to get to the object on the heap. They themselves are not the object.
So when you say


now e also is pointing to the same object on the heap.
How did it happen? Because the contents of d is copied into e which is the value to reach to the dog object on the heap.

Hope this explains.

And, miss-spelled my name, it's Shekhar.
[ October 31, 2008: Message edited by: Sudipto Shekhar ]
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
import java.awt.Dimension;
class ReferenceTest {
public static void main (String [] args) {
Dimension a = new Dimension(5,10);
System.out.println("a.height = " + a.height);
Dimension b = a;
b.height = 30;
System.out.println("a.height = " + a.height +
" after change to b");
}
}

output:
%java ReferenceTest
a.height = 10
a.height = 30 after change to b

we can conclude that both variables refer to the same instance
of the Dimension object.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Yes the same is explained in the K&B SCJP 5 book. I think you got it by now.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Finally...

assigning primitive var to another primitive var:

if you modify one var it wont effect other var.

assinging ref var to another ref var:

both can see the effect because they are pointing same object.

is it right?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

yes you are right that both can see the changes but not in terms of reference assignment



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Yes absolutely correct.
See this code.


here in the code tt changes the value of i.But when we do tt=new NewTest(); a new object is created on the heap,and what dose'nt change is nt which is passed to the method disp().

Hope this explains.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Mr. Ankit please check your private message.

Thank you.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

@Sudipto I have replied to your message.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Ankit please check your private message.Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assigning one primitve variable to another primitive variable..