College of Science
Department of Computer
Programing Fundamentals (Java)
First Stage
Prepared by:
Ari M.Saeed
2018 - 2019
Operators and Operands
• An operation is an action performed on one or more values.
• The symbol used in an operation is called an operator.
• A value involved in an operation is called an operand.
int a = 1;
int b = 3;
int c = a + b;
a, b, c: are operands.
= , + are operators.
2
The Arithmetic Operators
The Java programming language provides operators that
perform addition, subtraction, multiplication, and division.
Operators Precedence
+ Additive operator (also used for String
concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
The only symbol that might look new is "%", which divides
one operand by another and returns the remainder as its result.
3
Arithmetic Operations
int x = 9, y = 2;
int result = x % y;
System.out.println("result = "+result);
The output is:
result = 1
You can also combine the arithmetic operators with the simple
assignment operator to create compound assignments.
For example:
x+=1;
x=x+1;
Both increment the value of x by 1.
4
Arithmetic Operations
The + operator can also be used for concatenating (joining)
two strings together, as shown in the following:
String firstString = "How ";
String secondString = "are you?";
String thirdString = firstString + secondString;
System.out.println(thirdString);
The variable thirdString contains " How are you?"
5
Operators and Operands
• Operators with higher precedence are evaluated before
operators with relatively lower precedence.
• When operators of equal precedence appear in the same
expression, a rule must govern which is evaluated first.
• All binary operators except for the assignment operators
are evaluated from left to right.
• Assignment operators are evaluated right to left.
6
Assignment Operator
Assignment operator : Assign the value on its right to the
operand on its left. It is denoted by the symbol “=”.
The assignment operator statement has the syntax as shown
below:
int distance = 0;
int radius = 2;
7
Unary Operators
The unary operators require only one operand, they perform
various operations such as incrementing/decrementing a value
by one, negating an expression, or inverting the value of a
boolean.
Operators Precedence
+ Unary plus operator, indicates positive value
(numbers are positive without this,
however)
- Unary minus operator; negates an
expression
++ Increment operator, increments a value by 1
-- Decrement operator, decrements a value by
1
! Logical complement operator, inverts the
value of a boolean
8
Unary Operators
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}}
9
Unary Operators
In the first usage “variable_name++”, the current
statement will be executed at first. Then the value of the
variable will be incremented by 1. Then the next statement
will be executed.
In the second usage “++variable_name”, the value of the
variable will be incremented by 1 at first. Then the
current statement will be executed. Then the next
statement will be executed.
Tips: For decrement is also similar to what we have already
seen for increment operators.
10
Equality and Relational Operators
The equality and relational operators determine if one operand is
greater than, less than, equal to, or not equal to another operand.
Operators Use Description
> valu1 > value2 value1 is greater than value2
>= valu1 > = value2 value1 is greater than or equal to value2
< valu1 < value2 value1 is less than value2
<= valu1 < = value2 value1 is less than or equal to value2
== valu1 = = value2 value1 and value2 are equal
!= valu1 ! = value2 value1 and value2 are not equal
Tips: Keep in mind that you must use "==", not "=", when testing if
two primitive values are equal.
11
Conditional Operators
Conditional operators are used to evaluate a condition that's
applied to one or two boolean expressions.
The result of the evaluation is either true or false.
Condition Operator Example
int i = 2;
If one condition AND another condition && int j = 8;
((i <1) && (j>6))
int i = 2;
If either one condition OR another int j = 8;
||
ondition ((i<1) || (j>=10))
12
Conditional Operators
Another conditional operator is ? :
This operator is also known as the ternary operator because
it uses three operands.
This operator should be read as: "If result is true, assign the
value of salary1 to result. Otherwise, assign the value
of salary2 to result.“
int myAge= 18;
int yourAge= 19;
int selectAge;
selectAge = myAge < yourAge? myAge : yourAge;
System.out.println(selectAge);
This program prints 18 to the screen because result is true.
13
Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
Example:
int a = 38;
int b = 23;
~a = -39
a^b=49
14
Expressions
An expression is a construct made up of variables, operators,
and method invocations.
Expressions are constructed according to the syntax of the
language, that evaluates to a single value, For Example:
int employee = 44;
System.out.println(“Expressions “);
int instances = 1 + 2;
The Java allows you to construct compound expressions from
various smaller expressions.
For example:
3*5*2
15
Ambiguous and unambiguous Expressions
The order of expression is evaluated is unimportant because the
result of multiplication is independent of order. For example:
2 * 3 * 4
The outcome is always the same, no matter in which order you
apply the multiplications.
This is not true of all expressions, For example:
The following expression gives different results, depending on
whether you perform the addition or the division operation first:
x + y / 100 // ambiguous
To specify exactly how an expression will be evaluated using
balanced parenthesis (). For example:
(x + y) / 100 // unambiguous, recommended
16
Ambiguous and unambiguous Expressions
Operators that have a higher precedence get evaluated first.
For example, the division operator has a higher precedence
than does the addition operator.
Therefore, the following two statements are equivalent:
x+ y / 100
x + (y / 100) // unambiguous, recommended
When writing compound expressions, be explicit and
indicate with parentheses which operators should be
evaluated first.
17
Statements
Statements are roughly equivalent to sentences in natural
languages.
A statement forms a complete unit of execution.
The following types of expressions can be made into a
statement by terminating the expression with a semicolon (;).
Result = 8933.234; // assignment statement
result++; // increment statement
System.out.println("Hello World!"); // method invocation
statement
Bicycle myBike = new Bicycle(); // object creation statement
18
Statements
There are two other kinds of statements:
• Declaration statements.
• Control flow statements.
Double avalue = 8933.2; // Declaration statement
Finally, control flow statements regulate the order in which
statements get executed.
You'll learn about control flow statements in the next section
19
Blocks
A block is a group of zero or more statements between
balanced braces and can be used anywhere a single
statement is allowed. The following example, Block
class, illustrates the use of blocks:
class BlockIllustration{
public static void main(String[] args){
{ //block start
int var = 20;
var++;
System.out.println(var);
} //block end
}
}
20
Exercises 1
• What is ternary operator? Explain with example.
• Write a program to explain Signed left shift and Signed right shift.
• In the following program, clarify the value of variable c:
class Postfixprefix {
public static void main(String[] args) {
int a = 15;
int b = 10;
int c = a % b;
c++;
++c;
c--;
System.out.println(c);
}}
21
Exercises 2
• Write a java program to convert temperature in degree
Celsius to Fahrenheit and degree Kelvin.
• F= (C * 9/5) + 32
• F= (K - 273.15) * 9/5 + 32
• Fahrenheit (F), Celsius (C or o), Kelvin (K).
• Write a java program to find the area of this Triangle.
Area = ½ × b × h
h
22
References
Java in a Nutshell, 6th Edition, By David Flanagan,2015
Introduction to programing with java, John S.Dean,2008
http://java.sun.com/docs/books/tutorial
http://www.vogella.com
http://journals.ecs.soton.ac.uk/java/tutorial
23