Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

 

2.5 Details of Expressions

 

This section takes a closer look at expressions. Recall that an expression is a piece of program code that represents or computes a value. An expression can be a literal, a variable, a function call, or several of these things combined with operators such as + and >. The value of an expression can be assigned to a variable, used as a parameter in a subroutine call, or combined with other values into a more complicated expression.

 

Chapter 2.5 - Details of Expressions | Introduction to Programming Using Java

 

(The value can even, in some cases, be ignored, if that’s what you want to do; this is more common than you might think.) Expressions are an essential part of programming. So far, these notes have dealt only informally with expressions. This section tells you the more-or-less complete story (leaving out some of the less commonly used operators).

The basic building blocks of expressions are literals (such as 674, 3.14, true, and ’X’), variables, and function calls. Recall that a function is a subroutine that returns a value. You’ve already seen some examples of functions, such as the input routines from the TextIO class and the mathematical functions from the Math class.

 

Chapter 2.5 - Details of Expressions | Introduction to Programming Using Java

 

The Math class also contains a couple of mathematical constants that are useful in mathematical expressions: Math.PI represents π (the ratio of the circumference of a circle to its diameter), and Math.E represents e (the base of the natural logarithms). These “constants” are actually member variables in Math of type double. They are only approximations for the mathematical constants, which would require an infinite number of digits to specify exactly.

Literals, variables, and function calls are simple expressions. More complex expressions can be built up by using operators to combine simpler expressions. Operators include + for adding two numbers, > for comparing two values, and so on. When several operators appear in an expression, there is a question of precedence, which determines how the operators are grouped for evaluation. For example, in the expression “A + B * C”, B*C is computed first and then the result is added to A.

 

Chapter 2.5 - Details of Expressions | Introduction to Programming Using Java

 

We say that multiplication (*) has higher precedence than addition (+). If the default precedence is not what you want, you can use parentheses to explicitly specify the grouping you want. For example, you could use “(A + B) * C” if you want to add A to B first and then multiply the result by C.

The rest of this section gives details of operators in Java. The number of operators in Java is quite large, and I will not cover them all here. Most of the important ones are here; a few will be covered in later chapters as they become relevant.

 

details of expressions

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.2.1 – Variables | Introduction to Programming Using Java

Chapter 2.3.3 – Introduction to Enums | Introduction to Programming Using Java

Chapter 2.4.4 – Formatted Output | Introduction to Programming Using Java

3 thoughts on “Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java”

Leave a Comment