Operators and Expressions PDF

Summary

This document provides an overview of operators in Java, including arithmetic, assignment, and conditional operators. It also covers different types of operators, such as unary operators and comparison operators.

Full Transcript

IT2402 Operators and Expressions Operators (Agarwal & Bansal, 2023) Operators are symbols used to compute and compare values and test multiple conditions. Java supports several categories of operators. Arithmetic Operators All mathematical operations are perfor...

IT2402 Operators and Expressions Operators (Agarwal & Bansal, 2023) Operators are symbols used to compute and compare values and test multiple conditions. Java supports several categories of operators. Arithmetic Operators All mathematical operations are performed using operators in this category. An operand is the value to be computed. Name Operator Description Example Addition + Adds an operand 𝑥 =𝑦+𝑧 Subtraction - Substracts the right operand from the left operand 𝑥 =𝑦−𝑧 Multiplication * Multiples the operands 𝑥 =𝑦∗3 Division / Divides the left operand by the right operance 𝑥 = 𝑦/𝑧 Modulus % Calculates the remainder of an integer division 𝑥 = 𝑦%𝑧 Table 1. Arithmetic operators. The + Operator with Numeric Data Types Adding operands of the primitive numeric data type results in a primitive numeric data type. Byte var1 = 50; var3 results to 170. Since the byte data type can hold a range of -128 Byte var2 = 120; to +127, Java automatically converts any calculations of numeric data Byte var3 = var1+var2; results to the int data type. The + Operator with the String Data Type If both operands are strings, the + operator joins them to display as a single output. If one of the operands is a string object, the second operand is converted into a string before joining them. For example, Result = operand1 + operand2 operand1 operand2 Result 7 3 10 2 "abc" 2𝑎𝑏𝑐 "abc" "xyz" "abcxyz" Table 2. Operands for string data type. Assignment Operators An assignment operator assigns the specific value, variable, and/or functions to another variable. Name Operator Description Example Assigns the value of the right operand to the left Assignment = 𝑥=𝑦 operand Adds the operands and assigns the result to the left Add and Assignment += 𝑥+= 𝑦 operand Subtracts the right operand from the left operand Subtract and Assignment -= 𝑥−= 𝑦 and store the result in the left operand Multiplies the operand by the right operand and Multiple and Assignment *= 𝑥 ∗= 𝑧 stores the result in the left operand Divides the left operand by the right operand and Divide and Assignment /= 𝑥/= 𝑧 stores the result in the left operand 04 Handout 1 *Property of STI  [email protected] Page 1 of 5 IT2402 Modulus and Divides the left operand by the right operand and %= 𝑥% = 𝑧 Assignment stores the remainder in the left operand Table 3. Assignment operators. The operator used in x operator y can be represented as x = x operator y. Thus, x += y is equivalent to x = x += y. It applies to every operator above. Assigning values to more than one (1) variable at the same time is also allowed, such as x = y = 0; Unary Operators Compared to previous operators, this operator requires only one operand. Name Operator Description Example Unary Plus + Represents the positive value +𝑎 Unary Minus - Represents the negative value −𝑎 Increment ++ Increases the value of the operand by 1 𝑥++ Decrement -- Decreases the value of the operand by 1 𝑥−− Logical Complement ! Inverts the value of a Boolean variable ! 𝑡𝑟𝑢𝑒 Table 4. Unary operators. The increment/decrement operator can be used in two (2) ways: o As a prefix or pre-increment/decrement wherein it precedes the variable: ++invar; o As a suffix or post-increment/decrement wherein it follows the variable: invar--; For pre-increment: Suppose x = 7, then the value of ++x will be 8. For post-decrement: Suppose x = 12, then the value of x- - will be 11. Comparison Operators All mathematical operations involving comparison between values are performed using comparison operators. It is also often called a relational operators. Name Operator Description Example Equal to == Evaluates whether the operands are equal 𝑥 == 𝑦 Not Equal to != Evaluates whether the operands are not equal 𝑥! = 𝑦 Evaluates whether the left operand is greater than the Greater Than > 𝑥>𝑦 right operand Evaluates whether the left operand is less than the right Less Than < 𝑥= 𝑥 >= 𝑦 Equal to equal to the right operand Less Than or Equal Evaluates whether the left operand is less than or equal 𝑥=9≫2 number specified as the value for the second operand. 04 Handout 1 *Property of STI  [email protected] Page 2 of 5 IT2402 Left Shift >> 𝑥 = −10 ≫> 3 Shift replaces the bits that are shifted. Table 6. Shift operators. Zeros (0) are used as fillings when shifting in whichever direction. Shifting Positive Numbers The int data type occupies four (4) bytes in the memory. The rightmost eight bits of the number 9 are represented in binary as: 0 0 0 0 1 0 0 1 For the right shift, the bits of the left operand are shifted right. Using 𝑥 = 9 ≫ 2, the result becomes 2: 0 0 0 0 0 0 1 0 For the left shift, the bits of the left operand are shifted left. Using 𝑥 = 9 ≪ 2, the result becomes 40: 0 0 1 0 1 0 0 0 Bitwise Operators Compared to previous operators, bitwise operators perform operations bit-by-bit. These operators can be applied to byte, int, short, long, and char data types. Name Operator Description Example AND results in a 1 if both the bits are 1. Any other AND & 𝑥&𝑦 combination results in 0. OR results in a 0 when both the bits are 0. Any other OR | 𝑥|𝑦 combination results in 1. XOR results in a 0, if both the bits are of the same value XOR ^ 𝑥^𝑦 and 1 if the bits have different values. Inversion ~ Converts all 1 bits to 0’s and all 0 bits to 1’s ~𝑥 Table 7. Bitwise operators. When used with Boolean, x and y are integers and can be replaced with expressions that give a true or false result such as when both the expressions evaluate to true, the result of using & is true. Otherwise, false. Logical Operators These are used to combine the results of Boolean expressions. These operators share a likeness with bitwise operators, but logical operators are limited to Boolean expressions only. Name Operator Description Example Logical If both the operands are non-zero, the expression && 𝑥 > 4&&𝑦 < 8 AND returns true; otherwise, it returns false. If one or both the operands are non-zero, the expression Logical OR || 𝑥 > 4||𝑦 < 8 returns true; otherwise, it returns FALSE. Table 8. Logical operators. For the example of Logical AND, the result is true if the first condition (x>4) and the second condition (y>8) are both true. If any of the conditions are false, the result is false. 04 Handout 1 *Property of STI  [email protected] Page 3 of 5 IT2402 Conditional Operator This operator is used to control the flow of the program. The ternary operator is used mainly in loop statements. Name Operator Description Example (condition)? Evaluates val1 if the condition returns true and val2 Ternary 𝑥 = (𝑦 > 𝑧)? 𝑦: 𝑧 val1:val2 if the condition returns false. Table 9. Conditional operators. To explain the example, x is assigned the value of y if it is greater than z; otherwise, x is assigned the value of z. Order of Precedence of Operators Order of Precedence shows the hierarchy wherein an operation with high precedence is performed before the others. The table shows those with the same level of precedence are listed in the same row. Category Operators High Precedence [], () Unary +, -, ~, !, ++, -- Multiplicative *, /, % Additive +, - Shift , >>> Relational Equality =, ==, != Bitwise &, ^, | Logical &&, || Conditional ?: Assignment =, +=, -=, *=, !=, %= Table 10. Order of precedence. Expressions (Agarwal & Bansal, 2023) An expression is a combination of variables, constants, literals, and operators to produce a single value. A simple 23 + 16 and x = -82 are considered expressions. In Java, an expression statement consists of an expression followed by a semicolon (;) to evaluate an expression and disregard the result. int a = 10; System.out.println("The value of a is " + a); Each line in this code snippet is considered an expression statement. Here are several types of expressions in Java. Arithmetic Expression It is an expression that returns a numeric value based on the operators and operands used. For example: int x = 10; The last line is an arithmetic expression. It adds the value of x and y and int y = 15; int z = x + y; assigns the result to the z. 04 Handout 1 *Property of STI  [email protected] Page 4 of 5 IT2402 Assignment Expression It is an expression that involves assigning a value to a variable. For example: int a = 8; The last line is an assignment expression that adds 4 to the value of a and a = a + 4; assigns the result back to a. Relational Expression It is an expression that compares values using relational operators. For example: int x = 6; The last line is a relation expression comparing the values of x and y and int y = 8; boolean result = x < y; assigning the result to the variable result. Logical Expression It is an expression involving logical operators. For example: int j = 5; The last line is a logical expression that checks whether j is less int k = 4; than k and whether k is less than 4. The result is assigned to the boolean result = (j

Use Quizgecko on...
Browser
Browser