| Author |
With Regards Of Data Representation In The Memory
|
Ranjith Suranga
Ranch Hand
Joined: Oct 28, 2012
Posts: 84
|
|
Hi,
Please help me here.. I want to create my note today, but I am bit confuse here..
a = 10; a is byte variable.. If I am right .. here 10 is as a int.. that means it has stored here in the 32 bit variable temporarily and then copy to the bits to the byte array..
0 000 0000 0000 0000 0000 0000 0000 1010 ==>>10
^ sign bit
when assigning to the 8 bit (a byte variable) last 8 bits copied to a byte variable
0000 1010 ==>>a now a contains 10;
what I want to know is... when assigning a to b, is it copied directly (8-bits of a) to (8-bits of b) ?
or
is it copied (8-bits of a) to (32-bit int variable) temporarily , then assign to b like above process ?
Please help me quick...
Thank You.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Always use a UseAMeaningfulSubjectLine (<-- click) for your thread.
Ranjith Suranga wrote:
Please help me quick..
And EaseUp (<-- click)
And Welcome To Ranch
|
 |
Ranjith Suranga
Ranch Hand
Joined: Oct 28, 2012
Posts: 84
|
|
Rameshwar Soni wrote:Always use a UseAMeaningfulSubjectLine (<-- click) for your thread.
Ranjith Suranga wrote:
Please help me quick..
And EaseUp (<-- click)
And Welcome To Ranch
Thank you for showing my faults... It is extremely thank you.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5852
|
|
Ranjith Suranga wrote:
what I want to know is... when assigning a to b, is it copied directly (8-bits of a) to (8-bits of b) ?
or
is it copied (8-bits of a) to (32-bit int variable) temporarily , then assign to b like above process ?
It's probably not specified. You can look through the JLS if you want to find out for sure.
I think I read somewhere that most mainstream JVM implementations store bytes and ints in the same internal type, so all the JVM would have to do is a sign-extend or mask out the high bytes, if even that.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
I think you need not worry about such things, see what Jesper de Jong says here.
Hope it helps and lets see what other people has to say.
|
 |
Angus Comber
Ranch Hand
Joined: Jul 16, 2011
Posts: 88
|
|
Your post got me thinking.
So I knocked up this code.
It prints:
10=00000000000000000000000000001010
a=00001010
b=00001010
Not sure if that answers your question. I would guess that the reference held by a is copied to b.
You may be simplifying the copy operation too much. Probably b=a; means references are copied, not actual values.
Is this homework or something because it is not something that a programmer would generally need to worry about.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4747
|
|
Ranjith Suranga wrote:what I want to know is... when assigning a to b, is it copied directly (8-bits of a) to (8-bits of b) ?
or is it copied (8-bits of a) to (32-bit int variable) temporarily , then assign to b like above process?
As Jeff said, it may be specified; but basically, you don't need to know.
What IS specified is exactly how a byte is converted to an int, and vice-versa - see JLS 5.1.2 and 5.1.3.
However, I'd say that all this obsessing with what Java memory looks like is a waste of your time; especially at this stage. In many cases it's not specified; and a major point of Java is that IT manages memory - and in most cases you don't need to know how it does it.
My advice: DON'T use bytes unless you absolutely have to - especially if you're doing arithmetic on them - ints are much easier to work with.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5852
|
|
Angus Comber wrote:
It prints:
10=00000000000000000000000000001010
a=00001010
b=00001010
Not sure if that answers your question.
It doesn't.
Angus Comber wrote:
Not sure if that answers your question. I would guess that the reference held by a is copied to b.
You may be simplifying the copy operation too much. Probably b=a; means references are copied, not actual values.
a and b are primitive variables, not reference variables. They directly hold the numerical values we give them.
|
 |
Angus Comber
Ranch Hand
Joined: Jul 16, 2011
Posts: 88
|
|
a and b are primitive variables, not reference variables. They directly hold the numerical values we give them.
Can you please clarify?
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5852
|
|
Angus Comber wrote:
a and b are primitive variables, not reference variables. They directly hold the numerical values we give them.
Can you please clarify?
Primitive types--byte, char, short, int, long, float, double, boolean--directly store the values that we care about. There's no reference, and no object. So this statement: "Probably b=a; means references are copied" is incorrect for the a and b we're talking about here.
|
 |
Ranjith Suranga
Ranch Hand
Joined: Oct 28, 2012
Posts: 84
|
|
It is extremely thank you for all of you to giving answers, ideas, opinions... They really help me to improve. Once again.. thank you for all of you.
|
 |
 |
|
|
subject: With Regards Of Data Representation In The Memory
|
|
|