# Expressions, Literals, and Constants

### Expressions

Recall that we assign a "value" to a variable with the assignment operator "=", in the following way:

myVariableName = <some value or expression>;


An expression in Java is a combination of one or more operators (+,‐,*, /, %, &&, ||, and many more…) and operands that result in some calculation being performed. The operands might be numbers, but can also represent other things (characters, strings, boolean values, etc…).

Most of the arithmetic operators behave like you think they should:

int x = 2 + 3; // assigns x the value of 5
int x = 2 – 3; // assigns x the value of -1
int x = 2 * 3; // assigns x the value of 6


Sometimes, an operator can do different things, depending on the type of the operands on which it operates:

In the first case below, the “/” operator is operating on two floating‐point values, 12.0 and 5.0. In this case, normal division is applied. In the second case, however, the “/” operator is operating on two integer values. Here, the result in the integer number of times the denominator goes into the numerator.

double x = 17.0 / 5.0; //x = 3.2
double x = 17 / 5;     //x = 3.0


The “+” operator can be used to add numeric values, like the first case below, or concatenate (i.e., “jam together”) strings, as the second example suggests.

int x = 2 + 3;                  //x = 5
String s = "Hello " + "Bob";    //s = "Hello Bob"


Some operators may be new to you. For example, the remainder operator, “%” (sometimes called “mod”) gives the remainder of some number upon division by another. This operator also has two different contexts: one for integers and one for floating point numbers (floats and doubles), as seen in the two examples below:

 int x = 17 % 5;     //assigns x the value of 2
//note: 17 = 5 * 3 + 2

double x = 17.4 % 5.1; //assigns x the value of 2.1
//note: 17.4 = 5.1 * 3 + 2.1


Be careful when translating a pretty‐looking algebraic expression you might find in a math textbook into a Java expression, which must be typed on a single line. For example:

$${3+4x \over 5} - {10(y-5)(a+b+c) \over x} + 9\left( {4 \over x} + {9+x \over y} \right)$$

would be written as

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x+(9+x)/y)


Expressions are evaluated according to a strict order of operations. The standard "parentheses first, multiplication & division (left‐to‐right) next, followed by addition & subtraction (left-to‐right)" used in high school algebra serves as a starting point, as shown below, but we will eventually need to consider many more operators and where they fall in terms of precedence.

3 + 4 * 4 + 5 * (4 + 3) – 1 innermost parentheses 1st
3 + 4 * 4 + 5 * 7 – 1 leftmost mult/div
3 + 16 + 5 * 7 – 1 leftmost mult/div
3 + 16 + 35 – 1 leftmost add/subtract
19 + 35 – 1 leftmost add/subtract
53


### Literals

Constant values that appear directly in a program, like the values 2, 3, 5, 17.4,
and 5.1 above are called literals.

• Literals need not represent numeric values. For example, “Hello World!” is a String literal.
• When an integer literal appears in code, it is assumed to be of type int, unless we tell the program otherwise.
• We can tell a program to treat a numeric literal value as a long type by giving it a suffix of “l” or “L”. (It is better to write 2012L instead of 2012l, as the lower‐case “l” looks a lot like the number one, “1”.)
• When a decimal point appears in the literal, it is assumed to be of type double, unless we tell the compiler otherwise.
• We can tell a program to treat a numeric literal value as a float, by giving it a suffix of “f” or “F”. (So, for example 3.14F is a float)
• We can tell a program to treat a numeric literal value as a double, by giving it a suffix of “d” or “D”. (So for example 3D is a double whose value is 3.0)

### Constants

Similar to literals, constants represent permanent values (or data) that never change during the execution of a program. The difference is that constants are given names, just like variables are.

A constant must be declared and initialized in the same statement using the modifier "final", following the syntax below:

final <dataType> <CONSTANT_NAME> = <value>;


Examples:

final double PI = 3.14159;
final int SCREEN_HEIGHT = 600;


The name of a constant, by convention, should all be in uppercase, with words separated by underscores.

Once you have declared and initialized a constant, you can then use it much like a variable – except you can't change its value.