Java Operators, Expressions, and Statements, and Memory Management
Introduction to Java Operators and Expressions
- To write any type of Java program, certain components are required beyond values and data types, specifically operators and expressions.
- Classes form the basis of most computations in Java. Computations are performed within a Class using operators and expressions.
- An operator is defined as a symbol or letter that causes the compiler to take an action and yield a value.
- Operators act on different data items or entities referred to as Operands.
- An expression example is x+y, where the symbol + is the operator specifying the operation of addition.
- Categories of Java operators include:
- Arithmetic Operators
- Increment/Decrement Operators
- Relational Operators
- Conditional Operators
- Logical Operators
Arithmetical Operators
- Arithmetical operators are used for various mathematical calculations.
- The result of an arithmetical expression is always a numerical value.
- An arithmetical expression can be a variable, a constant, or a combination of both, connected via arithmetical operators.
- Arithmetical operators are categorized into two types:
- Unary Arithmetic Operators
- Binary Arithmetic Operators
Unary Arithmetic Operators
- A unary operator requires only one operand.
- Common unary operators include + (positive) and −(negative).
- Examples: −a, +b.
- Unary +: Makes an expression positive. For instance, if x=+10, the value of x is positive. These are equivalent: x=10, x=+10, and +x=10.
- Unary -: Makes an expression negative. For instance, if x=−10, the value of x is negative. These are equivalent: x=−10 and −x=10 (implies the negated value is 10).
Binary Arithmetic Operators
- Binary arithmetic operators act upon two operands.
- The following table lists binary operators:
- Addition (+): Adds two values. Example: if a=20 and b=5, then a+b=25.
- Subtraction (-): Subtracts the second value from the first. Example: if a=21 and b=9, then a−b=12.
- Multiplication (*): Multiplies the values. Example: if a=9 and b=7, then a×b=63.
- Division (/): Divides the first operand by the second to obtain the quotient. Example: if a=85 and b=5, then a/b=17.
- Modulus (%): Finds the remainder when the first operand is divided by the second. Example: if a=37 and b=5, then a%b=2 (since 37 divided by 5 leaves a remainder of 2).
Distinction Summaries
- Unary vs. Binary: Unary operators act on a single operand (e.g., pre-increment or post-increment), while binary operators require two operands (e.g., mathematical or relational operators).
- Division (/) vs. Modulus (%): The division operator calculates the quotient, whereas the modulus operator calculates the remainder of a division.
Increment and Decrement Operators
- Java provides specialized operators ++ and −, which are not found in many other languages.
- The increment operator (++) adds 1 to its operand: ++x or x++ means x=x+1.
- The decrement operator (−−) subtracts 1 from its operand: −−x or x−− means x=x−1.
- These operators exist in two forms: Prefix and Postfix.
Prefix Increment and Decrement
- Rule: FIRST CHANGE AND THEN USE.
- The value of the operand is changed first, and the new value is then used in the expression.
- Example 1: i=++i×8 where initial i=8.
- Initial value: 8
- Incremented value: 9
- Calculation: 9×8=72
- Result: 72; Final value of i=72
- Example 2: a=b×−−a where initial a=5 and b=6.
- Initial value of a: 5
- Decremented value of a: 4
- Calculation: 6×4=24
- Result: 24
Postfix Increment and Decrement
- Rule: FIRST USE AND THEN CHANGE.
- The expression uses the current value first, then increments or decrements the operand.
- Example 1: i=i++×9 where initial i=9.
- Initial value: 9
- First use: 9×9=81
- Then increment i to 10.
- Result assigned to i: 81
- Example 2: i=j×i−− where initial i=10 and j=5.
- Initial value of i: 10
- First use: 5×10=50
- Then decrement i to 9.
- Result: 50
Operator Specifics
- The ++ and −-$ operators operate strictly on a single operand.
- Precedence: Postfix operators have higher precedence over prefix operators. Thus, a++ has higher precedence than ++a, and a−− has higher precedence than −−a.
- Distinction: The prefix form returns the value of the increment operation itself. The postfix form returns the current value of the expression before the operation and then performs the increment on the variable.
Relational Operators
- Relational operators determine the relationship or comparison between operands.
- A relational expression returns a boolean-style result: 1 (true) or 0 (false).
- There are six relational operators:
- **Greater than (>):** Returns true if the left operand is larger.\n 2. **Less than (
- Example:
tot_increase = tot_increase + increase; to calculate total salary increases of all employees.
Standard Output Methods
- System.out.print(): Prints text to the monitor and leaves the cursor on the same line.
- System.out.println(): Prints text and then moves the cursor to the beginning of the next line.
- System.out.format(): A different method used for displaying formatted data.
- Visual Difference:
- Two
print() calls: Hello and World results in Hello World. - Two
println() calls: Hello and World results in:
HelloWorld