Every programming language have some constructs that forms its basis. From low level programming language like Assembly to high level language like Java or .Net there are certain key elements which makes up the language unique and approachable. Expressions are one of that basic stuff.

An expression is a construct that evaluates a single value and is made up of variables, operators, and method invocations built according to the syntax of the language. If we want to execute a computation, frame a condition, or do anything else in a programming language, we employ a set of symbols to do it. An expression is made up of these symbols and are typically used to generate new values and can assign a value to a variable.

Expressions are crucial components of any Java program and I can safely say that every project that has been created with Java language cannot be built without them.

## Java Expression

Variables, operators, literals, and method calls make up a Java expression. Operands and operators are used to build expressions. An expression’s operators specify which operations should be applied to the operands. The precedence and associativity of the operators govern the order in which they are evaluated in an expression.

**Take an example:**

int marks; marks =95; Here, marks=90 is an expression that returns an int value. double a = 3.2, b = 3.4, result; result = a + b - 3.4; Here, a+b-3.4 is an expression if (num1 == num2) System.out.println("num 1 is larger than num 2");

Here, num1 == num2 is an expression that returns a Boolean value. Similarly, “num 1 is larger than num 2” is a string expression.

## How to Evaluate Java Expressions?

Even though Java has its method for evaluating expressions behind the scenes, a Java expression’s outcome and its corresponding arithmetic expression are identical. As a result, you can use the arithmetic rule to evaluate a Java expression confidently.

### Types of Expressions

While an expression typically yields a result, this is not always the case. In Java, there are a few types of expressions:

**Those that generate a value, i.e.**

(1 + 1) result

**Those who, for example, assign a variable**

(i= 10)

Because an expression can comprise a wide range of elements such as method invocations or increment operators that affect the state (i.e., memory), those that have no result but may have a “side effect.”

## Example of Expressions in Java

Here are the examples of different types of java expressions;

### Expressions that produce a value

A wide range of Java arithmetic, comparison, and conditional operators are used in expressions that yield a value. +, *, /, >, ++, and percent are examples of arithmetic operators. The conditional operators? and ||, as and comparison operators,=, and > are examples of conditional operators. A complete list can be found in the Java specification.

**Some of these expressions that produce a value are:**

1/2 6% 3 pi + (10 * 2)

In the last expression, take note of the parenthesis. It instructs Java to compute the value of the expression within the parenthesis first (much like the mathematics you learned in school), then finishes the computation.

### Expressions that Assign a Variable

This program has a lot of expressions (shown in bold italics) that each assigns a value to something.

int secInOneDay = 0; int totalDaysInOneWeek = 7; int hrsInOneDay = 24; int minsInAnHour = 60; int secInOneMin = 60; boolean calWeek = true; secInOneDay = secInOneMinute * minsInAnHour * hrsInOneDay; // 7 System.out.println("The number of seconds in a day is" + second Sunday); if ( calculateWeek == true) { System.out.println("The number of seconds in a week is "+ second Sunday * daysInWeek); }

The assignment operator is used in all of the expressions in the first six lines of the code above. It assigns the value on the right to the variable on the left.

**Also Read:** Functional Interfaces In Java

The line represented by //7 is an expression that can be used as a statement on its own. It also demonstrates how expressions may be constructed using multiple operators. The result of evaluating each statement in turn (i.e., secInOneMinute * minsInAnHour = 3600, followed by 3600 * hrsInOneDay = 86400) is the ultimate value of the variable secInOneDay.

### Java Expression with No Results

While some expressions have no effect, they can have a side effect when an expression alters the value of any of its operands.

Certain operators, such as the assignment, increment, and decrement operators, always cause a side effect. Consider the following:

intproduct =y*z;

The product is the only variable that changes in this equation; y and z remain unchanged. This one is known as a “side effect.”

## Conclusion

With above examples we can clearly depict that Java programs constitute expressions and a good understanding of it is necessary for any Java programmer who wants to excel in the field. In general, the rules for expressions are similar to the rules defined in Algebra (Mathematics) and have been derived from its principles.