0% found this document useful (0 votes)
19 views2 pages

Java Operators Explained: Types & Usage

The document provides an overview of Java operators, categorizing them into unary, binary, and ternary types based on the number of operands. It explains various operators including arithmetic, relational, logical, and shorthand operators, along with examples for each type. The document serves as a guide for understanding how these operators function in Java programming.

Uploaded by

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

Java Operators Explained: Types & Usage

The document provides an overview of Java operators, categorizing them into unary, binary, and ternary types based on the number of operands. It explains various operators including arithmetic, relational, logical, and shorthand operators, along with examples for each type. The document serves as a guide for understanding how these operators function in Java programming.

Uploaded by

Twinkle
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 Java Operators

2 ---------------
3 C=A+B
4 C,A,B are the operands
5 + is the operator
6 = is the assignment operator
7 Operators are of three types based on the number of operands.
8 Operator
9 |
10 -----------------------------------------------------------
11 | | |
12 Unary(one operand) Binary(Two operands) Ternary(2 or more operands)
13 | | |
14 --- Unary + --- Arithmetic ?: (ternary)
15 --- Unary - --- Shorthand
16 --- Not !(Logical) --- Relational
17 --- Postfix --- Logical
18 --- Prefix --- Bitwise (Class 11 & 12)
19
20 Unary operator: Suppose A=5, B=-6
21 ---------------
22 1) Unary + C = + A => C=+5 => C=5
23 C = + B => C=+(-6)=> C=-6
24
25 2) Unary - C = - A => C=-5
26 C = - B => C=-(-6) => C=6
27
28 3) Postfix (***keep in mind that A++ / ++A both means A is increaseed by 1)
29 (similarly A-- / --A both means A is decreased by 1)
30
31 C = A++ (here the value of A will be copied to C and then A will be
32 increased by 1)
33 C = 5,A = 6
34
35 C = A-- (here the value of A will be copied to C and then A will be
36 decreased by 1)
37 C = 5,A = 4
38
39 4)Prefix C = ++A (here the value of A will be increased by 1 and then copied to C)
40 A = 6, C = 6
41
42 C = --A (here the value of A will be decreased by 1 and then copied to C)
43 A = 4, C = 4
44
45 Binary
46 ------
47
48 (a) Arithmetic Operator: A=5,B=2,C=6,D=2.0
49 ---------------------
50 (i) + (Addition) E=A+B=>E=5+2=>E=7 E=A+D=> E=5+2.0=>E=7.0
51 (ii) - (Subtraction) E=A-B=>E=5-2=>E=3 E=A-D=> E=5-2.0=>E=3.0
52 (iii) * (Multiplication) E=A*B=>E=5*2=>E=10 E=A*D=>E=5*2.0=>E=10.0
53 (iv) / (Division)
54 (If both numbers are integers that, numbers which do not have decimal point
place)
55 E=A/B => E=5/2 =>E=2 (quotient)
56 E=A/C => E=5/6 => E=0 (L<R,/,0)
57 L R
58 (Now if any one of the values is having a decimal point place, then perform
59 normal division)
60 E=A/D => E=5/2.0 => E=2.5
61 E=D/A => E=2.0/5 => E=0.4
62 (v) % (Modulus or Remainder)
63 E=A%B => E=5%2 => E=1
64 E=B%C => E=2%6 => E=2 (L<R,%,L)
65 L R
66
67 (b) Relational Operator: A=5,B=2,C=6,D=5
68 --------------------
69 (i) less than < , A<C,True, A<B,False, A<D,False
70 (ii) greater than > A>C, False, A>B,True, A>D,False
71 (iii) less than equal to <= A<=C,True, A<=D, True, A<=B,False
72 (iv) greater than equal to >= A>=C,False, A>=D,True,
73 (v) equal to == A==D,True, A==C,False (same true, different false)
74 (vi) not equal to != A!=D,False, A!=C,True (same false, different true)
75
76 (c) Logical Operator: A=5,B=2,C=6,D=5
77 -----------------
78 (i) and ( && ): If all the conditions are true then only the final answer
79 is true, otherwise false.
80 Examples:
81 (a) A>B && A==D
82 5>2 && 5==5
83 True && True => True
84
85 (b) A!=C && A==D && B>C
86 5!=6 && 5==5 && 2>6
87 True && True && False, => False
88
89 (ii) or ( || ) : If any one of the conditions are true then the final answer
90 is true, if all the conditions are false then the final
91 answer is false.
92 Examples:
93 (a) A!=C || A==D || B>C
94 True || True || False, => True
95
96 (b) A==C || A!=D || B>C
97 False || False || False , => False
98
99 (iii) not ! : It converts true to false and vice versa.
100 Examples:
101 (a) !(A==C && B>C), !(False && False), !(False), True
102 (b) !(A!=C && B<C), !(True && True) , !(True), False
103
104 (d) Shorthand Operator : A=5,B=2
105 --------------------
106 (i) A+=B => A=A+B, A=5+2=7
107 (ii) A-=B => A=A-B, A=5-2=3
108 (iii) A*=B => A=A*B, A=5*2=10
109 (iv) A/=B => A=A/B, A=5/2 = 2
110 (v) A%=B => A=A%B, A=5%2=1
111

