This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
in java implicitly everything lower than integer automatically gets converted into int and we cannot assign byte value to the int value
hence we have to do the explicit typecasting
we have to cast the int into byte as follows
This should work now
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Joined: Apr 15, 2010
thank you for your reply - but sorry, this was not exactly the answer I'm looking for:
I already know that with such casting, I can "force" the compiler to trust me.
My question was related to the initialization stuff ... besides an int value of 127 (or generally spoken: from -128 until + 127) fits well into a byte ... same thing with a smart cowboy fitting into a pair of jeans ... a fat person - instead - should need a corset ... or simply a cast in order to wear them ;-)
So why do I need to initialize my int in the same line where I declare it?
Ok, as proposed, I've just performed a generation of the following byte codes concerning compile-time and run-time constants:
... and now the run-time stuff:
If I'm right, the variable i2b (line 15, seems to be an alias for i, doesn't it?) is generated in the second byte code - so indeed there's no matter of initialization. Correct?
I've pasted thes two examples in order to become familiar with different behaviours behind the scenes.
Thanks for the hint using a decompiler.
I've just tried out the cavaj decomplier in order to see how compile-time and run-time constants are treated differently.
Here are the results - first comes the compile-time stuff:
and then the run-time stuff:
Of cause I've been obliged to perform an explicit cast to byte (line 13) in the latter case ... otherwise my code should not have compiled (in order to decompile it again)
What I get from the latter case is the fact that "int i" is now a run-time constant (see line 12, last code).
In the first code, "int i" doesn't appear, instead "byte byte0" gets the value 127 (see line 12, first code).
Another nice thing to see (although it has nothing to do with this issue) is the fact that the compiler creates a default no-arg constructor!
It's a very good idea to study some pieces of code compiling and decompiling it again!
Maybe it's beyond the exam, but - as already mentioned - I want to become familiar with different behaviours of Java without having to learn tons of rules by heart.
Did you try inline initialization of the final int with value greater than 127?
Joined: Apr 15, 2010
Yes, I did ... as expected, the compiler jumps up and down, screaming "possible loss of precision" ... that's rather clear to me, because a number exceeding +27 is no longer considered as a byte.
Joined: Jan 01, 2009
Considering a scenario where the compiler allows what you were expecting, what might be the output of the below piece of code
1 int i = 1278;
2 byte b = i;
In case the value of int i is less than 128 and works for your piece of code in the question, how will you enforce that the value is not changed by any other module.
If that gets changed the output of some part of your code would be unpredictable which might lead to a test scenario with random result.
Joined: Apr 15, 2010
Thank you, Suresh!
I see one crucial point concerning compile-time constants: to prevent them of being modified later by a routine or module.
Now it's perfectly clear to me - same thing with case-statements (switch-case) where only compile-time constants are allowed, too.
I revise my statement concerning compiler and "stupid behaviour" ;-)
Guys, I know this topic is kinda dead for 4 months, but let me ask, why this:
works fine, as the a is a compile-time constant, while this:
shows "Type mismatch: cannot convert from long to int" error in line 02. Isn't it the same situation as in the int/byte example - a compile-time constant that fits the int? Am I missing some obvious fact here?