Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Potato

Thread: ints, bytes, shorts longs. Simple problem.

  1. #1
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default ints, bytes, shorts longs. Simple problem.

    I am going through a textbook called Head First Java. It just barely gives me some info

    It talkeds about primitive and reference type variables and gives me a list of values signed to the type.

    byte 8bits -128 to 127
    short 16bits -32768 to 32767
    int 32bits -2147483648 to 2147483647

    the it goes to tell me this won't work!!

    int x = 24; //int being the type of variable, x being the name, and 24 being it's value, which fits in the int range
    byte b = x; //why won't this work?? 24 is in the range of both byte and int type variables.

  2. #2
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: ints, bytes, shorts longs. Simple problem.

    I don't think ints can be transferred to byte values.

    EDIT: Basically, a dumbed down version of that. ↓
    Last edited by Darkzombies; 11-28-2012 at 02:43 AM.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: ints, bytes, shorts longs. Simple problem.

    In the expression of int x = 24; you are assigning a literal value to int type. When the literal value is in the range allowed for a type then it will be accepted. But in the expression of byte b = x; you are assigning an integer type to a byte. This is not considered as assigning a literal value to the byte. x will not converted to literal before assigning to the byte type.

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

    Default Re: ints, bytes, shorts longs. Simple problem.

    Quote Originally Posted by gordlonious View Post
    int x = 24; //int being the type of variable, x being the name, and 24 being it's value, which fits in the int range
    byte b = x; //why won't this work?? 24 is in the range of both byte and int type variables.
    You know that 24 fits in a byte and I know that, but the compiler is way too stupid to know that; a compiler doesn't interpret the code it compiles so it has no idea what the value of (int) x might be.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: ints, bytes, shorts longs. Simple problem.

    But they're not the same.

    In the eyes of the compiler the int 24 is:

    00000000 00000000 00000000 00011000

    while the byte 24 is:

    00011000

    there you clearly see that integers have an additional 24 bytes, hence why you can't simply set the value of a byte as an integer, even if the left 24 bits are all 0s. Instead, you need to cast the integer to a byte:

    Java Code:
    byte b = (byte) x;
    that's basically saying "ignore everything except for the right-most 8 bits" so even if your number is say 139544 or:

    00000000 00000010 00100001 00011000

    if you cast it to a byte it's just 24
    Last edited by Potato; 11-28-2012 at 10:37 AM.
    wsaryada likes this.

  6. #6
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: ints, bytes, shorts longs. Simple problem.

    thanks guys, definitely cleared things up.

Similar Threads

  1. Identifying Ints and Non Ints in a string
    By Mnelson in forum New To Java
    Replies: 6
    Last Post: 07-07-2012, 12:08 AM
  2. Longs are read a Ints
    By nhmllr in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 03:31 AM
  3. Combining multiple ints to 2 bytes.
    By gonzalioz in forum Advanced Java
    Replies: 3
    Last Post: 05-12-2011, 03:12 PM
  4. Converting ints to Strings Problem
    By gkoef in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 01:07 PM
  5. Problem with bytes and characters
    By PrinceSendai in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 06:18 AM

Posting Permissions

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