aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte Assignment Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte Assignment Operator " Watch "Byte Assignment Operator " New topic
Author

Byte Assignment Operator

Jenn Palan
Greenhorn

Joined: Mar 25, 2010
Posts: 2


May I know why line A won't get the compile error ? I thought add two bytes together will get an int where conversion is needed for result?
What is the difference between this two lines?

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

We can assign a value directly without a cast if at compile time the compiler can determine that the value will fit in the range of the variable.

In first case this is exactly same as writing



Because compile will know the result (4) at compile time and 4 comes under the range of byte so no cast is required.

But in second case the value of the variables will be determined at rune time so compiler will not be sure about the exact value and will not be able to determine that whether the value will fit in byte range or not. So explicit cast is required.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

actually it's all about promotion rules...... have a glance at it in K & B book.....

all data-types that occupy lesser memory than integer (byte,short) are type promoted to integer during binary operations .....


that is why you got a compiler error...


num1 and num2 values will be promoted to integer and you can't assign an integer to a byte variable without explicit cast....

Remember that implicit casting is done in case of shorthand operators....

Eg:
works perfectly....

gives compile time error ....

Next time remember to include code-snippets inside proper tag (that is code tag)...


KARTHICK.C , SCJP6-93%
(Born to Win)
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

First of all, Welcome to Java-Ranch Jen.

The first statement can be determined at compile-time by the compiler and it knows it can fit into the size of byte.

Where as the second statement can't be determined at the compile time. The exact value can only be known when the
data members are properly initialized at the run-time, so in that cast to an int is mandatory.

As already explained very well by Harpreet.

Hope this helps,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Jenn Palan
Greenhorn

Joined: Mar 25, 2010
Posts: 2
Thanks a lot for your explanation. I think i need to restudy about this chapter again. I might skip a lot
of the explanation in the book.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
karthick chinnathambi wrote:actually it's all about promotion rules...... have a glance at it in K & B book....


Karthick hits the nail on the head!

Whilst most here are reading off the same page, to avoid confusion yes it is a Casting issue but more clearly, the result of an expression is either the wider of the two types if operands are of different types whilst the result of two integer type operations is always an int



However, the reason you can say something like...

....is because 4 is a raw type with no specific typecast and whilst it is indeed an int
,it is in this case they byte-size of the int which is checked and found to fit within the bytes range.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

For the assignment operator used in a way like +=, -=, *= and /= , the compiler will implicitly cast the result.

....is because 4 is a raw type with no specific typecast and whilst it is indeed an int
,it is in this case they byte-size of the int which is checked and found to fit within the bytes range.


Here the reason that we can add 4 as reassign the result to a byte variable is not that the value will fit in the byte range but it is because there will be implicitly type cast by the compiler. Actually it will be interpreted as:


Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Harpreet Singh janda wrote:For the assignment operator used in a way like +=, -=, *= and /= , the compiler will implicitly cast the result.

....is because 4 is a raw type with no specific typecast and whilst it is indeed an int
,it is in this case they byte-size of the int which is checked and found to fit within the bytes range.


Here the reason that we can add 4 as reassign the result to a byte variable is not that the value will fit in the byte range but it is because there will be implicitly type cast by the compiler. Actually it will be interpreted as:




Thanks good to know, but what about straight assignment, does that follow also it is an implicit cast?

E,g.

byte b = 0;
byte c = b+1;
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

If the value being assigned can be determined at compile time and is in range than we can directly assign it like:

Because compiler can know that the value is 0 and 0 comes in range so it will work.
But

Will not work, because compiler will check that the value is out of range so will generate an error.


Will also not work because here we need to provide a cast, as compiler can not determine the value and value can only be determined at run time.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Thanks, that's cleared that up
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte Assignment Operator