Java / Comp Sci Question


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.


Reported as homework. Go read your language documentation.

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?

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.

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.

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.)

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.

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.

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.


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!

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.

:hangs head in shame:

I didn’t even realize that compliment and complement were different words. Would you believe I work in communications?

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?

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


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.

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?)