<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>C (and Java) tricks you to think that a "short" has a single representation but this is not true,</div><div>it has two representations, one on stack (local variables) and one on heap (fields and array elements).</div><div><br data-mce-bogus="1"></div><div>On stack (or on registers because you do not control exactly where a local variable is stored), values are either 32 or 64 bits</div><div>This is dues to the fact that 64 bits CPUs only have 32bits / 64 bits registers.</div><div><br data-mce-bogus="1"></div><div>On heap, everything is a multiple of 8 bits</div><div>(Different CPUs may have different alignment constraints depending how you access the memory (plain, volatile, etc) but that's another story).</div><div><br data-mce-bogus="1"></div><div>So on stack a char is 32 bits and on heap, a char is 16 bits.</div><div><br data-mce-bogus="1"></div><div>Because of that, when you want to compute something using a char, the compiler will have to convert it from char to a 32 bits integer</div><div>(the conversion has to propagate the sign bit so the 32 bits value is the same as the 16 bits value).</div><div><br data-mce-bogus="1"></div><div>That's why in Java, the result of the addition of two shorts is an int,</div><div>  short s = ...</div><div>  short s2 = ...</div><div>  s + s2. // this is an int</div><div><br data-mce-bogus="1"></div><div>I hope it helps.</div><div><br data-mce-bogus="1"></div><div>regards,</div><div>RĂ©mi</div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Kenneth Fogel" <kfogel@dawsoncollege.qc.ca><br><b>To: </b>"discuss" <discuss@openjdk.org><br><b>Sent: </b>Friday, August 8, 2025 6:10:32 PM<br><b>Subject: </b>Memory Usage in Java<br></blockquote></div><div><style><!--

@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Aptos;}

p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Aptos",sans-serif;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Aptos",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:11.0pt;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">






<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Please let me know where I might ask this question or find the answer if this is not the right place.</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">In a book I am working on I am writing about how Java deals with memory allocation for the primitive types on a 64 bit CPU. I know the size of primitive variable types such as byte is 1, int is 4, etc. and that the size
 is related to the range of allowable values. From my C/C++ days I learned that the actual amount of RAM used was related to the word size of a CPU so that when a byte is read you are really reading a word. This meant to me that if I had 10 variables of type
 byte, not in an array, it would consume 80 bytes of RAM. From my reading it appears to be the same in Java except for arrays. Is this accurate? For example, an int is 4 bytes but when Java runs on a 64 bit CPU is it really reading and writing 8 bytes? If it
 is, can we say that the 4 bytes of padding is RAM dead space that cannot be used for anything else?
</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">Thank you for your time,</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">Ken Fogel</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
</div><br></blockquote></div></div></body></html>