Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: Show
Unary Operators in JavaJava unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand. Let’s look at the various unary operators in detail and see how they operate. Operator 1: Unary minus(-)This operator can be used to convert a positive value to a negative one. Syntax: -(operand) Illustration: a = -10 Example: Javaa = -107
a = -108 a = -109 Number = 20 Result = -200
Number = 20 Result = -201 Number = 20 Result = -202 Number = 20 Result = -203
Number = 20 Result = -204 Number = 20 Result = -205 Number = 20 Result = -204 Number = 20 Result = -207 Number = 20 Result = -208 Number = 20 Result = -209 !(operand)0 Number = 20 Result = -204 !(operand)2 !(operand)3 !(operand)4 !(operand)3 !(operand)6 !(operand)7 !(operand)8 !(operand)9
!(operand)3 cond = !true; // cond < false1 !(operand)3 cond = !true; // cond < false3 cond = !true; // cond < false4 cond = !true; // cond < false5
!(operand)3 cond = !true; // cond < false7 !(operand)3 cond = !true; // cond < false9
!(operand)3 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false1 !(operand)3 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false3 !(operand)3 cond = !true; // cond < false3 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false6 cond = !true; // cond < false5 Number = 20 Result = -204 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false9 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false9 Output Number = 20 Result = -20 Operator 2: ‘NOT’ Operator(!)This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand. Syntax: !(operand) Illustration: cond = !true; // cond < false Example: Javanum++1
a = -108 a = -109 Number = 20 Result = -200
Number = 20 Result = -201 Number = 20 Result = -202 Number = 20 Result = -203
Number = 20 Result = -204 Number = 20 Result = -205 Number = 20 Result = -204 Number = 20 Result = -207 Number = 20 Result = -208 Number = 20 Result = -209 !(operand)0 Number = 20 Result = -204 !(operand)2 !(operand)3 num = 5 num++ = 68 !(operand)3 ++num0 ++num1 ++num2 !(operand)9 !(operand)3 !(operand)6 ++num6 ++num7 ++num8 ++num9 !(operand)9
!(operand)3 num = 5 ++num = 62 !(operand)3 cond = !true; // cond < false3 num = 5 ++num = 65 num = 5 ++num = 66 !(operand)3 cond = !true; // cond < false3 num = 5 ++num = 69 a = -1000 !(operand)3 cond = !true; // cond < false3 a = -1003 a = -1004
!(operand)3 num = 5 ++num = 62 !(operand)3 a = -1008 !(operand)3 cond = !true; // cond < false3 a = -1011 a = -1012 !(operand)3 cond = !true; // cond < false3 a = -1015 a = -1016 !(operand)3 cond = !true; // cond < false3 a = -1019 a = -1020 Number = 20 Result = -204 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false9 Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false9 Output: Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false
Operator 3: Increment(++)It is used to increment the value of an integer. It can be used in two separate ways: 3.1: Post-increment operator When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement. Syntax: num++ Illustration: num = 5 num++ = 6 3.2: Pre-increment operator When placed before the variable name, the operand’s value is incremented instantly. Syntax: ++num Illustration: num = 5 ++num = 6 Operator 4: Decrement(–)It is used to decrement the value of an integer. It can be used in two separate ways: 4.1: Post-decrement operator When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement. Syntax: a = -100 Illustration: a = -101 4.2: Pre-decrement operator When placed before the variable name, the operand’s value is decremented instantly. Syntax: a = -102 Illustration: a = -103 Operator 5: Bitwise Complement(~)This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0. Which is a logical operator?A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.
What are operators and operands?An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression. Arithmetic operators ( +, -, *, /, **, % )
Which operator is used to determine that the operands are not exactly of the same value?The inequality operator != returns true if its operands aren't equal, false otherwise. For the operands of the built-in types, the expression x != y produces the same result as the expression !(
What type of operators are the following ==?Relational Operators
== (Equal to)– This operator is used to check if both operands are equal.
|