0% found this document useful (0 votes)
10 views12 pages

Module 6 - Java Operators

The document discusses Java operators including arithmetic, increment/decrement, relational, and logical operators. It provides examples of code using each type of operator and the expected output. Sample code shows declaring variables and using the different operators to perform calculations on the variables. The output demonstrates the results of using each operator in the examples provided. Videos are referenced for additional explanation of each operator type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views12 pages

Module 6 - Java Operators

The document discusses Java operators including arithmetic, increment/decrement, relational, and logical operators. It provides examples of code using each type of operator and the expected output. Sample code shows declaring variables and using the different operators to perform calculations on the variables. The output demonstrates the results of using each operator in the examples provided. Videos are referenced for additional explanation of each operator type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Palawan State University

MODULE 1 Bachelor of Science in Information Technology


Quezon Campus
COMPUTER PROGRAMMING 1

Page | 1 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

6 Chapter
JAVA OPERATORS

JAVA OPERATORS

Java provides a rich set of operations environment. Java operators can be divided into the following
categories:

1. Arithmetic Operators
2. Increment & Decrement Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Assignment Operators
7. Conditional Operators

1. Arithmetic Operators
Arithmetic operators are used in mathematical expression in the same way that are used in algebra.

Watch the Video


Video # 19 : Arithmetic Addition

Watch the Video


Video # 20 : Arithmetic Subraction

Watch the Video


Video # 21 : Arithmetic Multiplication

Watch the Video


Video # 22 : Arithmetic Division

Watch the Video


Video # 23 : Arithmetic Remainder

Page | 2 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

SAMPLE INPUT 1
Here's a sample program in the usage of these operators:

public class ArithmeticSample {


public static void main(String[] args) {

//a few numbers


int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;

System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);

//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));

//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));

//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));

//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));

//computing the remainder resulting from dividing numbers


System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));

//mixing types
System.out.println("Mixing types...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));

Page | 3 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

SAMPLE OUTPUT 1
Here is the output of the program,

Variable values...
i = 37
j = 42
x = 27.475
y = 7.22

Adding...
i + j = 79
x + y = 34.695

Subtracting...
i - j = -5
x - y = 20.255

Multiplying...
i * j = 1554
x * y = 198.37

Dividing...
i / j = 0
x / y = 3.8054

Computing the remainder...


i % j = 37
x % y = 5.815

Mixing types...
j + y = 49.22
i * x = 1016.58

Watch the Video


Video # 24 : Arithmetic Sample coding 1

Watch the Video


Video # 25 : Arithmetic Sample coding 2

Page | 4 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

2. Increment & Decrement Operators


Aside from the basic arithmetic operators, Java also includes a unary increment operator (++) and
unary decrement operator (--). Increment and decrement operators increase and decrease a value
stored in a number variable by 1.

The increment and decrement operators can be placed before or after an operand.
When used before an operand, it causes the variable to be incremented or decremented by 1, and
then the new value is used in the expression in which it appears.

SAMPLE INPUT 2
Here's a sample program in the usage of these operators:

public class IncrementDecrementSample {

public static void main(String[] args)


{

int i = 10;
int j = 3;
int k = 0;
int z;

k = ++j + i;
z = j++ + i;

System.out.println(“k”+(k));
System.out.println(“z”+(z));

}
}

SAMPLE OUTPUT 2
Result: k = 14
Z = 13

Watch the Video


Video # 26 : Increment/Decrement Sample

Page | 5 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

3. Relational Operators
Relational operators compare two values and determines the relationship between those values.
The output of evaluation are the Boolean values true or false.

Watch the Video


Video # 27 : Relational Greater than

Watch the Video


Video # 28 : Relational Greater-Equal

Watch the Video


Video # 29 : Relational less than

Watch the Video


Video # 30 : Relational Less-Equal

Watch the Video


Video # 31 : Relational Equal

Watch the Video


Video # 32 : Relational Not-Equal

Page | 6 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

SAMPLE INPUT 3
Here's a sample program that uses relational operators,

public class RelationalDemo {

public static void main(String[] args) {

//a few numbers


int i = 37;
int j = 42;
int k = 42;

System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" k = " + k);

//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j));
System.out.println(" j > i = " + (j > i));
System.out.println(" k > j = " + (k > j));

//greater than or equal to


System.out.println("Greater than or equal to...");
System.out.println(" i >= j = " + (i >= j));
System.out.println(" j >= i = " + (j >= i));
System.out.println(" k >= j = " + (k >= j));

//less than
System.out.println("Less than...");
System.out.println(" i < j = " + (i < j));
System.out.println(" j < i = " + (j < i));
System.out.println(" k < j = " + (k < j));

//less than or equal to


System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j));
System.out.println(" j <= i = " + (j <= i));
System.out.println(" k <= j = " + (k <= j));

//equal to
System.out.println("Equal to...");

System.out.println(" i == j = " + (i == j));


System.out.println(" k == j = " + (k == j));

//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j));
System.out.println(" k != j = " + (k != j));

Page | 7 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

SAMPLE OUTPUT 3
Here's the output from this program:

Variable values...
i = 37
j = 42
k = 42

Greater than...
i > j = false
j > i = true
k > j = false

Greater than or equal to...


i >= j = false
j >= i = true
k >= j = true

Less than...
i < j = true
j < i = false
k < j = false

Less than or equal to...


i <= j = true
j <= i = false
k <= j = true

Equal to...
i == j = false
k == j = true

Not equal to...


i != j = true
k != j = false

Watch the Video


Video # 33 : Relational Sample coding 1

Watch the Video


Video # 34 : Relational Sample coding 2

Page | 8 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

4. Logical Operators
Logical operators have one or two Boolean operands that yield a Boolean result.

There are three logical operators:

1. && (logical AND), ( & called Ampersand)


2. | | (logical OR) and ( | called Pipe)
3. ! (Logical NOT). ( ! called Exclamation mark)

1. && (logical AND)

Here is the truth table:

SAMPLE INPUT 4
Here's a sample source code that uses logical AND,

public class TestAND {


public static void main( String[] args ){

int i = 0;
int j = 10;
boolean test= false;

test = (i > 10) && (j++ > 9);


System.out.println(i);
System.out.println(j);
System.out.println(test);

}
}

SAMPLE OUTPUT 4
Result: 0
10
false

Watch the Video


Video # 35 : Logical AND Sample coding

Page | 9 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

2. | | (logical OR)

Here is the truth table:

SAMPLE INPUT 5
Here's a sample source code that uses logical OR,

public class TestOR {

public static void main( String[] args ){

int i = 0;
int j = 10;
boolean test= false;

test = (i > 10) || (j++ > 9);


System.out.println(i);
System.out.println(j);
System.out.println(test);

}
}

SAMPLE OUTPUT 5
Result: 0
10
true

Watch the Video


Video # 35 : Logical AND Sample coding

Page | 10 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

3. ! (logical NOT)

Here is the truth table:

SAMPLE INPUT 6
Here's a sample source code that uses the logical NOT operator,

public class TestNOT {

public static void main( String[] args ){

boolean val1 = true;


boolean val2 = false;

System.out.println(!val1);

System.out.println(!val2);

SAMPLE OUTPUT 6
The output of the program is,

false
true

Watch the Video


Video # 36 : Logical OR Sample coding

Page | 11 Prepared by Ms. Heraine Jane D. Cortado


Palawan State University
MODULE 1 Bachelor of Science in Information Technology
Quezon Campus
COMPUTER PROGRAMMING 1

5. Bitwise Operators

Operator Description
& (Ampersand) Bitwise AND
| (Pipe) Bitwise OR
^ (Caret) Bitwise exclusive OR
<< Left shift
>> Right shift

Here is the truth table

a b a&b a|b a^b

0 0 0 0 0

0 1 0 0 1

1 0 0 0 1

1 1 1 1 0

Watch the Video


Video # 37 : BitwiseAND Sample coding

Watch the Video


Video # 38 : Bitwise OR Sample coding

Watch the Video


Video # 39 : Bitwise EOR Sample coding

Watch the Video


Video # 40 : left shift Sample coding

Watch the Video


Video # 41 : right shift Sample coding

Page | 12 Prepared by Ms. Heraine Jane D. Cortado

You might also like