Upcoming SlideShare
×

Dacj 1-3 b

820 views

Published on

Published in: Technology, News & Politics
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
820
On SlideShare
0
From Embeds
0
Number of Embeds
125
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dacj 1-3 b

1. 1. Operators in JavaPre-assessment Questions 1. Predict the output of the expression, 16 % 3? a. 5 b. 0 c. 1 d. 4 • What is the default value of the float data type? a. 0.0 b. 0 c. 1.0 d. 1 ©NIIT Java Fundamentals Lesson 3B / Slide 1 of 26
2. 2. Operators in JavaPre-assessment Questions (Contd.) 3. Consider the statements: Statement A: The name of a variable can begin with a digit. Statement B: The name of a variable can contain white spaces. Identify the correct option. a. Statement A is true and statement B is false b. Statement A is false and statement B is true c. Both, statements, A and B, are true d. Both, statements, A and B, are false ©NIIT Java Fundamentals Lesson 3B / Slide 2 of 26
3. 3. Operators in JavaPre-assessment Questions (Contd.) 1. _______variables are the local variables that are accessed by the function in which the variables are declared. a. Static b. Automatic c. Instance d. Class 2. _______literals are enclosed in single quotes a. String b. Character c. Boolean d. Integer ©NIIT Java Fundamentals Lesson 3B / Slide 3 of 26
4. 4. Operators in JavaSolutions to Pre-assessmentQuestions 1. c. 2. a. 3. d. 4. a. 5. b. ©NIIT Java Fundamentals Lesson 3B / Slide 4 of 26
5. 5. Operators in JavaObjectives In this lesson, you will learn to: • Use operators • Arithmetic assignment operators • bit-wise operators • shift operators • instance-of operator • Identify the operators precedence ©NIIT Java Fundamentals Lesson 3B / Slide 5 of 26
6. 6. Operators in JavaUsing Unary Operators • Using the Increment and Decrement Operators • The increment and decrement operators are unary operators. • The increment operator (++) increases the value of an operand by 1. • The decrement operator (--) decreases the value of an operand by 1. • Prefix Form • The operator precedes the operand. • Operator operates on the operand before the value of operand is used in the expression. • Postfix Form • In the postfix form, operator follows the operand. • Operator operates on the operand after the value of operand is used in the expression. ©NIIT Java Fundamentals Lesson 3B / Slide 6 of 26
7. 7. Operators in JavaUsing the Arithmetic AssignmentOperators • Arithmetic Assignment Operators • Addition(+), subtraction(-), multiplication(*), division(/), and modulo(%) are the arithmetic operators supported by Java. • Various arithmetic operators, such as +, -, /, *, and % are combined with the assignment operator (=) and are called arithmetic assignment operators. ©NIIT Java Fundamentals Lesson 3B / Slide 7 of 26
8. 8. Operators in JavaUsing the Arithmetic AssignmentOperators (Contd.) Operator Use Description += op1 += op2 Adds operand, op1 and operand, op2 and assigns the result to op1. This expression is equivalent to op1 = op1+op2. -= op1 -= op2 Subtracts operand, op2 from operand, op1 and assigns the result to op1. This expression is equivalent to op1 = op1 – op2. ©NIIT Java Fundamentals Lesson 3B / Slide 8 of 26
9. 9. Operators in JavaUsing the Arithmetic AssignmentOperators (Contd.) Operator Use Description *= op1 *= op2 Multiplies operand, op1 and operand, op2 and assigns value of the result to op1. This expression is equivalent to op1 = op1*op2. /= op1 /= op2 Divides operand, op1 by operand, op2, and assign the value of the result to op1. This expression is equivalent to op1 = op1/op2. ©NIIT Java Fundamentals Lesson 3B / Slide 9 of 26
10. 10. Operators in JavaUsing the Arithmetic AssignmentOperators (Contd.) Operator Use Description %= op1 %= op2 Assigns the remainder of division of operand, op1 and operand, op2 to op1. This expression is equivalent to op1 = op1% op2. ©NIIT Java Fundamentals Lesson 3B / Slide 10 of 26
11. 11. Operators in JavaUsing Bit-wise Operators • Bit-wise operators • Operate on the individual bits of their operand. • Operands can be various data types like int, short, long, char, and byte. • Operands are converted into their binary equivalents before operation. • The result in the binary form after the operation is converted back into its decimal equivalent. ©NIIT Java Fundamentals Lesson 3B / Slide 11 of 26
12. 12. Operators in JavaUsing Bit-wise Operators(Contd.) • The following table lists the various bit-wise operators in Java: Operator Use Operation &(AND) x&y Performs bit-wise AND operation. It evaluates to 1 if both bits, x and y are 1. If either or both bits are 0, the result is 0. |(OR) x|y Performs bit-wise OR operation. It evaluates to 0 if both bits, x and y are 0. If either or both bits are 1, the result is 1. ©NIIT Java Fundamentals Lesson 3B / Slide 12 of 26
13. 13. Operators in JavaUsing Bit-wise Operators(Contd.) Operator Use Operation ~(inversion) ~x Performs unary NOT operation. Converts all the 1s into 0s and all the 0s into 1s. ^(XOR) x^y Performs bit-wise XOR operation. It evaluates to 1 if bits have different values and 0 if both the bits have the same value. ©NIIT Java Fundamentals Lesson 3B / Slide 13 of 26
14. 14. Operators in JavaUsing Bit-wise Operators (Contd.)• Using the Bit-wise AND Operator • The Bit-wise AND operator (&) performs AND operation on two operands. • Displays 1 if both bits are 1 else 0 in all other cases. Operation Result 0&0 0 0&1 0 1&1 1 1&0 0 ©NIIT Java Fundamentals Lesson 3B / Slide 14 of 26
15. 15. Operators in JavaUsing Bit-wise Operators (Contd.)• Using the Bit-wise OR Operator • The Bit-wise OR operator (|) performs OR operation on two operands. • Displays 0 if both bits are 0 else 1 in all other cases. Operation Result 0|0 0 0|1 1 1|1 1 1|0 1 ©NIIT Java Fundamentals Lesson 3B / Slide 15 of 26
16. 16. Operators in JavaUsing Bit-wise Operators (Contd.)• Using the Bit-wise NOT Operator • Bit-wise NOT operator (~) is a unary operator and performs NOT operation on each bit of binary number. • The NOT operator inverts or complements each of the bits of a binary number. Operation Result ~0 1 ~1 0 ©NIIT Java Fundamentals Lesson 3B / Slide 16 of 26
17. 17. Operators in JavaUsing Bit-wise Operators (Contd.)• Using the Bit-wise XOR Operator • The Bit-wise XOR (^) operator performs XOR operation on two operands. • The XOR operator applied on two bits results in 1 if exactly one bit is 1 else 0 in all other cases . Operation Result 0^0 0 0 ^1 1 1^1 0 1^0 1 ©NIIT Java Fundamentals Lesson 3B / Slide 17 of 26
18. 18. Operators in JavaUsing Shift Operators • Works on the bits of data. • Shifts the bits of it’s operand either to left or right. Operator Use Operation >> val1 >> val2 Shifts the bits of the val1 operand to the right by the number of (Right Shift) positions specified by val2. << val1<< val2 Shifts the bits of the val1 operand to the left by the number of (Left Shift) positions specified by val2. ©NIIT Java Fundamentals Lesson 3B / Slide 18 of 26
19. 19. Operators in JavaUsing Shift Operators (Contd.) Operator Use Operation >>> val1 >>> val2 Shifts the bits of the val1 operand to the right by the number of (Unsigned Shift positions specified by val2. A zero Operator) value is input in the high-order bit irrespective of the value of the high-order bit of val1. The high- order bit is the leftmost bit of the binary number. ©NIIT Java Fundamentals Lesson 3B / Slide 19 of 26
20. 20. Operators in JavaUsing Shift Operators (Contd.)• Using the Right Shift and Left Shift Operators • The right shift and the left shift operators are binary operators. • The right shift operator shifts all the bits of a binary number in the right direction. operand >> num • The left shift operator, <<, shifts all the bits of a binary number in the left direction. operand << num• Using the Unsigned Shift Operator • Unsigned shift operator (>>>) is used to shift the bits of a binary number to the right. • The operator fills the leftmost bits of a binary value with 0 irrespective of whether the number has 0 or 1 at the leftmost bit. ©NIIT Java Fundamentals Lesson 3B / Slide 20 of 26
21. 21. Operators in JavaUsing instance of Operator • Used to test whether an object is an instance of a specific class. • Used at the run time. • The syntax of the instanceof operator is: op1 instanceof op2 • op1 is the name of an object and op2 is the name of a class. • Returns a true value if the op1 object is an instance of the op2 class. ©NIIT Java Fundamentals Lesson 3B / Slide 21 of 26
22. 22. Operators in JavaOperators Precedence • Each operator in an expression is evaluated in a predetermined order called operator precedence. • Operators on the same line have equal precedence. Operators Category [], (), . , expr++, expr-- Array index, method call, member access, and postfix operators ++expr, --expr, +, -, !, ~ Unary postfix operators *, /, % Multiplicative Arithmetic operators +, - Additive Arithmetic operators ©NIIT Java Fundamentals Lesson 3B / Slide 22 of 26
23. 23. Operators in JavaOperators Precedence(Contd.) Operators Category <<, >>, >>> Shift operators >, <, <=, >=, instanceof Relational operators ==, != Equality operator & Bit-wise AND operator ^ Bit-wise XOR operator | Bit-wise OR operator && Conditional AND operator || Conditional OR operator =, +=, -=, *=, /=, %= Assignment operators ©NIIT Java Fundamentals Lesson 3B / Slide 23 of 26
24. 24. Operators in JavaDemonstration-Precedence ofOperators in Java • Problem Statement • Create a Java application to understand the order of precedence of various operators in Java. • Solution • The Java application is created using the various operators in Java. • To solve the above problem, perform the following tasks: 3. Code an application with an expression having multiple operators of different precedence 4. Compile and execute the code. ©NIIT Java Fundamentals Lesson 3B / Slide 24 of 26
25. 25. Operators in JavaSummary • In this lesson, you learned: • Operators are used to calculate, compare and compute values. The operators are classified as: • Unary operators • Binary operators • Unary operators operate on one operand, such as increment and decrement operators. • Binary operators operate on two operands, such as arithmetic operators +,-,/,*. • Arithmetic operators, such as, +, _, combined with assignment operator(=) are arithmetic assignment operators. • Bit-wise operators operate on one bit at a time. • The bit-wise operators operate on data types such as, int, short, long, char and byte. • The various bit-wise operators are bit-wise AND (&), bit-wise OR (|), bit- wise NOT (~), and bit-wise XOR (^). ©NIIT Java Fundamentals Lesson 3B / Slide 25 of 26
26. 26. Operators in JavaSummary (Contd.) • Shift operators are used to shift the bits of the number to the left or right. The various shift operators are right shift (>>), left shift (<<), and unsigned shift operator (>>>). • The instanceof operator is used to test that whether an object is an instance of a particular class. • An expression can have several operators and each operator is evaluated in order of their precedence. ©NIIT Java Fundamentals Lesson 3B / Slide 26 of 26