Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

With Regards Of Data Representation In The Memory  RSS feed

 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use a UseAMeaningfulSubjectLine (<-- click) for your thread.

Ranjith Suranga wrote:
Please help me quick..


And EaseUp (<-- click)

And Welcome To Ranch
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!