Results 1 to 7 of 7
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Primitive Data Types

    This is literally a one-sentence question. Java's tutorial says the long has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). However, when I declare this:

    Java Code:
    long number = 600851475143;
    I get an error saying "The literal 600851475143 of type int is out of range". So what's going on? Am I missing something here? Did Java lie to me?! (most likely I'm missing something.. Java never lies). Help greatly appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Primitive Data Types

    Quote Originally Posted by CuppaCoffee View Post
    This is literally a one-sentence question. Java's tutorial says the long has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). However, when I declare this:

    Java Code:
    long number = 600851475143;
    I get an error saying "The literal 600851475143 of type int is out of range". So what's going on? Am I missing something here? Did Java lie to me?! (most likely I'm missing something.. Java never lies). Help greatly appreciated.
    Add a trailing 'L' i.e.

    Java Code:
    long number = 600851475143L;
    Literal integer numbers are assumed to be ints by default by the compiler, until you tell it otherwise.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Primitive Data Types

    Just like it said, the literal is out of range for an "int." To assign the value of a long, append an L to the end of it.

    Regards,
    Jim
    Last edited by jim829; 05-08-2013 at 09:43 PM. Reason: slow draw McGraw
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Primitive Data Types

    Quote Originally Posted by jim829 View Post
    Last edited by jim829; Today at 08:43 PM. Reason: slow draw McGraw
    I love this so much; I thought it is worthwhile to repeat it here ;-)

    kind regards,

    Jos (<--- speedy)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Primitive Data Types

    Quote Originally Posted by JosAH View Post
    Add a trailing 'L' i.e.

    Java Code:
    long number = 600851475143L;
    Literal integer numbers are assumed to be ints by default by the compiler, until you tell it otherwise.

    kind regards,

    Jos
    Quote Originally Posted by jim829 View Post
    Just like it said, the literal is out of range for an "int." To assign the value of a long, append an L to the end of it.

    Regards,
    Jim
    Aaaahhhh, I see... What does the 'L' represent though? I've also seen the letter 'f' or 'F' being added to the end and in the middle of numerical variables...

    Well, thanks for the assistance, guys! (regardless of the speed )

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Primitive Data Types

    L is for long, F is for float. Without them, the values are taken to be bytes, shorts, and ints and doubles.

    Regards,
    Jim
    Last edited by jim829; 05-08-2013 at 11:35 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

Similar Threads

  1. Passing Primitive Data Type Arguments
    By tariqm in forum New To Java
    Replies: 6
    Last Post: 04-15-2012, 01:58 PM
  2. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 04:27 PM
  3. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 06:43 AM
  4. primitive Data types
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 09:29 PM
  5. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •