Two Laptop Bag*
The moose likes Beginning Java and the fly likes Final Byte variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final Byte variable" Watch "Final Byte variable" New topic
Author

Final Byte variable

Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 102




Q1. What is diffrence between final byte and bye variable ?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

In the first case, when adding two byte variables, internally they are being promoted to int and so the result is an int, which you must cast back to a byte - because in Java for an arithmetic operation to be performed, the operands must be atleast an int.
In the second case, since you are adding two final byte variables, Java compiler knows that you cannot change them in future and so it performs addition at the compile time itself and assigns it to b7. Here the promoting to int & casting to byte - all are done internally at the compiling stage itself.


OCPJP 6
 
 
subject: Final Byte variable
 
Similar Threads
Arithmetic problem in division
Java scientific calculator (reposted)
Calculator Problem
Creating a file from Various chunks of different files in java
tictactoe help