6 Replies Latest reply on Dec 18, 2007 1:16 PM by 807601

    Reading bytes in particular position

    807601
      Is it possible to define & retrieve number of bytes allocated?

      For e.g:

      Say, I have four values, each of different datatypes.
      Say,
      int, String, boolean, double.

      I am concatenating them to form an object. As int occupies 32bits, can I say, get the first 4 bytes.

      Nowhere have I found syntax to restrict the size like int(2) or to retrieve bytes. And from the forum replies, I see that the size may vary depending on the processor used. Is my understanding correct?

      In C, it seems so simple to do such retrieval & compare using pointers. Am not able to find any equivalent in Java.

      Please advise.
        • 1. Re: Reading bytes in particular position
          800322
          jeno wrote:
          Is it possible to define & retrieve number of bytes allocated?
          Of what? Primitives: actually amount of memory allocated is an implementation detail of the JVM, thus unspecified (it might as well allocate 8 byte for a 4-byte-int if that's somehow more efficient). The value range of primitives are specified in the JVM specs.
          References: the Instrumentation package has something that can tell you the estimated object's size.
          Nowhere have I found syntax to restrict the size like int(2)
          There is none.
          or to retrieve bytes.
          Ever tried bitwise AND?
          And from the forum replies, I see that the size may vary depending on the processor used. Is my understanding correct?
          Depends on your definition of "size".
          • 2. Re: Reading bytes in particular position
            807601
            What helped me alot when dealing with bit masking and converting in java was the following webpage:

            http://www.leepoint.net/notes-java/data/expressions/bitops.html
            • 3. Re: Reading bytes in particular position
              807601
              Thank you for your reply. Learned something new.
              Instrumentation package has something that can tell you the estimated object's size.
              I went through the details & like you had mentioned, can get only the size of objects & not for primitive data types.
              Ever tried bitwise AND?
              bitwise AND can be used only for long & integers, isn't it?

              Thanks.
              • 4. Re: Reading bytes in particular position
                800322
                jeno wrote:
                I went through the details & like you had mentioned, can get only the size of objects & not for primitive data types.
                As I said, memory allocation for primitives is undefined.
                bitwise AND can be used only for long & integers, isn't it?
                Yes.
                • 5. Re: Reading bytes in particular position
                  807601
                  Thank you very much. My doubt has been clarified.
                  • 6. Re: Reading bytes in particular position
                    807601
                    If you already have a class of objects, you might want to take a look at ObjectInputStream and ObjectOutputStream classes. You can just read and write objects, rather than, go through the trouble of reading the first 4 bytes and deciding what it is, then the next 2 and etc.