Chapter 5
Chapter 5
Chapter 5
OPERATORS AND
EXPRESSIONS
ARITHMETIC OPERATORS
The arithmetic operators are the operators that perform any
basic form of mathematics. This include.
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus.
OPERATOR DESCRIPTION EXAMPLE
CONSIDER A=10
AND B=10
+
-
++
--
1.The unary “+” operator refers to a positive value.
• It serves a no task except to emphasize that a value must be positive.
• Example:
int a = + 10;
int b = +a;
In the example b will contain a positive value of a .
2. The “-” operator refers to the negative value. It serves no task except
to emphasize that a value must negative.
Int a=20;
Int b = 6;
Int c = -a;
Int d = -b;
Int e = -50 +10;
in the example ,c and d contain negative value of a and b,being -20 and -
6 respectively .the value of e is -40;
Increment and decrement operator
(++ and --)
• The increment and decrement operators are very simple
operators that simple increase or decrease the value by 1.
• These operators are generally used in loops to increment or
decrement the counter variables.
• The increment operator adds 1 to its operand ,and decrement
operators subtracts 1
• x = x + 1; is the same as x++;
and
x = x - 1; is the same as x--;
• The increment and decrement operators can either prefix or
postfix the operand .
x = x +1 ; can be written as
++x; // in the prefix form or as x++; // postfix form
x = 10;
y = ++x;
// first increment the value of x and then assign the value to y.
X= 10;
Y = x++;
// first the value of x is to y and the it will get increment.
The assignment operator
The assignment operator is the signal equal sign , = .this
behavior of assignment operator is similar to other
programming language like c and c++ .
~ ~5 ~0101 1010 10
^ 5^1 0101 ^ 0001 0100 4
IF ELSE STATEMENT
if( Expression 1) If to specify a block of code to be executed , if specified
{ condition is true.
Statements // block of code
Else to specify a block of code to be executed , if
}
else specified condition is false.
{ Conditional operator follows the same algorithm as of if-
Statements // block of code else statement, but it takes less space and helps to write
}
the if-else statements in the shortest way possible..
Instanceof operator
• It is used to check whether an object is an instance of a particular class
or not.
• The operator also checks whether an object is an instance of a class
that implements an interface
SYNTAX : (Object reference variable) Instanceof (class/interface type)
EXAMPLE :
Public static void main(String args[])
{
String name = “JAVA”;
System.out.println(“Is name an instance of String:” + (name
instanceof String));
}
OUTPUT :
Is name an instance of string : true
ADDITIONAL OPERATORS
SEMI-COLON(;)
CURLY BRACKETS { }
PARENTHESES( )
SQUARE BRACKETS [ ]
COMMA (,)
SINGLE QUOTE (‘)
DOUBLE QUOTES (“)
DOT OPERATOR (.)
NEW OPERATOR (new)
SEMI COLON (;)
The semi-colon is used to indicate the end of an expression, a declaration or a statement
Example: System.out.println(“Hello”);
CURLY BRACKETS{ }
Curly Brackets are used to create a section of code. Curly brackets are also used to create variable
scope.
Example : class Test{ if(a>10) {
}}
PARENTHESES ( )
Parentheses are used to differentiate a method such as main from a regular variable. It is also used to
isolate an operation or an expression with regard to another operation or expression.
Example : main()
or
int a=(2*(9/3-1))*(4-8/2)
SQUARE BRACKETS [ ]
Square brackets are mostly used to control the dimension or index of an array.
EXAMPLE : int a[5];
COMMA ( , )
The comma is used to separate variables used in a group.
SINGLE QUOTE ( ‘ )
The single quote is used to include one character to initialize or assign a symbol to , a
variable declared as char.
DOT OPERATOR ( . )
Dot operator is used to access the instance variables and methods of class using an object.
Example : function.f1;
function.simple();
NEW OPERATOR
The new operator is used to create objects . Instances of classes and arrays.
Example : Student age = new Student();
Expressions In Java
Expressions:
• An Expression is a Combination of variables, Literals,
and Operators.
• An expressions in java is any valid combination of
variables, literals and operators.
Example: In a expression x = 3;
Types of Expressions:
• Arithmetic Expressions
• Boolean Expressions
• String Expressions
Arithmetic Expressions:
• One Special expression in java is the use of the addition operator (+) to create
and concatenate Strings.
• Java string expressions are expressions which results in string. the string
expression may consist of operators, variable, literals and strings.
• The + operators, when used with the strings and other objects, creates a single
string that contains the concatenation of all its operands.
• If any of the operands in string concatenation is not string, it is automatically
converted to a string
package expressions;
}
PRECEDENCE OF ARITHMETIC
OPERATORS
The java compiler must apply some rules to determine the order of execution
in expressions.
x = a * b + c * d / 10
Here, the expression is made up of five variable names (x , a , b , c and d)
and four operators ( = , * , + ,and /)
When the two or more operators occur in the same larger expression, the
compiler applies two rules
1. Precedence
2. Associativity
OPERATOR PRECEDENCE
• WHAT IS ASSOCIATIVITY ?
When two operators have the same precedence associativity determines
evaluation order .
If an expression contains same precedence , in this case associativity
determines the evaluation order . Associativity can be either left –to- right or
right- to- left.
• Example for associativity.
x = y = z = 17 is treated as x = ( y = ( z =17)) this is right -to -left associativity .
72 / 2 / 3 is treated as (72 / 2 ) / 3 this is left- to - right associativity
MATHEMATICAL
• FUNCTIONS
The java.lang.Math contains a set of basic math functions for
obtaining the absolute value, highest and lowest of two
values, rounding of values, random values etc. These basic
math functions of the Java Math class will be covered in the
following methods.
• Math. Abs()
The Math. Abs() function returns the absolute value of the parameter passed to
it. The absolute value is the positive value of the parameter. If the parameter
value is negative, the negative sign is removed and the positive value
corresponding to the negative value without sign is returned.
EXAMPLE
int abs1 = Math.abs(10); // abs1 = 10
int abs2 = Math.abs(-20); // abs2 = 20
•
Math.min()
The Math.min() method returns the smallest of two values passed to it as
parameter.
EXAMPLE
int min = Math.min(10, 20);
• Math.max()
The Math.max() method returns the largest of two values passed to it
as parameter. Here is a Math.max() Java example:
EXAMPLE
Int max = Math.max(10, 20);