Unit 1: Primitive Data Types
Operators -> The Building Blocks of Java Logic
Definition and Purpose of Operators
Definition
Operators are the building blocks of Java logic. They are used to perform operations on variables and values. Operators are used to manipulate data and form expressions. Operators are used to perform arithmetic, relational, logical, and bitwise operations.
Purpose
Operators are used to assign values to variables. Operators are used to convert data types. Operators are used to cast data types. Operators are used to shift bits. Operators are used to compare objects. Operators are used to perform a variety of other operations. In short, operators are used to perform a variety of operations on variables and values.
Categories of operators
There are several categories of operators in Java. The following table shows the categories of operators in Java.
Categories of Operators
Category | Operators |
---|---|
Arithmetic | +, -, *, /, % |
Relational | ==, !=, >, <, >=, <= |
Logical | &&, ||, ! |
Bitwise | &, |, ^, ~, <<, >>, >>> |
Assignment | =, +=, -=, *=, /=, %= |
Unary | +, -, ++, -- |
Conditional | ? : |
instanceof | instanceof |
Arithmetic Operators
Artithmetic operators are used to perform arithmetic operations on variables and values.
Arithmetic Operators
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Addition +
int a = 5;
int b = 10;
int c = a + b;
System.out.println(c); // 15
Subtraction -
int a = 5;
int b = 10;
int c = a - b;
System.out.println(c); // -5
Multiplication \
int a = 5;
int b = 10;
int c = a * b;
System.out.println(c); // 50
Division /
int a = 5;
int b = 10;
int c = a / b;
System.out.println(c); // 0
Modulus %
int a = 5;
int b = 10;
int c = a % b;
System.out.println(c); // 5
Relational Operators
Relational operators are used to compare two values and return a boolean value (true or false). The following table shows the relational operators in Java.
Relational Operators
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Equal to =
int a = 5;
int b = 10;
boolean c = a == b;
System.out.println(c); // false
Not equal to !
int a = 5;
int b = 10;
boolean c = a != b;
System.out.println(c); // true
Greater than >
int a = 5;
int b = 10;
boolean c = a > b;
System.out.println(c); // false
Less than <
int a = 5;
int b = 10;
boolean c = a < b;
System.out.println(c); // true
Greater than or equal to >
int a = 5;
int b = 10;
boolean c = a >= b;
System.out.println(c); // false
Less than or equal to <
int a = 5;
int b = 10;
boolean c = a <= b;
System.out.println(c); // true
Logical Operators
Logical operators are used to perform logical operations on variables and values.
Logical Operators
Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Logical AND &
int a = 5;
int b = 10;
boolean c = a > 0 && b > 0;
System.out.println(c); // true
Logical OR |
int a = 5;
int b = 10;
boolean c = a > 0 || b > 0;
System.out.println(c); // true
Logical NOT !
int a = 5;
int b = 10;
boolean c = !(a > 0 && b > 0);
System.out.println(c); // false
Unary Operators
Unary operators are used to perform unary operations on variables and values.
Unary Operators
Operator | Description |
---|---|
+ | Unary plus |
- | Unary minus |
++ | Increment |
-- | Decrement |
Unary plus +
int a = 5;
int b = +a;
System.out.println(b); // 5
Unary minus -
int a = 5;
int b = -a;
System.out.println(b); // -5
Increment +
int a = 5;
a++;
System.out.println(a); // 6
Decrement -
int a = 5;
a--;
System.out.println(a); // 4
Assignment Operators
Assignment operators are used to assign values to variables.
Assignment Operators
Operator | Description |
---|---|
= | Simple assignment |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Simple assignment =
int a = 5;
System.out.println(a); // 5
Add and assign +
int a = 5;
a += 5;
System.out.println(a); // 10
Subtract and assign -
int a = 5;
a -= 5;
System.out.println(a); // 0
Multiply and assign \
int a = 5;
a *= 5;
System.out.println(a); // 25
Divide and assign /
int a = 5;
a /= 5;
System.out.println(a); // 1
Modulus and assign %
int a = 5;
a %= 5;
System.out.println(a); // 0
Bitwise and Bit Shift Operators
(Not heavily emphasized in AP CS A, but good for a comprehensive understanding)
Bitwise and bit shift operators are used to perform bitwise and bit shift operations on variables and values.
Bitwise and Bit Shift Operators
Operator | Description |
---|---|
&& | AND |
|| | OR |
^ | XOR |
~ | NOT |
<< | Left shift |
>> | Right shift |
>>> | Unsigned right shift |
Bitwise AND &
Bitwise AND &&
The && operator returns true if both operands are true; otherwise, it returns false.
int age = 19;
int height = 6;
// true && true
if (age >= 18 && height >= 6) {
System.out.println("You are eligible to ride the roller coaster.");
} else {
System.out.println("You are not eligible to ride the roller coaster.");
}
Bitwise OR |
Bitwise OR ||
The || operator returns true if either operand is true; otherwise, it returns false.
int age = 19;
int height = 4;
// if either returns true, the whole expression returns true
if (age >= 18 || height >= 6) {
System.out.println("You are eligible to ride the roller coaster.");
} else {
System.out.println("You are not eligible to ride the roller coaster.");
}
Bitwise XOR ^
Bitwise XOR ^
The ^ operator returns true if either operand is true, but not both; otherwise, it returns false.
int age = 19;
int height = 4;
// if either returns true, the whole expression returns true
if (age >= 18 ^ height >= 6) {
System.out.println("You are eligible to ride the roller coaster.");
} else {
System.out.println("You are not eligible to ride the roller coaster.");
}
Bitwise NOT ~
Bitwise NOT ~
The bitwise NOT operator, represented by the tilde (~) symbol is a unary operator that inverts the bits of its operand. When applied to an integer, every bit that was 0 becomes 1, and every bit that was 1 becomes 0.
int a = 5;
int b = ~a;
System.out.println(b); // -6
Left shift <
Left shift <<
The left shift operator, represented by the << symbol is a binary operator that shifts the bits of its first operand to the left by the number of places specified by the second operand. The leftmost bits are discarded, and the rightmost bits are filled with zeros.
int a = 5;
int b = a << 1;
System.out.println(b); // 10
Right shift >
Right shift >>
The right shift operator, represented by the >> symbol is a binary operator that shifts the bits of its first operand to the right by the number of places specified by the second operand. The rightmost bits are discarded, and the leftmost bits are filled with the sign bit (0 for positive numbers and 1 for negative numbers).
int a = 5;
int b = a >> 1;
System.out.println(b); // 2
Unsigned right shift >
Unsigned right shift >>>
The unsigned right shift operator, represented by the >>> symbol is a binary operator that shifts the bits of its first operand to the right by the number of places specified by the second operand. The rightmost bits are discarded, and the leftmost bits are filled with zeros.
int a = 5;
int b = a >>> 1;
System.out.println(b); // 2
Conditional Operator (Ternary Operator ? :)
Conditional Operator ? :
The conditional operator, represented by the ? : symbol is a ternary operator that evaluates a boolean expression and returns one of two values, depending on the result of the evaluation. If the expression evaluates to true, the first value is returned; otherwise, the second value is returned.
Used for conditional assignment
// Can drink alcohol if age is greater than or equal to 21
int age = 19;
String canDrinkAlcohol = age >= 21 ? "Yes" : "No";
System.out.println(canDrinkAlcohol); // No
instanceof Operator
instanceof Operator
The instance of is used for type comparison. The instanceof operator is a binary operator that returns true if the first operand is an instance of the second operand; otherwise, it returns false.
class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);//true
}
}
Operator Precedence and Associativity
Precendence and associativity are used to determine the order in which operators are evaluated.
Order in which operators are evaluated
Operator Precedence and Associativity
The order in which operators are evaluated is determined by operator precedence and associativity. The following table shows the order in which operators are evaluated.
Operator | Description |
---|---|
() | Parentheses |
++, -- | Postfix increment and decrement |
++, -- | Prefix increment and decrement |
+, - | Unary plus and minus |
! | Logical NOT |
~ | Bitwise NOT |
(type) | Type cast |
* / % | Multiplication, division, and modulus |
+, - | Addition and subtraction |
<<, >>, >>> | Bit shift |
<, <=, >, >= | Relational |
==, != | Equality |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
&& | Logical AND |
|| | Logical OR |
?: | Ternary |
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= | Assignment |
Parentheses and their effect on operation order
Parentheses are used to group expressions and override the default order of operations. Expressions within parentheses are evaluated first.
int a = 5;
int b = 10;
int c = 15;
int d = a + b * c;
int e = (a + b) * c;
System.out.println(d); // 155
System.out.println(e); // 225
Common Pitfalls and Best Practices
Some common pitfalls and best practices to keep in mind when working with operators in Java are:
- distinguishing between = (assignment) and == (equality)
- importance of parentheses in complex expressions to avoid ambiguity
- potential issues with integer division
Distinguishing between = (assignment) and == (equality)
Distinguishing between = (assignment) and == (equality)
The = operator is used to assign a value to a variable. The == operator is used to compare two values. The following example illustrates the difference between the two operators.
int a = 5;
int b = 10;
boolean c = a == b;
System.out.println(c); // false
Importance of parentheses in complex expressions to avoid ambiguity
Importance of parentheses in complex expressions to avoid ambiguity
Parentheses are used to group expressions and override the default order of operations. Expressions within parentheses are evaluated first. The following example illustrates the importance of parentheses in complex expressions to avoid ambiguity.
int a = 5;
int b = 10;
int c = 15;
int d = a + b * c;
int e = (a + b) * c;
System.out.println(d); // 155
System.out.println(e); // 225
Potential issues with integer division
Potential issues with integer division
Integer division can result in unexpected results. The following example illustrates the potential issues with integer division.
int a = 5;
int b = 10;
int c = a / b;
System.out.println(c); // 0
double d = 5;
double e = 10;
double f = d / e;
System.out.println(f); // 0.5
Summary
In this unit, we learned about operators in Java. We learned about the different categories of operators in Java. We learned about arithmetic, relational, logical, bitwise, assignment, unary, conditional, and instanceof operators. We learned about operator precedence and associativity. We learned about common pitfalls and best practices when working with operators in Java.
References
AP CSA Homework Assignment
Assignment
Instructions
Create a program in java that will demonstrate the use of the following operators:
- Create a Java program that demonstrates the use of the following operators:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Unary operators
- Conditional operator
- instanceof operator
- Create a Java program that demonstrates the use of operator precedence and associativity.
- Create a Java program that demonstrates the use of parentheses in complex expressions to avoid ambiguity.
- Create a Java program that demonstrates the potential issues with integer division.
Some example projects:
- Create a simple calculator that performs arithmetic operations on two numbers.
- Create a program that determines if a number is even or odd.
- Create a program that determines if a number is positive or negative.
- Create a program that determines if a number is prime or composite.