0% found this document useful (0 votes)
28 views17 pages

Session 4

The document provides an overview of Java programming fundamentals, focusing on various types of operators including arithmetic, relational, logical, bitwise, assignment, increment/decrement, and conditional operators. It explains the precedence and associativity of operators, along with examples of expressions and their evaluations. Additionally, it introduces special operators like instanceof and the dot operator, along with their usage in Java programming.

Uploaded by

saarthak22305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views17 pages

Session 4

The document provides an overview of Java programming fundamentals, focusing on various types of operators including arithmetic, relational, logical, bitwise, assignment, increment/decrement, and conditional operators. It explains the precedence and associativity of operators, along with examples of expressions and their evaluations. Additionally, it introduces special operators like instanceof and the dot operator, along with their usage in Java programming.

Uploaded by

saarthak22305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT I: FUNDAMENTALS OF JAVA

TECHNOLOGY
AND PROGRAMMING

Operators Precedence &


Associativy Expression
OPERATORS

2
2
Java supports a rich set of operators.

• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and Decrement operators
• Conditional operators
• Bitwise logical operators
• Special operators

ARITHMETIC OPERATORS
Integer Arithmetic
a % b equivalent to a – ( a / b ) * b
Operator Meaning

+ Addition or Unary plus -14 % 3 = -2


- Subtraction or Unary Minus
-14 % -3 = -2
-14 % -3 = 2
* Multiplication
Real Arithmetic
/ Division

% Modulo Division
Mixed – Mode Arithmetic
Ex: 15 / 10.0 produces the result 1.5
3
3
15 / 10 produces the result 1
class BasicMath {
public static void main(String args[]) {
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3; Integer Arithmetic
int c = b / 4; a=2
int d = c - a; b=6
int e = -d; c=1
System.out.println("a = " + a); d = -1
System.out.println("b = " + b); e=1
System.out.println("c = " + c); Floating Point Arithmetic
System.out.println("d = " + d); da = 2.0
System.out.println("e = " + e); db = 6.0
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);}}
RELATIONAL OPERATOR LOGICAL OPERATOR
Operator Meaning Operator Meaning
< Is less than
&& Logical AND
<= Is less than or equal to
> Is greater than || Logical OR
>= Is greater than or equal to ! Logical NOT
== Is equal to
!= Is not equal to

BITWISE LOGICAL OPERATOR


C# supports operators that may be used for manipulation of data at bit level. These operators
may be used for testing the bits or shifting them to the right or left. Bitwise operators may not
be applied to floating – point data.
Operator Meaning

& Bitwise Logical AND

| Bitwise Logical OR

^ Bitwise Logical XOR

~ One’s Complement

<< Left Shift

>> Right Shift


5
5
Bitwise Complement ( ~ )
Variable Value Binary Pattern

X 23 00010111

~X 132 11101000

Y FF 11111111

~Y 00 00000000

Shift Operations

Left Shift
Variable Value Binary Pattern
X 33 0 0 1 0 0 0 0 1 (8 bits)

X << 3 the bit pattern of X value is left shifted by thrice.


00100001

0 01000010
0 10000100
1 00001000 6
6
The Resultant bit pattern will be 00001000
Right Shift

Variable Value Binary Pattern


Y 41 00101001

Y>>3 the bit pattern of the Y value is right shifted by thrice.


00101001

00010100 1
00001010 0
00000101 0

The Resultant bit pattern will be 00000101

Bitwise Logical AND Bitwise Logical OR Bitwise Exclusive OR


Variable Value Binary Pattern Variable Value Binary Pattern Variable Value Binary Pattern
X 5 0101 X 5 0101 X 5 0101
Y 2 0010 Y 2 0010 Y 2 0010
X&Y 0 0000
X|Y 7 0111 X^Y 7 0111
A 6 0110
A 6 0110 A 6 0110
B 3 0011
B 1 0001 B 3 0011
A&B 2 0010
A&B 7 0111 A^B 5 0101
7
7
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b; a = 0011
int d = a & b; b = 0110
int e = a ^ b; a|b = 0111
int f = (~a & b) | (a & ~b); a&b = 0010
int g = ~a & 0x0f; a^b = 0101
System.out.println(" a = " + binary[a]); ~a&b|a&~b = 0101
System.out.println(" b = " + binary[b]); ~a = 1100
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
Assignment Operator

Assignment operators are used to assign the value of an expression to a variable. C# has a set
of ‘shorthand’ assignment operators which are used in the form

v op= exp

v - is the variable
exp - is the expression
op - is the binary operator

v op= exp is equivalent to v = v op(exp);


Ex:
x + = y + 1; is equivalent to x = x + (y +1)

Advantages:

. What appears on the left – hand side need not be repeated and
therefore it becomes easier to write.

. The statement is more concise and easier to read.

. The use of shorthand operators results in a more efficient code. 9


9
Increment and Decrement Operator

The operators are ++ and - -

++ means Increment Operator


- - means Decrement Operator

The operator ++ adds 1 to the operand while – subtracts 1. Both are unary operators and are
used in the following form:

++m; or m++
--m or m—

++m; is equivalent to m = m + 1 (or m + = 1;)


--m; is equivalent t o m = m – 1 (or m - = 1;)

We use the increment and decrement operators extensively in for and while loops

For Example:
The statement
Case 1 Case 2 a [ i ++ ] = 10; is equivalent to
m = 5; m = 5; a [ i ] = 10; 10
10
y = ++m; y = m--; i = i + 1;
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c; a=2
int d; b=3
c = ++b; c=4
d = a++; d=1
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
Conditional Operator
It is also known as Ternary operator

Syntax:
exp_1? exp_2: exp_3

Special Operators
Instanceof Operator
The instanceof is an object reference operator and returns true if the object on the left
– hand side is an instance of the class given on the right – hand side. This operator allows us to
determine whether the object belongs to a particular class or not.
Example:
person instanceof student
Dot Operator
The dot operator (.) is used to access the instance variables and methods of class
objects.
Example:
person1.age //Reference to the variable age
person1.salary( ) //Reference to the method salary( ) 12
12
Operators Precedence & Associativity

The Precedence of the Java Operators


•Notice that the first row shows items that you may not normally think of
as operators: parentheses, square brackets, and the dot operator.

•Technically, these are called separators, but they act like operators in an
expression.

•Parentheses are used to alter the precedence of an operation.


Expressions
• An expression is a construct made up of variables, operators, and
method invocations, which are constructed according to the syntax of
the language, that evaluates to a single value.
• Examples of expressions are in bold below:
int number = 0;
anArray[0] = 100;
System.out.println ("Element 1 at index 0: " + anArray[0]);
int result = 1 + 2; // result is now 3 if(value1 == value2)
System.out.println("value1 == value2");

L 2.19
Expressions
The data type of the value returned by an expression depends on the
elements used in the expression.
 The expression number = 0 returns an int because the assignment
operator returns a value of the same data type as its left-hand operand;
in this case, number is an int.
As you can see from the other expressions, an expression can return
other types of values as well, such as boolean or String.
The Java programming language allows you to construct compound
expressions from various smaller expressions as long as the data type
required by one part of the expression matches the data type of the
other.
 Here's an example of a compound expression: 1*2*3

L 2.20
Example
Consider the expression 5+8/(3+1)+12*4+3
Step:1 5+8/4e+12*4+3
Step:2 5+2+12*4+3
Step:3 5+2+48+3
Step:4 58

You might also like