File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when arithmetic opearations are performed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when arithmetic opearations are performed" Watch "when arithmetic opearations are performed" New topic
Author

when arithmetic opearations are performed

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
I was is happy with my concept that arithmetic operations are performed at runtime and compiler doesn't bother about it until i see the output of following code:

public class TestFinalVariables {

public static void main(String []argv)
{
final int firstInt=12, secondInt=5;
byte b=firstInt+secondInt;

System.out.println("7 + 5: "+b);
}
}

compiler doesn't complain anything wrong in the code and output is
"7 + 5= 12"

but compiler starts complaining that int can't be converted to byte when i make following changes:

final int firstInt=125, secondInt=5;

it means that arithmatic operations are performed at compile time ???
confused
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
Since you are using final constants and adding them up and assigning them to a byte variable, the checking is performed at compile time itself since they are constant evaluations. And since the addition operation goes out of range you get a compiler error.
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
thanks for reply, chetan

you mean that if i add variables (which are not final) then the addition is performed at runtime otherwise at compile time.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
15.28 Constant Expression
 
wood burning stoves
 
subject: when arithmetic opearations are performed