Size of primitive types?

Discussion in 'Java' started by Ken, May 28, 2004.

  1. Ken

    Ken Guest

    Hi. Does the Java spec mandate set sizes for primitive types (e.g.,
    int, float, char, etc.)? I know the ANSI C++ standard does not do
    this, which makes these sizes compiler dependent. So I guess I'm
    trying to figure out if in Java these sizes are compiler or JVM
    dependent.

    Thanks!

    Ken
    Ken, May 28, 2004
    #1
    1. Advertising

  2. Ken

    P.Hill Guest

    P.Hill, May 28, 2004
    #2
    1. Advertising

  3. Ken

    Ryan Stewart Guest

    "P.Hill" <> wrote in message
    news:c985fs$lgl$...
    > Ken wrote:
    > > So I guess I'm
    > > trying to figure out if in Java these sizes are compiler or JVM
    > > dependent.

    >
    > The language specifies their size and sign explicitly, see:
    >

    http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9151
    > "4.2.1 Integral Types and Values"
    >
    > HTH,
    > -Paul
    >

    Actually, as I pointed out in comp.lang.java (please don't multipost, OP),
    the spec doesn't mandate the size of primitive types. It only specifies the
    range of values they may take on.
    Ryan Stewart, May 28, 2004
    #3
  4. Ken

    Roedy Green Guest

    On 28 May 2004 12:50:36 -0700, (Ken) wrote or quoted
    :

    >Hi. Does the Java spec mandate set sizes for primitive types (e.g.,


    yes. This is one of its most pleasant features, and is necessary for
    platform independence. See http://mindprod.com/jgloss/primitive.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 29, 2004
    #4
  5. Ken

    Roedy Green Guest

    On Fri, 28 May 2004 17:33:59 -0500, "Ryan Stewart"
    <> wrote or quoted :

    >Actually, as I pointed out in comp.lang.java (please don't multipost, OP),
    >the spec doesn't mandate the size of primitive types.


    It specifies the size to the extent there is no way you can tell if
    the actual size inside is larger. I/O sizes and endianness are fixed.

    Arithmetic cannot give extra precision.

    Unlike C, the JVM is completely free to add padding and alignment
    bytes wherever it chooses, and you would never know.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 29, 2004
    #5
  6. Ken

    Ryan Stewart Guest

    "Roedy Green" <> wrote in message
    news:eek:...
    > On Fri, 28 May 2004 17:33:59 -0500, "Ryan Stewart"
    > <> wrote or quoted :
    >
    > >Actually, as I pointed out in comp.lang.java (please don't multipost,

    OP),
    > >the spec doesn't mandate the size of primitive types.

    >
    > It specifies the size to the extent there is no way you can tell if
    > the actual size inside is larger. I/O sizes and endianness are fixed.
    >
    > Arithmetic cannot give extra precision.
    >
    > Unlike C, the JVM is completely free to add padding and alignment
    > bytes wherever it chooses, and you would never know.
    >

    I took the OP's question to mean actual size on the hardware since he's
    comparing it to C++. To my understanding, there is no standard for that in
    Java.
    Ryan Stewart, May 29, 2004
    #6
  7. Ryan Stewart wrote:

    >
    > Actually, as I pointed out in comp.lang.java (please don't multipost, OP),


    I don't think comp.lang.java is an authorised newsgroup (has not been
    officially created).

    Mark Thornton
    Mark Thornton, May 29, 2004
    #7
  8. Ken

    Ryan Stewart Guest

    "Mark Thornton" <> wrote in message
    news:...
    > Ryan Stewart wrote:
    >
    > >
    > > Actually, as I pointed out in comp.lang.java (please don't multipost,

    OP),
    >
    > I don't think comp.lang.java is an authorised newsgroup (has not been
    > officially created).
    >

    I thought it was created but then "uncreated". That doesn't keep people from
    posting there. Unfortunately I saw his post there first.
    Ryan Stewart, May 29, 2004
    #8
  9. Ken

    P.Hill Guest

    Ryan Stewart wrote:
    >>comp.lang.java

    >
    > I thought it was created but then "uncreated". That doesn't keep people from
    > posting there. Unfortunately I saw his post there first.


    You read an group you know to be old and out of date? That's odd.
    Try to encourage it's death by leaving it alone and using
    comp.lang.java.programmer or .help

    -Paul
    P.Hill, May 29, 2004
    #9
  10. On Sat, 29 May 2004 08:03:31 -0500, Ryan Stewart wrote:
    > "Mark Thornton" <> wrote in message
    >> Ryan Stewart wrote:

    ....
    >>> Actually, as I pointed out in comp.lang.java (please don't multipost,

    > OP),
    >>
    >> I don't think comp.lang.java is an authorised newsgroup (has not been
    >> officially created).
    >>

    > I thought it was created but then "uncreated". That doesn't keep people from
    > posting there. Unfortunately I saw his post there first.


    What about setting the follow-ups*
    to a valid group?

    * A trivial matter when using 40tude Dialog..

    It also warns of cross-posting, and
    posting to groups not listed on the
    local server (my local server does not
    support c.l.j and 40tude warns me when
    I am about to reply to a thread cross-posted
    to it ..or 'comp.lang.java.javascript').

    It also warns you when F'ups are set to
    the poster themselves, which is a very
    irritating practice.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 29, 2004
    #10
  11. Ken

    Roedy Green Guest

    On Sat, 29 May 2004 06:59:51 -0500, "Ryan Stewart"
    <> wrote or quoted :

    >I took the OP's question to mean actual size on the hardware since he's
    >comparing it to C++. To my understanding, there is no standard for that in
    >Java.


    Java standards don't say HOW something has to be implemented, just the
    net effect on the programmer. So even though the logical sizes of
    primitives to the programmer are rigidly specified, exactly how that
    is pulled off with the hardware is none of our business.

    In a similar way Java is says nothing about how many bits a reference
    is, or whether it must be a pointer a handle (indirect pointer), and
    what additional information it carries besides address.

    It does not even insist that 0 be used to represent null.

    Java does not specify byte sex internally, just that when you do i/o
    it looks as though the machine were 2's complement big endian. This
    way a little-endian machine is not in the least slowed down, except
    when it comes time to do binary i/o.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 29, 2004
    #11
  12. Ken

    Roedy Green Guest

    On Sat, 29 May 2004 17:42:50 GMT, Andrew Thompson
    <> wrote or quoted :

    >What about setting the follow-ups*
    >to a valid group?


    You have to put some answer back in the bad group to tell the OP who
    where the rest of us are hanging out.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 29, 2004
    #12
  13. On Sat, 29 May 2004 18:58:51 GMT, Roedy Green wrote:
    > On Sat, 29 May 2004 17:42:50 GMT, Andrew Thompson wrote or quoted :
    >
    >>What about setting the follow-ups*
    >>to a valid group?

    >
    > You have to put some answer back in the bad group to tell the OP who
    > where the rest of us are hanging out.


    The *answer* *does* go back to the original group..
    It is only *responses* to that post that
    go to the 'follow-up' address..

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 29, 2004
    #13
  14. Ken

    Ryan Stewart Guest

    "P.Hill" <> wrote in message
    news:c9ade9$fg3$...
    > Ryan Stewart wrote:
    > >>comp.lang.java

    > >
    > > I thought it was created but then "uncreated". That doesn't keep people

    from
    > > posting there. Unfortunately I saw his post there first.

    >
    > You read an group you know to be old and out of date? That's odd.
    > Try to encourage it's death by leaving it alone and using
    > comp.lang.java.programmer or .help
    >

    I do encourage its death. I do so by encouraging people who post there to
    instead use one of the aforementioned groups. I think I failed to include
    that in my reply this time though...
    Ryan Stewart, May 29, 2004
    #14
  15. Ken

    Roedy Green Guest

    On Sat, 29 May 2004 19:10:34 GMT, Andrew Thompson
    <> wrote or quoted :

    >
    >The *answer* *does* go back to the original group..
    >It is only *responses* to that post that
    >go to the 'follow-up' address..



    Unless you mention that you have done that, people won't notice the
    follow-up to and their responses just disappear as far as they are
    concerned. In the political groups, it is a standard dirty trick to
    flame then set the follow-up to some irrelevant bitbucket group like
    alt.test so the replies will be thrown away.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 29, 2004
    #15
  16. On Sat, 29 May 2004 19:34:12 GMT, Roedy Green wrote:
    > On Sat, 29 May 2004 19:10:34 GMT, Andrew Thompson wrote or quoted :

    ...
    >>The *answer* *does* go back to the original group..
    >>It is only *responses* to that post that
    >>go to the 'follow-up' address..

    >
    > Unless you mention that you have done that, ..

    <snip Roedy going off on a tangent>

    I make a point of mentioning it.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 29, 2004
    #16
  17. Roedy Green wrote:
    > It specifies the size to the extent there is no way you can tell if
    > the actual size inside is larger. I/O sizes and endianness are fixed.
    >
    > Arithmetic cannot give extra precision.


    Actually, it *can*, disproving your claim above. That's why there is an
    extra feature, the "strictfp" keyword that allows you to *avoid* having
    extra precision...
    Michael Borgwardt, May 30, 2004
    #17
  18. Michael Borgwardt wrote:

    > Roedy Green wrote:
    >
    >> It specifies the size to the extent there is no way you can tell if
    >> the actual size inside is larger. I/O sizes and endianness are fixed.
    >>
    >> Arithmetic cannot give extra precision.

    >
    >
    > Actually, it *can*, disproving your claim above. That's why there is an
    > extra feature, the "strictfp" keyword that allows you to *avoid* having
    > extra precision...


    Sorry, it can provide an extended exponent range but not extra
    precision. Using strictfp ensures that the exponent range is that of
    standard double (float), it makes no difference to the precision (number
    of mantissa bits) which is always fixed at 53 for double expressions and
    I think 24 for float expressions (after allowing for the implied leading
    1 bit in each case).
    Non conforming VMs may behave differently (e.g. Microsoft's VM).

    Mark Thornton
    Mark Thornton, May 30, 2004
    #18
  19. Roedy Green wrote:

    > On Sun, 30 May 2004 15:09:06 +0100, Mark Thornton
    > <> wrote or quoted :
    >
    >
    >>Sorry, it can provide an extended exponent range but not extra
    >>precision.

    >
    >
    > Wait. I thought the idea was to allow the extra guard bits to be used
    > which happens naturally when you use the Pentium FP registers.


    No, the main idea was to allow x86 to execute with little loss of speed.
    The strict mode requires that the range of the exponent be respected
    and this requires the load/save technique to ensure an outsize exponent
    is not carried through intermediate results. The x86 processors have a
    mode to specify the precision to use (float, double or extended) so it
    isn't necessary to allow extended precision in order to use these
    processors at maximum speed.

    Yes, some people did want a way to allow extended precision as well, but
    this was not approved. Also worth noting is that the fastest way of
    performing many floating point operations now is via the SSE and SSE2
    instructions, which provide float and double precision respectively.

    Mark Thornton
    Mark Thornton, May 31, 2004
    #19
  20. Michael Borgwardt wrote:

    > Roedy Green wrote:
    >
    >>> Sorry, it can provide an extended exponent range but not extra
    >>> precision.

    >>
    >>
    >>
    >> Wait. I thought the idea was to allow the extra guard bits to be used
    >> which happens naturally when you use the Pentium FP registers.

    >
    >
    > No, after googling around a bit I found that I had been wrong and Mark is
    > right. Leaving out strictfp only allows the JVM to use the extra bits
    > for the exponent, not the mantissa, in intermediate results (apparently
    > the FPU allows you to choose how to distribute them). This makes FP
    > arithmetic faster and succeed in some instances where strictfp arithmetic
    > has to produce an overflow, but it does not make it more precise.
    >
    > Yes, it appears idiotic to me, too. The only people who would object to
    > some results being more precise (and thus different) on some platforms
    > will be using strictfp anyway, won't they?


    Actually no, this is a very complicated business. Algorithms to provide
    quad precision (based on doubles) require the precision to be exactly as
    expected, but also want as much speed as possible.

    Mark Thornton
    Mark Thornton, May 31, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?S=F8ren_Bak?=

    Collections API for primitive types

    =?ISO-8859-1?Q?S=F8ren_Bak?=, Aug 27, 2003, in forum: Java
    Replies:
    0
    Views:
    405
    =?ISO-8859-1?Q?S=F8ren_Bak?=
    Aug 27, 2003
  2. munki
    Replies:
    5
    Views:
    3,062
    Michael Borgwardt
    Oct 8, 2003
  3. garfield
    Replies:
    3
    Views:
    468
    John C. Bollinger
    Oct 30, 2003
  4. Replies:
    7
    Views:
    600
    Victor Bazarov
    May 9, 2005
  5. Daniel Pitts
    Replies:
    7
    Views:
    470
Loading...

Share This Page