# Fitting a large primitive into a small reference variable

• October 22nd, 2009, 08:18 AM
Phobia
Fitting a large primitive into a small reference variable
for example,this program :

Code :

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?

• October 22nd, 2009, 08:44 AM
literallyjer
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.

Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
• October 22nd, 2009, 11:04 AM
Phobia
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?
• October 22nd, 2009, 11:21 AM
copeg
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by Phobia
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.
• October 22nd, 2009, 02:44 PM
Json
Re: Fitting a large primitive into a small reference variable
Ok here we go :D

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
• October 22nd, 2009, 04:44 PM
Phobia
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by Json
Ok here we go :D

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?
• October 23rd, 2009, 02:29 AM
Json
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
• October 23rd, 2009, 03:40 AM
chronoz13
Re: Fitting a large primitive into a small reference variable
Quote:

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?
• October 23rd, 2009, 09:54 AM
literallyjer
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by chronoz13
what if 127? it will compile ryt?

Yes. As Json pointed out, the range for a byte is -128 to 127.
• October 23rd, 2009, 10:08 AM
Phobia
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by Json
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?
• October 23rd, 2009, 10:30 AM
helloworld922
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!)

Code :

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

• October 23rd, 2009, 10:39 AM
Phobia
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by helloworld922
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!)

Code :

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

I didn't even mention implicit casting,how is this relevant?
• October 23rd, 2009, 10:50 AM
helloworld922
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.

Code :

int a = 5;
double b = a;

Java will see this as:
Code :

int a = (int) 5;
double b = (double) a;

You asked when the compiler would throw an error, and I just told you.
• October 23rd, 2009, 10:52 AM
Phobia
Re: Fitting a large primitive into a small reference variable
Quote:

Originally Posted by helloworld922
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.

Code :

int a = 5;
double b = a;

Java will see this as:
Code :

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 :D

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

just like the example in the original post :)
• October 23rd, 2009, 10:53 AM
chronoz13
Re: Fitting a large primitive into a small reference variable
Quote:

Code :

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

how is it possible when this
Code :

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?
• October 23rd, 2009, 03:10 PM
helloworld922
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.