Join Peggy Fisher for an in-depth discussion in this video Understanding basic expressions, part of Learning Java 8.
- When writing Java expressions, we need to know how to convert our algebraic equations to Java. So before we move to our next program, let's quickly review the math opterations in Java. You'll notice that the symbols are similar to other languages. Some of the operations work differently, depending on the data type of the operands. One of these operations is the plus sign. When it's used with two numbers, they're added together. But when it's used between two strings, it creates one long string. This is called concatenation.
We have the minus sign for subtraction, the asterisks for multiplication, the forward slash for division, and we have the percent sign which is something called modulus division, but it's also referred to as remainder division. Let's take a look at some algebraic equations and their corresponding Java equivalents. We're used to A equals pi r squared. This is how we find the area of a circle. In Java, we would use more meaningful names and we would write area equals, to get pi, we use the math library Math.PI times radius times radius.
a squared plus b squared equals c squared is Pythagorean theorem, which is used quite often in algebra. We use the variable c to represent the hypotenuse, but to find the value of c, we need to rearrange our equation so that c is on the left-hand side of the equals sign, and c is equal to the square root. Well, we don't have a square root symbol, but we can use the math library again and do Math.sqrt and in parenthesis we'll say a squared, which is a times a, plus b squared, or b times b.
Let's take a look at one more. How about y equals mx plus b. We know that that is the equation of a line. In Java, we might write y equals m times x. If we don't put the asterisks in between the m and the x, Java thinks that mx is a variable all on its own. Then we have plus b, or to make it a little more meaningful we might write y is equal to slope times x plus yIntercept.
Another type of expression is a Boolean expression. Bool, which is short for Boolean, is a primitive data type and is used frequently in decision statements and as controls for loops. You might have noticed that we don't have math symbols for some other common math operations, such as square root or powers. Some languages use the caret symbol to represent power, such as two caret three would be equal to eight. Because that would represent two cubed. Since Java does not have the same symbols for these equations, we must use the math class that is included in the Java API.
Take a minute to go out to the Oracle site and search for the math class to see all the different methods available to use when you include the math API, such as min and max, absolute value, square root, power, et cetera.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs