Operators
Table of Contents
Assign the value 10 to the variable maximum
Assign the value false to the variable isBig
Print out the two values
//This is the most important operator
int maximum;
boolean isBig;
maximum = 10;
isBig = false;
System.out.println("maximum is " + maximum);
System.out.println("isBig is " + isBig);
Assign the int 1000 to the variable num
Add 100 to num
/*
The += operator is a shorthand (abbreviation).
That is,
num += 10;
is equivalent to
num = num + 10;
*/
int num = 1000;
num += 100;
System.out.println("num (using +=) is " + num);
Assign the int 1000 to the variable num
Subtract 100 from num
/*
The -= operator is a shorthand (abbreviation).
That is,
num -= 10;
is equivalent to
num = num - 10;
*/
int num = 1000;
num -= 100;
System.out.println("num (using -=) is " + num);
Let a, b, c, d = 10, 20, 30, 40
Let sum = a + b + c + d
Print sum
int a, b, c, d, sum;
a = 10;
b = 20;
c = 30;
d = 40;
sum = a + b + c + d;
System.out.println("sum of 10, 20, 30 and 40 = " + sum);
Let larger = 5.0
Let smaller = 0.5
Let difference = larger minus smaller;
Print difference
double larger, smaller, difference;
larger = 5.0;
smaller = 0.5;
difference = larger - smaller;
System.out.println("Difference between 5 and 0.5 is " + difference);
Let a = 5
Let b = 2
Let product = a * b
Print product
int a, b, product;
a = 5;
b = 2;
product = a * b;
System.out.println("product of 5 times 2 is " + product);
Let a = 10
Let b = 5
Let quotient = a / b
Print quotient
int a, b, quotient;
a = 10;
b = 5;
quotient = a / b;
System.out.println("a is " + a);
System.out.println("b is " + b);
System.out.println("quotient for a divided by b is " + quotient);
/*What are the results when you try
7/5
7/0
*/
Let a = 5.0
Let b = 2.5
Let quotient = a / b
Print quotient
double a, b, quotient;
a = 5.0;
b = 2.5;
quotient = a / b;
System.out.println("a is " + a);
System.out.println("b is " + b);
System.out.println("quotient for a divided by b is " + quotient);
/*What are the results when you try
5.0/0.0
*/
int a, b, remainder;
a = 7;
b = 5;
//Using Java's Remainder (modulus) operator %
remainder = a % 5;
System.out.println("Remainder of 7 divided by 5 is " + remainder);
Given:
Even numbers have remainder 0 when divided by 2 (evenly divided by 2)
Odd numbers have remainder 1 when divided by 2 (not evenly divided by 2)
Let num = 9
Calculate remainder of num divided by 2
Let isEven be true if the remainder is equal to 0
Let isOdd be true if the remainder is equal to 1
int num, remainder;
boolean isEven, isOdd;
num = 9;
remainder = num % 2;
isEven = remainder == 0;
isOdd = remainder == 1;
System.out.println("num is " + num);
System.out.println("remainder (when num is divided by 2) is " + remainder);
System.out.println("isEven is " + isEven);
System.out.println("isOdd is " + isOdd);
Let int1 = 15
Let int2 = 15
Let result = "Equal to" comparison int1 and int2
Print result
//Use the "Equal to" (equality) operator "==" that is used for primitive variables (int, boolean, double, etc)
//The equality operator results in a boolean answer
//NOTE WELL -- we used int types here, but this operator is similar for all primitive types
int int1 = 15;
int int2 = 15;
boolean result;
result = int1 == int2;
System.out.println("'Equal to' operator result for (15 == 15) is " + result);
//Now try setting int2 to 99, and see what happens
Let int1 = 15
Let int2 = 15
Let result = "Not Equal to" comparison int1 and int2
Print result
Change int2 to 99
Let result = int1 == int2
Print result
//Use the "Not Equal to" (inequality) operator "!=" that is used for primitive variables (int, boolean, double, etc)
//The equality operator results in a boolean answer
//NOTE WELL -- we used int types here, but this operator is similar for all primitive types
int int1 = 15;
int int2 = 15;
boolean result;
result = int1 != int2;
System.out.println("'Not Equal to' operator result for (15 != 15) is " + result);
//Now try setting int2 to 99 and see what happens
int a, b;
a = 1000;
b = 5;
System.out.println("");
System.out.println("Relational Operators");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a == b is " + (a == b));
System.out.println("a != b is " + (a != b));
System.out.println("a > b is " + (a > b));
System.out.println("a >= b is " + (a >= b));
System.out.println("a < b is " + (a < b));
System.out.println("a <= b is " + (a <= b));
//Try changing a and b
Let bool1 = true
Let bool2 = true
Let result = "Equal to" comparison bool1 and bool2
Print result
//Use the equality or equals operator "==" that is used for primitive variables
boolean bool1 = true;
boolean bool2 = true;
boolean result = bool1 == bool2;
System.out.println("'Equal to' operator result for (true == true) is " + result);
//Now try setting bool2 to false and see what happens
/*NOTE WELL: THIS IS SORT OF A TRICK PROBLEM
The reason is that
true == true results in true
false == true results in false
So we could just use the left operator as-is (it does not change)*/
boolean boolean1, boolean2, booleanResult;
boolean1 = true;
boolean2 = false;
//We use Java's AND operator which is &&
booleanResult = boolean1 && boolean2;
System.out.println("" + boolean1 + " && " + boolean2 + " is " + booleanResult);
//Try: Try changing the variable boolean2
boolean boolean1, boolean2, booleanResult;
boolean1 = true;
boolean2 = false;
//We use Java's OR operator which is ||
booleanResult = boolean1 || boolean2;
System.out.println("" + boolean1 + " || " + boolean2 + " is " + booleanResult);
//Try: Try changing the variable boolean2
Given an int variable, negate the value
e.g. for 8, derive -8
for -205, derive 205
(that is, just flip the sign of the number)
Let value = 205
Let flippedValue = -value
Print value
Print flippedValue
int value, flippedValue;
value = 205;
flippedValue = -value;
System.out.println("value is " + value);
System.out.println("flippedValue (using unary -) is " + flippedValue);
//Try: Try changing the variable
Let value = 8
Increment the value
Print value
int value;
value = 9;
value++;
System.out.println("Incremented value is " + value);
Let value = 8
Decrement the value
Print value
int value;
value = 9;
value--;
System.out.println("Decremented value is " + value);
Given a boolean, derive and print the opposite value (called logical NOT, negation, or complement)
For true, derive false
For false, derive true,
That is: flip it
Let value = false
Let flippedValue = the logical negation of value
Print value
Print flippedValue
boolean value, flippedValue;
value = false;
flippedValue = !value;
System.out.println("value is " + value);
System.out.println("flippedValue (using !=) is " + flippedValue);
//Try: Try changing the variable
int a, b, max;
a = 10;
b = 8008;
max = (a < b) ? a : b;
System.out.println("min (using ternary operator) is " + max);