Common questions

Powered by AI

Relational operators in Java, such as <, >, <=, >=, ==, and !=, are used to compare two values or expressions and return a boolean result. These operators are crucial for decision-making structures, such as if-else statements and loops, because they allow the program to choose different execution paths based on conditions. For example, if A is 5 and C is 6, the expression 'A < C' evaluates to true, which could govern a conditional path in the code . These operators enable dynamic decision-making by evaluating variable relationships at runtime .

Logical operators in Java include 'AND' (&&), 'OR' (||), and 'NOT' (!). 'AND' returns true only if all operands are true. For example, the expression 'A>B && A==D' evaluates to true if A is greater than B and A equals D . 'OR' returns true if at least one operand is true. For example, 'A!=C || A==D' returns true if A is not equal to C or A equals D . 'NOT' flips the boolean value of an expression, converting true to false and vice versa. For example, '!(A==C && B>C)' returns true because A is not equal to C, making the whole expression false, and the 'NOT' operator then returns true .

Unary operators operate on a single operand, such as unary plus and minus which signify positive and negative values respectively, as well as postfix and prefix operators which alter the value of their operand by incrementing or decrementing it by one . Binary operators involve two operands and include arithmetic operations (addition, subtraction, multiplication, division, modulus) and relational operations (greater than, less than, etc.). Ternary operators require at least two operands and are primarily used for making concise conditional assignments, typically taking the form of a condition followed by two expressions separated by a colon .

Shorthand operators in Java, such as +=, -=, *=, /=, and %=, are used to modify variable values concisely without requiring re-typing the variable on both sides of the equation. For instance, 'A += B' is a more efficient way of writing 'A = A + B' . These operators enhance code readability and efficiency by reducing redundancy and potential errors in long expressions, especially in loops or large codebases where such operations are frequently executed .

The ternary operator in Java is significant because it allows for concise conditional assignments, effectively streamlining code by reducing the need for multi-line if-else statements. It operates with a syntax of 'condition ? expression1 : expression2', returning expression1 if the condition is true, and expression2 otherwise. For example, 'int result = (A > B) ? A : B;' assigns the greater of A or B to result, achieving in one line what typically requires several lines and an if-else construct, thus improving code compactness and readability .

The modulus operator (%) in Java returns the remainder of the division between two numbers. For instance, '5 % 2' returns 1, because dividing 5 by 2 results in a quotient of 2 with a remainder of 1 . Common use cases for the modulus operator include determining even or odd numbers (a number is even if it mod by 2 equals 0), cycling through circular structures like arrays, and implementing hash functions in data structures to ensure range limits .

In postfix operations such as 'A++', the current value of A is used in expressions before A is incremented. For example, if A is initially 5, 'int C = A++' assigns 5 to C, then increments A to 6 . In prefix operations such as '++A', A is incremented before it is evaluated in expressions. So, 'int C = ++A' first increments A to 6, then assigns this value to C . This order of evaluation directly impacts the result of operations, especially in complex expressions or iterative processes .

The expression 'A != C' evaluates to true if A is not equal to C and false if they are equal. For instance, if A is 5 and C is 6, 'A != C' is true because the values are different . Conversely, if A and C were both 5, 'A != C' would evaluate to false since the values are identical . These evaluations are critical in control flow structures, determining actions based on inequality .

Developers prefer using logical operators in Java because they enable the combination of multiple conditions into a single, concise expression, facilitating complex decision logic in a straightforward, readable manner. Logical operators like '&&' (AND), '||' (OR), and '!' (NOT) provide substantial benefits when evaluating multiple conditions, as they allow for short-circuit evaluation, reducing unnecessary computations. For instance, in 'A>B && A==D && B>C', once any condition evaluates false with 'AND', the evaluation stops, optimizing performance by not checking further conditions. This capability is particularly advantageous in large-scale applications where performance and readability are critical .

When both operands of the division operator ('/') are integers in Java, the result is also an integer, that is, the quotient without any fractional part. For example, 5 / 2 results in 2 . If at least one of the operands is a floating-point number, Java performs normal division, producing a floating-point result. For example, 5 / 2.0 results in 2.5 .

You might also like