• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

With Regards Of Data Representation In The Memory

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic