Expressions

Operators

Expressions

🙋 Need help? Ask an expert now!

An expression is a mixture of literals, operators, variable names, and parentheses used to calculate a value. Please keep in mind that in Java, the expression on the right side of the assignment statement is evaluated first. The expressions will look similar to regular mathematical expressions from math class. Please take a look at the expressions below.

ExpressionsExample.java
package exlcode;
            
            public class ExpressionsExample {
            
              public static int exampleVariableOne = ((7-4) * (-3/-1));
            
              public static void main(String[] args) {
                System.out.println(exampleVariableOne);
              }
            }

Expressions can be written without using any spaces at all, but the use of one or more spaces to visually separate the parts without changing the meaning is useful for the programmer and reader.

Keep in mind the discussion we had previously on division of integers. If the operands are integers, these operators will perform integer arithmetic. If one or both operands are floating points, the operators will perform floating point arithmetic. Any number with a decimal would result in a double or float. For integers, 5/2 results in 2. For floating point, 5.0/2.0 results in 2.5 and 5/2.0 results in 2.5.

Edit Me on GitHub!

Application Question

Consider the following code segment:

double varOne;
                            int varTwo = 56;
                            int varThree = 25;
                            varOne = varTwo / varThree;
                            System.out.println(varOne);
                            

What is printed as a result of executing this code segment?