'long' integer of 19 digits will not compile

Discussion in 'Java' started by Keith Valentine, Sep 19, 2004.

  1. I am learning about integer types and I have found that when I try to
    compile code that assigns the highest or lowest 'long' integers to a 'long'
    type variable I receive the errors:

    "integer number too large: -9223372036854775808"

    Or

    "integer number too large: 9223372036854775807"

    These values contain 19 digits, however values containing a maximum of 9
    digits will compile without a problem.

    Does anyone know why this is?

    Keith
    Keith Valentine, Sep 19, 2004
    #1
    1. Advertising

  2. On Sun, 19 Sep 2004 12:03:28 -0400, Keith Valentine wrote:

    > I am learning about integer types


    The best group for those learning JAva is described here..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    >..and I have found that when I try to
    > compile code that assigns the highest or lowest 'long' integers to a 'long'
    > type variable I receive the errors:
    >
    > "integer number too large: -9223372036854775808"


    long l = 9223372036854775807l; // compiles just fine, try it

    > These values contain 19 digits, however values containing a maximum of 9
    > digits will compile without a problem.
    >
    > Does anyone know why this is?


    System.out.println(Long.MAX_VALUE);
    System.out.println(Integer.MAX_VALUE);

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
    Andrew Thompson, Sep 19, 2004
    #2
    1. Advertising

  3. Keith Valentine

    VisionSet Guest

    "Keith Valentine" <> wrote in message
    news:n5i3d.4190$...
    > I am learning about integer types and I have found that when I try to
    > compile code that assigns the highest or lowest 'long' integers to a

    'long'
    > type variable I receive the errors:
    >
    > "integer number too large: -9223372036854775808"
    >
    > Or
    >
    > "integer number too large: 9223372036854775807"
    >
    > These values contain 19 digits, however values containing a maximum of 9
    > digits will compile without a problem.
    >


    You are probably doing this:

    long lng = 9223372036854775807;

    which you can't do because the literal value 9223372036854775807 is being
    taken as an integer which it obviously isn't.
    You must do this:

    long lng = 9223372036854775807L;

    which tells the compiler you wish to have the literal treated as a long.
    All literals are taken as an int unless you specify otherwise.

    The error message gives you a clue:

    "integer number too large" it has failed to recognise that you want it to be
    a long.

    --
    Mike W
    VisionSet, Sep 19, 2004
    #3
  4. VisionSet coughed up:
    > "Keith Valentine" <> wrote in message
    > news:n5i3d.4190$...
    >> I am learning about integer types and I have found that when I try to
    >> compile code that assigns the highest or lowest 'long' integers to a
    >> 'long' type variable I receive the errors:
    >>
    >> "integer number too large: -9223372036854775808"
    >>
    >> Or
    >>
    >> "integer number too large: 9223372036854775807"
    >>
    >> These values contain 19 digits, however values containing a maximum
    >> of 9 digits will compile without a problem.
    >>

    >
    > You are probably doing this:
    >
    > long lng = 9223372036854775807;
    >
    > which you can't do because the literal value 9223372036854775807 is
    > being taken as an integer which it obviously isn't.
    > You must do this:
    >
    > long lng = 9223372036854775807L;
    >
    > which tells the compiler you wish to have the literal treated as a
    > long. All literals are taken as an int unless you specify otherwise.



    Yes, always been that way.

    But in the OP's defense, IMHO the jls really /should/ have allowed for
    implicit type conversion when the immediate value is very large.



    >
    > The error message gives you a clue:
    >
    > "integer number too large" it has failed to recognise that you want
    > it to be a long.


    --
    "It's easier to be terrified by an enemy you admire."
    -Thufir Hawat, Mentat and Master of Assassins to House Atreides
    Thomas G. Marshall, Sep 20, 2004
    #4
  5. Thanks Andrew for your help.

    Keith

    "VisionSet" <> wrote in message
    news:0Wi3d.2544$...
    >
    > "Keith Valentine" <> wrote in message
    > news:n5i3d.4190$...
    >> I am learning about integer types and I have found that when I try to
    >> compile code that assigns the highest or lowest 'long' integers to a

    > 'long'
    >> type variable I receive the errors:
    >>
    >> "integer number too large: -9223372036854775808"
    >>
    >> Or
    >>
    >> "integer number too large: 9223372036854775807"
    >>
    >> These values contain 19 digits, however values containing a maximum of 9
    >> digits will compile without a problem.
    >>

    >
    > You are probably doing this:
    >
    > long lng = 9223372036854775807;
    >
    > which you can't do because the literal value 9223372036854775807 is being
    > taken as an integer which it obviously isn't.
    > You must do this:
    >
    > long lng = 9223372036854775807L;
    >
    > which tells the compiler you wish to have the literal treated as a long.
    > All literals are taken as an int unless you specify otherwise.
    >
    > The error message gives you a clue:
    >
    > "integer number too large" it has failed to recognise that you want it to
    > be
    > a long.
    >
    > --
    > Mike W
    >
    >
    Keith Valentine, Sep 20, 2004
    #5
  6. Thanks for your help Mike.

    Keith

    "VisionSet" <> wrote in message
    news:0Wi3d.2544$...
    >
    > "Keith Valentine" <> wrote in message
    > news:n5i3d.4190$...
    >> I am learning about integer types and I have found that when I try to
    >> compile code that assigns the highest or lowest 'long' integers to a

    > 'long'
    >> type variable I receive the errors:
    >>
    >> "integer number too large: -9223372036854775808"
    >>
    >> Or
    >>
    >> "integer number too large: 9223372036854775807"
    >>
    >> These values contain 19 digits, however values containing a maximum of 9
    >> digits will compile without a problem.
    >>

    >
    > You are probably doing this:
    >
    > long lng = 9223372036854775807;
    >
    > which you can't do because the literal value 9223372036854775807 is being
    > taken as an integer which it obviously isn't.
    > You must do this:
    >
    > long lng = 9223372036854775807L;
    >
    > which tells the compiler you wish to have the literal treated as a long.
    > All literals are taken as an int unless you specify otherwise.
    >
    > The error message gives you a clue:
    >
    > "integer number too large" it has failed to recognise that you want it to
    > be
    > a long.
    >
    > --
    > Mike W
    >
    >
    Keith Valentine, Sep 20, 2004
    #6
    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. Dan Pop
    Replies:
    0
    Views:
    1,169
    Dan Pop
    Jun 24, 2003
  2. cyberdude
    Replies:
    2
    Views:
    5,106
    Keith Thompson
    Jun 25, 2003
  3. music4
    Replies:
    3
    Views:
    900
    CBFalconer
    Jan 5, 2004
  4. Oliver Graeser
    Replies:
    10
    Views:
    574
    Oliver Graeser
    Sep 26, 2008
  5. Suresh V
    Replies:
    5
    Views:
    3,685
    SaticCaster
    Jul 5, 2010
Loading...

Share This Page