Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Fitting a large primitive into a small reference variable

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Fitting a large primitive into a small reference variable

    for example,this program :


    public class test{
     
    	public static void main(String[] args)
    		{
    			byte i = (byte) 550;
    			System.out.println(i);
    		}
    }

    outputs the number 38 instead of 550,can somebody explain that to me?


    thanks in advance


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Fitting a large primitive into a small reference variable

    That is because a byte is 8 bits while a int is 32 bits. Also, Java primitives are signed, which means the largest number a byte can hold is 127. So, stuffing the number 550 into a byte overflows allotted space gives you a different number.

    Read more here:

    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fitting a large primitive into a small reference variable

    well,i know the size of each type,but can you explain to me why am i getting this number in particular,and not anything else?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by Phobia View Post
    well,i know the size of each type,but can you explain to me why am i getting this number in particular,and not anything else?
    You are casting an int to byte, so the resulting byte is composed of the least significant byte of the int. Write out the value of 550 in binary, and you will see the 8 bits that make up the least significant byte of the int is what is printed out.
    Last edited by copeg; October 22nd, 2009 at 11:23 AM.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Ok here we go

    Lowest byte: -128
    Highest byte: 127

    The thing is that when you assign 128 to a byte it will in fact be -128 as the sign bit changes and 129 will effectively be -127, 130 will be -126 etc.

    Since this is the case, you can calculate this fairly simple.

    550 - 128 - 128 - 128 - 128 = 38

    Its sort of hard to explain, you need to play around with it to understand it.

    Everything above 127 will start over from -128 so to speak.

    550 - 128 = 422

    422 - 128 = 294

    294 - 128 = 166

    166 - 128 = 38 // Now this is the first value that fits in the byte so thats the value we get

    I'm sure this makes little or no sense, but there you go, play around with it

    // Json

  6. #6
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by Json View Post
    Ok here we go

    Lowest byte: -128
    Highest byte: 127

    The thing is that when you assign 128 to a byte it will in fact be -128 as the sign bit changes and 129 will effectively be -127, 130 will be -126 etc.

    Since this is the case, you can calculate this fairly simple.

    550 - 128 - 128 - 128 - 128 = 38

    Its sort of hard to explain, you need to play around with it to understand it.

    Everything above 127 will start over from -128 so to speak.

    550 - 128 = 422

    422 - 128 = 294

    294 - 128 = 166

    166 - 128 = 38 // Now this is the first value that fits in the byte so thats the value we get

    I'm sure this makes little or no sense, but there you go, play around with it

    // Json
    now I get it,thanks a lot


    another question,does the SCJP exam have any questions related to this topic?

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Fitting a large primitive into a small reference variable

    You might see a question which tries to store 128 into a byte and you just need to recognise that it will be a compiler error because its a too large number for a byte.

    // Json

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Fitting a large primitive into a small reference variable

    You might see a question which tries to store 128 into a byte and you just need to recognise that it will be a compiler error because its a too large number for a byte

    what if 127? it will compile ryt?

  9. #9
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by chronoz13 View Post
    what if 127? it will compile ryt?
    Yes. As Json pointed out, the range for a byte is -128 to 127.

  10. #10
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by Json View Post
    You might see a question which tries to store 128 into a byte and you just need to recognise that it will be a compiler error because its a too large number for a byte.

    // Json
    so it won't attempt an explicit cast,right?

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fitting a large primitive into a small reference variable

    Java will only implicitly cast upwards:

    bytes -> shorts -> ints -> longs -> floats -> doubles -> String

    (technically, char's can be cast to int's implicitly)

    Anything else must be explicitly casted (Note: String's can't be explicitly casted down!)

    byte a = 128; // compiler error
    byte a = (byte) 128; // compiles fine

  12. #12
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by helloworld922 View Post
    Java will only implicitly cast upwards:

    bytes -> shorts -> ints -> longs -> floats -> doubles -> String

    (technically, char's can be cast to int's implicitly)

    Anything else must be explicitly casted (Note: String's can't be explicitly casted down!)

    byte a = 128; // compiler error
    byte a = (byte) 128; // compiles fine
    I didn't even mention implicit casting,how is this relevant?

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fitting a large primitive into a small reference variable

    Casting is casting. Implicit casting just means you don't have to specify what you're casting to, so Java will "implicitly" cast it for you.

    int a = 5;
    double b = a;

    Java will see this as:
    int a = (int) 5;
    double b = (double) a;

    You asked when the compiler would throw an error, and I just told you.

  14. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 23rd, 2009)

  15. #14
    Junior Member
    Join Date
    Sep 2009
    Location
    Cairo,Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fitting a large primitive into a small reference variable

    Quote Originally Posted by helloworld922 View Post
    Casting is casting. Implicit casting just means you don't have to specify what you're casting to, so Java will "implicitly" cast it for you.

    int a = 5;
    double b = a;

    Java will see this as:
    int a = (int) 5;
    double b = (double) a;

    You asked when the compiler would throw an error, and I just told you.
    no,you got me wrong


    i was asking about the exam,won't I get a question with explicit casting?

    just like the example in the original post

  16. #15
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Fitting a large primitive into a small reference variable

    byte a = 128; // compiler error
    byte a = (byte) 128; // compiles fine

    how is it possible when this
    byte a =  (byte) 128
    ?
    what will be the logic behind this?..
    i mean...how did it became fine? when the range of byte is only -128 to 127?

  17. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fitting a large primitive into a small reference variable

    Java lets you cast down explicitly because it's useful sometimes. Remember, what we're really dealing with here are bits. So, in a 32 bit integer, 128 is 00000000000000000000000010000000. Casting down to a byte tells the computer to take the 8 furthest right bits (also known as the least significant bits, or LSB) because a byte contains 8 bits. So, a will contain 10000000. Because of the way Java handles signed values, this translates to -128.

    The reason Java doesn't do this implicitly is because it's not what you'd expect to happen normally (-128 equals 128?). You can tell the compiler this is what you want by making the explicit cast.

  18. The Following User Says Thank You to helloworld922 For This Useful Post:

    Json (October 24th, 2009)

Similar Threads

  1. java.net.HttpURLConnection:large file to upload
    By tommy_725 in forum Java Networking
    Replies: 1
    Last Post: October 28th, 2009, 11:53 AM
  2. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM
  3. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 11th, 2009, 11:39 PM
  4. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM