# Java / Comp Sci Question

**URL:** <https://boards.straightdope.com/t/java-comp-sci-question/611060>\
**Category:** Factual Questions\
**Created:** [January 29, 2012, 4:56am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060 "2012-01-29T04:56:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Relunctant\_Mathematics](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Relunctant\_Mathematics](https://boards.straightdope.com/u/Relunctant_Mathematics)\
**Post date:** [January 29, 2012, 4:56am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/1 "2012-01-29T04:56:56Z")

</div>

Hello,

I am taking a computer science class that focuses on Java. I’ve spent the last 8 hours or so converting between decimal binary, octal, hex, two’s compliment, etc. Things have been going well, if slow, but I’ve reached a question that I don’t really understand, hoping someone can help me to figure this one out.

The question is:

On a 64 -bit machine, how many bits are required to store variables of each of these types:

a. long

b. int

c. short

d. char

e. byte

f. reference to any object type

Now, I know from my notes that most of these are Java primitive types. I also know that each is associated with different bit types (long is 64, int is 32, short is 16, etc). What I don’t understand is how to calculate bits required based on the machine architecture.

Does a data type require different amounts of bits depending upon whether it is a 32 bit or 64 bit machine? I’m kind of confused.

Any guidance much appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Quartz](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/quartz/32/267_2.png) [@Quartz](https://boards.straightdope.com/u/Quartz)\
**Post date:** [January 29, 2012, 5:09am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/2 "2012-01-29T05:09:15Z")

</div>

Reported as homework. Go read your language documentation.

---

<div class="post-metadata">

**Author:** ![Relunctant\_Mathematics](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Relunctant\_Mathematics](https://boards.straightdope.com/u/Relunctant_Mathematics)\
**Post date:** [January 29, 2012, 5:21am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/3 "2012-01-29T05:21:17Z")

</div>

I’m not trying to get the answer fed to me, I’m trying to understand the question and provided context. I came here after sifting through a number of google responses. Just trying to understand if there is difference between the bit length of integers depending on machine architecture.

Googling has only led me to confirm what’s in my notes, the bit types of the integers, but I can find nothing that suggests this changes based on architecture. Am I missing something? Is the mention of the machine architecture unimportant?

---

<div class="post-metadata">

**Author:** ![md2000](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@md2000](https://boards.straightdope.com/u/md2000)\
**Post date:** [January 29, 2012, 5:37am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/4 "2012-01-29T05:37:43Z")

</div>

Most such answers - depends on the implementation, although java should be the same in every version. For long and short, are these integers or float? I assume integer.  
I don’t know java much, but based on dozens of other language implementations …

Long - long integer 4 bytes? hence 32 bits, about plus/minus 4 billon  
Int , Short - I’m tempted to say these are both 2 bytes plus minus 32768  
char byte - hmmm, byte, you think? A character is 1 byte, the type just indicates treat it as a printable character not a number 0-255 on which you do math and Boolean.

Integers are typically handled as twos complement, where the first bit = one indicates negative.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [January 29, 2012, 5:55am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/5 "2012-01-29T05:55:16Z")

</div>

There isn’t an answer really, though I’m surprised there’s much variation in documentation. It isn’t really significant in Java, which operates through a virtual Java machine. So different processor architectures shouldn’t make any difference. You are also asking about how many bits required to store a data type, as opposed to the range of values allowed. It isn’t necessarily the same thing. A boolean needs only to occupy 1 bit to cover the range of values, 0-1. But in many cases a boolean value will use at least 1 byte because there’s nothing else to do with the other bits. And for a particular processor to maintain word alignment, it might use up to 64 bits of storage.

Anyway, in general, what you are looking for is:

a. long - 64 bits

b. int - 32 bits

c. short - 16 bits

d. char - 16 bits (as far as I know, all Java is now based on 16bit Unicode characters)

e. byte - 8 bits (effectively by definition)

As far as object references, there can be a lot of variation between implementations. The reference itself is just a pointer to a descriptor. I don’t recall offhand any particular implemenation detail, but it’s likely to be 64 bits on a 64 bit machine to take advantage of the address space. But it could be far less since you only need enough bits to reference the number of objects that can be created.

---

<div class="post-metadata">

**Author:** ![Relunctant\_Mathematics](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Relunctant\_Mathematics](https://boards.straightdope.com/u/Relunctant_Mathematics)\
**Post date:** [January 29, 2012, 6:03am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/6 "2012-01-29T06:03:24Z")

</div>

> [@TriPolar](#):
>
> There isn’t an answer really, though I’m surprised there’s much variation in documentation. It isn’t really significant in Java, which operates through a virtual Java machine. So different processor architectures shouldn’t make any difference. You are also asking about how many bits required to store a data type, as opposed to the range of values allowed. It isn’t necessarily the same thing. A boolean needs only to occupy 1 bit to cover the range of values, 0-1. But in many cases a boolean value will use at least 1 byte because there’s nothing else to do with the other bits. And for a particular processor to maintain word alignment, it might use up to 64 bits of storage.
> 
> Anyway, in general, what you are looking for is:
> 
> a. long - 64 bits
> 
> b. int - 32 bits
> 
> c. short - 16 bits
> 
> d. char - 16 bits (as far as I know, all Java is now based on 16bit Unicode characters)
> 
> e. byte - 8 bits (effectively by definition)
> 
> As far as object references, there can be a lot of variation between implementations. The reference itself is just a pointer to a descriptor. I don’t recall offhand any particular implemenation detail, but it’s likely to be 64 bits on a 64 bit machine to take advantage of the address space. But it could be far less since you only need enough bits to reference the number of objects that can be created.

Thanks for this. These are the answers I had based on my notes. I’m guessing the 64 bit machine mention was there just for the object reference answer then. I was concerned that I had missed something and it would change the answers on a through e. I was particularly thrown off because we covered java primitive types and their bit length on the first day of class, and it was the second to last question on the assignment. Each question got progressively harder and then this easy one was thrown in. I figured I must be missing something. Over thinking it I guess.

Thanks again.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [January 29, 2012, 6:06am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/7 "2012-01-29T06:06:30Z")

</div>

> [@TriPolar](#):
>
> d. char - 16 bits (as far as I know, all Java is now based on 16bit Unicode characters)

Which is a massive pain, since Unicode is now far beyond the 16-bit system it was in the early part of its history; Chinese alone required the move to 32-bit codepoints, which _should_ be the last such expansion.

(UTF-16, Java’s native encoding, _can_ encode characters beyond the original 16-bit definition, but, one, it’s a pain, and, two, 16-bit _thinking_ is still prevalent among people who otherwise know things.)

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [January 29, 2012, 6:07am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/8 "2012-01-29T06:07:20Z")

</div>

I don’t know the answer to this in Java, but in C, it would just be “Insufficient information”, even with the specification of a 64-bit machine. It’s not actually specified by the standard, and can vary with the implementation. There are some built-in functions that will tell you the answer, though.

---

<div class="post-metadata">

**Author:** ![Relunctant\_Mathematics](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Relunctant\_Mathematics](https://boards.straightdope.com/u/Relunctant_Mathematics)\
**Post date:** [January 29, 2012, 6:10am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/9 "2012-01-29T06:10:29Z")

</div>

> [@md2000](#):
>
> Integers are typically handled as twos complement, where the first bit = one indicates negative.

Ha, I spent a good two hours on two’s compliment exercises today. Started to have a couple of two’s _insults_ in mind after a while. Got hung up trying to convert from two’s compliment binary back to decimal for negative integers. Somehow missed the ‘subtract 256’ step in my notes and kept wondering why just toggling the bits and adding one wasn’t working.

---

<div class="post-metadata">

**Author:** ![fumster](https://avatars.discourse-cdn.com/v4/letter/f/e9c0ed/32.png) [@fumster](https://boards.straightdope.com/u/fumster)\
**Post date:** [January 29, 2012, 6:30am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/10 "2012-01-29T06:30:31Z")

</div>

> [@Relunctant\_Mathematics](#):
>
> Ha, I spent a good two hours on two’s compliment exercises today. Started to have a couple of two’s _insults_ in mind after a while. Got hung up trying to convert from two’s compliment binary back to decimal for negative integers. Somehow missed the ‘subtract 256’ step in my notes and kept wondering why just toggling the bits and adding one wasn’t working.

Are you being ironic or do you not know how to spell complement?

I’m old enough to remember when the lengths of bytes was not necessarily 8 bits, never mind word size.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [January 29, 2012, 7:01am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/11 "2012-01-29T07:01:46Z")

</div>

> [@sh1bu1](#):
>
> Are you being ironic or do you not know how to spell complement?
> 
> I’m old enough to remember when the lengths of bytes was not necessarily 8 bits, never mind word size.

I was going to bring up non-8bit bytes, but I didn’t know if anybody else remembered those. The accepted definition of a byte is 8 bits now, but it didn’t have to be that way. And of course ‘word’ is totally arbitrary.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [January 29, 2012, 7:35am UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/12 "2012-01-29T07:35:27Z")

</div>

> [@TriPolar](#):
>
> And of course ‘word’ is totally arbitrary.

Word.

The definition of ‘word’ has varied from ‘variable-length’, to multiple _decimal_ digits to, for quite a while, 36 bits (with 18-bit pointers) on large machines, 12 bits on smaller machines, and 60 bits on supercomputers, among other sizes.

IBM finally convinced most of the world to go to binary-only words of some power-of-two size divided into eight-bit bytes when the System/360 project took off in the mid-1960s and through the 1970s; it took a while, but the fact microprocessors (single-chip CPUs) have always worked that way since their introduction in 1971 really helped. Now, we can even share text files between computers more often than not! Even if they were made by different companies! Hooray!

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [January 29, 2012, 12:13pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/13 "2012-01-29T12:13:02Z")

</div>

Does Java not have a ‘sizeof’? Or do you not have access to a 64 bit machine?

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [January 29, 2012, 5:44pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/14 "2012-01-29T17:44:35Z")

</div>

> [@Khadaji](#):
>
> Does Java not have a ‘sizeof’? Or do you not have access to a 64 bit machine?

I don’t recall if Java has a sizeof function or operator. It doesn’t really make sense to the Java model though. You know the type of the variable, and otherwise the actual storage requirements are indeterminate. Java is not a static language that allocates memory in fixed amounts, or has specific storage locations for variables.

There are means of picking up the type and parameter signature for a method.

---

<div class="post-metadata">

**Author:** ![Relunctant\_Mathematics](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Relunctant\_Mathematics](https://boards.straightdope.com/u/Relunctant_Mathematics)\
**Post date:** [January 29, 2012, 11:10pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/15 "2012-01-29T23:10:44Z")

</div>

> [@sh1bu1](#):
>
> Are you being ironic or do you not know how to spell complement?

_:hangs head in shame:_

I didn’t even realize that [compliment and complement were different words.](http://public.wsu.edu/~brians/errors/complement.html) Would you believe I work in communications?

---

<div class="post-metadata">

**Author:** ![erislover](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@erislover](https://boards.straightdope.com/u/erislover)\
**Post date:** [January 30, 2012, 5:33pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/16 "2012-01-30T17:33:14Z")

</div>

I thought “word” was just a shorthand for the expected size of the machine operand in standard addressing. Nowadays it is usually 8, 32, or 64, though 16 is still floating around and odd (as in slightly strange) numbers never quite left. Aren’t the latest forth chips from Chuck Moore’s project 18 bits or something?

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [January 30, 2012, 8:21pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/17 "2012-01-30T20:21:46Z")

</div>

> [@Derleth](#):
>
> IBM finally convinced most of the world to go to binary-only words of some power-of-two size divided into eight-bit bytes when the System/360 project took off in the mid-1960s and through the 1970s; it took a while, but the fact microprocessors (single-chip CPUs) have always worked that way since their introduction in 1971 really helped.

On the other hand, their **EBCDIC** standard is struggling to catch on.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [January 30, 2012, 8:31pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/18 "2012-01-30T20:31:42Z")

</div>

> [@Bytegeist](#):
>
> On the other hand, their **EBCDIC** standard is struggling to catch on.

Reported

---

<div class="post-metadata">

**Author:** ![Greg\_Charles](https://avatars.discourse-cdn.com/v4/letter/g/839c29/32.png) [@Greg\_Charles](https://boards.straightdope.com/u/Greg_Charles)\
**Post date:** [January 30, 2012, 8:48pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/19 "2012-01-30T20:48:03Z")

</div>

In C, the length of many primitive types depends on the implementation of the C compiler. Java specifically changed that, so an int in Java means the same thing whether it’s on 32 bit or 64 bit, and whether the platform is desktop computer, mainframe, smartphone, or microwave oven. I imagine that’s the point that test question was trying to make.

There is no sizeof operator in Java.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [January 30, 2012, 11:45pm UTC](https://boards.straightdope.com/t/java-comp-sci-question/611060/20 "2012-01-30T23:45:40Z")

</div>

> [@Greg\_Charles](#):
>
> In C, the length of many primitive types depends on the implementation of the C compiler. Java specifically changed that, so an int in Java means the same thing whether it’s on 32 bit or 64 bit, and whether the platform is desktop computer, mainframe, smartphone, or microwave oven.

Right, and this is true regardless of whether Java is compiled to JVM bytecode or to machine code, for example when using gcj. I know that, at least when Sun (the corporation originally responsible for Java) was still in business (it’s since been bought out by Oracle), the word ‘Java’ was trademarked _ **as relates to computer programming languages** _\* and Sun had, as a condition of your use of the trademark, a compatibility test to ensure your implementation was the same as Sun’s implementation in all important respects.

Microsoft violated the trademark by releasing a non-compliant Java implementation (satisfying the first two steps of ‘Embrace, Extend, Extinguish’); Sun sued, Microsoft lost, and that’s why we now have .Net, C#, and a whole new, different, and incompatible all-encompassing runtime scheme.

\*(Why do I expect people will now post “But java is a common word! It means coffee! And an island! Waaaah!” in a little while?)

[Next page](https://boards.straightdope.com/t/java-comp-sci-question/611060.md?page=2)
