This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning one primitve variable to another primitive variable..

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...?
 
Vaes Bart
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation is in your post already.
Also [code] tags are your friend
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sekhar.
are you sure?
Is it similar for reference variables also?
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really i have surprised with your previous post regarding ref variables.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyway thanks a lot shekhar for spending your time for me.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the same is explained in the K&B SCJP 5 book. I think you got it by now.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right that both can see the changes but not in terms of reference assignment

 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ankit please check your private message.

Thank you.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sudipto I have replied to your message.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit please check your private message.Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic