### Expressions and Operators

• Expressions

• Expression:

• Expression = a combination of one or more operators and operands that performs a computation.

• Operators can be any of the following:

 +,   −,   ×,   /,   %,   ++,   −−,   =,   +=,   −=,   .... &&,   ||,   !,   &,   |,   ~,   << , ....

• Operands can be any of the following:

 A constant (value)            A variable An expression

• Types of expressions in Java (and many programming languages):

 Arithmetic expressions (result is a numeric value) Boolean expressions (result is a logical value (true/false)) String expressions (result is a String)

• Arithmetic expression

• Example:

 ``` score = score - 10 * lateDays ; ```

This expression comprises of 3 sub-expressions:

• The most inner expression "10 * lateDays"

• The expression at the next level is "score (first-expr)"

• The top-level expression is "score = (second-expr)"

(This expression is called an assignment expression because the operator is the assignment operator)

Important note:

• = is an operator

• The evaluation of the expression x = y will result in:

 Storing the value of y into the variable x Returns the value y

• Boolean expressions

• Example:

 ``` boolean isLate; isLate = submissionDate < dueDate ; ```

• Arithmetic operators

• Arithmetic operators:

Operator Meaning Example Result
+ addition 1 + 2 3
subtration 1 2 −1
* multiplication 3 * 2 6
/ division 7 / 4
7.0 / 4.0
1
1.75
% remainder 7 / 4 3

• Pretest question

• If a and b are int's such that b ≠ 0, then which of the following expression is always equivalent to a%b (remainder)

 a - (a/b)*b (a/b)*b a - a/b (double)a/b - a/b

• To "guess" the solution, use an concrete example (because it must work for all value, it must work for this one too !)

 ``` Let: a = 7, b = 4 a % b = 3 1. a - (a/b)*b = 7 - (7/4)*4 = 7 - 1*4 = 3 possible 2. (a/b)*b = (7/4)*4 = 1*4 = 4 no 3. a - a/b = 7 - 7/4 = 7 - 1 = 6 no 4. (double)a/b - a/b = (double)7/4 - 7/4 = 7.0/4 - 7/4 = 1.75 - 1 = 0.75 no ```

• a/b is the number of times that b will "fit" in a

• a - (a/b)*b is do this operation:

 take away the number of whole numbers of b from a; what remains is the part that does not fit in b

• "Shortcut" assignment operators:

Operator Example Equivalent
+= i += 8 i = (i + 8)
−= i −= 8 i = (i − 8)
×= i ×= 8 i = (i × 8)
/= i /= 8 i = (i / 8)
%= i %= 8 i = (i % 8)

• Increment/decrement operators:

Operator Name Effect
var++   post-increment   Return the (original) value and afterwards increment the variable
var--   post-idecrement   Return the (original) value and afterwards decrement the variable

++var   pre-increment   Increment the variable first, and return the new value
--var   pre-decrement   Decrement the variable first, and return the new value

Examples:

 ``` x = 4; y = x++; <===> y = x; x++; (POST increment) Result: x = 5 y = 4 x = 4; y = x--; <===> y = x; x--; (POST decrement) Result: x = 3 y = 4 x = 4; y = ++x; <===> x++; y = x; (PRE increment) Result: x = 5 y = 5 x = 4; y = --x; <===> x--; y = x; (PRE decrement) Result: x = 3 y = 3 ```

• Boolean expressions

• Boolean expression:

 Boolean expression = an expression (formula) that evaluates (computes) to a Boolean (logical) value

• Examples:

 ``` score > 60 age <= 65 ```

• Compare operators (produces Boolean values)

• Compare operators:

Operater Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to

• Difference between == and equals()

• 2 ways to compare objects:

 ``` String s1, s2; // 2 string reference variables s1 = "Hello"; s2 = "Hell" + "o"; Method 1: if ( s1 == s2 ) .... Method 2: if ( s1.equals(s2) ) .... ```

• The == operator:

• The == compare operator compares references

 Check whether one reference typed variable contains the same address as another reference typed variable

• The equals() method:

• The expression s1.equals(s2) will invoke the method equals() to compare the 2 objects s1 and s2

• The programmer can decide the criteria that determine whether the 2 objects are equal !!!

(Very flexible !!)

• In case of Strings, the 2 string objects are equal when all characters are same

• If a class does not have an equal() method defined, the following default method will be used:

 ``` public boolean equals( Object obj ) { return ( this == obj ); // Same address } ```

This method is built-in into the Object class provided by Java.

• Example:

 ``` String s1 = "Hello"; // s1 = "Hello" String s2 = "Hello"; // s2 = "Hello" String x = "Hell"; String y = "o"; String s3 = x + y; // s3 = "Hello" if ( s1 == s2 ) System.out.println("s1 == s2"); else System.out.println("Not: s1 == s2"); if ( s1 == s3 ) // FALSE !!! System.out.println("s1 == s3"); else System.out.println("Not: s1 == s3"); System.out.println(); if ( s1.equals( s2 ) ) System.out.println("s1.equals( s2 )"); else System.out.println("Not: s1.equals( s2 )"); if ( s1.equals( s3 ) ) System.out.println("s1.equals( s3 )"); else System.out.println("Not: s1.equals( s3 )"); ```

Output:

 ``` s1 = Hello s2 = Hello s3 = Hello s1 == s2 Not: s1 == s3 // s3 is a different object with the SAME content !!! s1.equals( s2 ) s1.equals( s3 ) ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac Equals.java To run:          java Equals

• Boolean operators

• What are Boolean operators:

 Boolean operators manipulate logical values

• Boolean operators:

Operator Meaning
&& logical AND
|| logical OR
! logical NOT