xhtml
 

Expressions

To start with, we are going to look at simple programs which use a single expression to produce a result, and then print it out. An expression is a piece of code which calculates a value, made up out of constants, variables, operators, functions and brackets. For example, the formula for one of the roots of a quadratic equation might look like this in Java:

(-b + Math.sqrt(b*b - 4*a*c)) / (2*a)

You have to be more precise than with printed or handwritten formulas, though. For example, in the code above:

If you don't like the idea of having to put Math. in front of all the mathematical functions every time you call them, you can put this line at the top of your program:

import static java.lang.Math.*;

Then you can use all the functions from the Math class directly.


Back