Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations. Operands of an operator are always evaluated from left to the right. Java operators are classified into three categories: Unary operator Binary operator Ternary operator (conditional operator) These operators operate on only one operand These operators operate on two operands These operators operate on three operands. The value returned by the expression depends upon a condition. Arithmetic operators: These are used to perform basic mathematical calculations. There are two types of arithmetic operator: Binary arithmetic operators (need two operands to perform calculation.) Addition + Subtraction - Multiplication * Division / Remainder % Adds values of its operands Subtracts values of its operands Multiplies values of its operands Divides the first operand by the second. Displays the quotient Finds the modulus of its first operand relative to the second. Displays the remained. Unary arithmetic Operators ( operators that act on one operand) Unary + Unary - This operator precedes an operand. The operands must have arithmetic type and the result is the value of the argument. This operator proceeds an operand. The operands must have arithmetic type and the result is the negation of its operand s value. ( 1 )
Increment (++) / Decrement Operators(- -) These are used to increase or decrease the value stored in a variable by 1 respectively. The operator ++ adds 1 to its operand. The operator - - Subtracts 1 from its operand. These operators can be used both as a prefix and as a suffix. Prefix Increment (++x) Postfix Increment (x++) Prefix Decrement (- -x) Postfix Decrement (x- -) When an increment operator precedes an operand, it is prefix increment. It increases the value of operand by 1 and then evaluates the expression. When an increment operator follows its operand, it is postfix increment. It evaluates the expression, and then increases the value of operand by 1 It decreases the value of operand by 1 and then evaluates the expression. It evaluates the expression, and then decreases the value of operand by 1 Relational Operator These are used to show the relationship between the operands. These operators compare the values of variables and determine the result in the Boolean expression format (true/false). These operators have lower precedence than arithmetic operators, but a higher precedence than the assignment operator. Operator Greater than > Less than < Greater than or equal to >= Less than or equal to <= Equal to = = Not equal to!= Symbol Logical Operator These are used to construct more complex decision making expressions. They provide result in the form of true or false, depending upon the evaluation of different relational expressions. ( 2 )
Java supports five binary and one unary logical operators: Operator Use Description && op1 && op2 Returns true if op1 and op2 are both true. It has lower precedence than the relational operators. op1 op2 Returns true if either op1 or op2 is true. It has lower precedence than the relational operators.!! op It works on single expression or operand. It is a unary operator. It reverse the result of the expression following it. It has higher precedence than any of the relational or arithmetic operators. Bitwise Operators Bitwise operators are special types of operators, which perform manipulations on binary data (bits). These are used for addition, multiplication, shifting and negating the binary data. These are also called binary operators as they are based on Boolean algebra. They work only with byte, short, int and long type operands. Java provides four Bitwise operators: Operators & Meaning Bitwise AND Bitwise OR ^ Bitwise XOR (exclusive OR) ~ Bitwise complement Assignment Operator ( = ) It is used to assign a value of an expression to a variable. It assigns the value on its right side to the variable written on the left of it. If we try to assign a new value to an operand, which already contains some value, then the existing value will be replaced by the new one. Syntax: variable = expression; Java Shorthand assignment Operators: Operator Expression Shorthand assignment expression + = a=a+1 a+=1 ( 3 )
-= a=a-1 a-=1 *= a=a*n a*=n /= a=a/n a/=n %= a=a%n a%=n = a=a n a =n ^= a=a^n a^=n <<= a=a<<n a<<=n >>= a=a>>n a>>=n >>>= a=a>>>n a>>>=n Ternary Operator(?:) (Conditional Operator) This operator needs three operands to perform an operation. Syntax: variable=<test expression>?<expression1>:<expression2>; Test expression1 is evaluated first. If it evaluates to true then the variable contains the value of expression1, otherwise it will contain the value of expression 2. Conditional operator has lower precedence than most of the operators. Special Operators The [ ] operator The. operator The ( ) operator The type operator The new operator Instanceof operator (Object reference) The square brackets are used to declare arrays, to create arrays and to access a particular element in an array. The dot operator accesses instant members of an object or class members of a class When declaring or calling a method, the method s arguments are listed between parenthesis( ). One can also pass an empty argument. This operator casts or converts a value in a variable to the specified type. Used to create a new object or a new array. It is used to check whether the object belongs to a particular class. It returns the value true if its first operand is an instance of its second. ( 4 )
Operator precedence Operator precedence determines the order in which expressions are evaluated. Java Operator Precedence Table Precedence Operator Type Associativity () [] Parentheses Array subscript Member selection Left to Right High precedence ++ -- Unary post-increment Unary post-decrement Right to left ++ -- + -! ~ ( type ) Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast Right to left * / % Multiplication Division Modulus + - Addition Subtraction << >> >>> Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension < <= > >= instanceof Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only) ==!= Relational is equal to Relational is not equal to & Bitwise AND ^ Bitwise exclusive OR Bitwise inclusive OR ( 5 )
&& Logical AND Logical OR? : Ternary conditional Right to left = += -= *= /= %= Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Right to left Expressions An expression in Java is a valid combination of operators, constants and variables i.e. a legal combination of JAVA TOKENS. The type of operators used in an expression determine the expression type. An expression is composed of one or more operands and returns a value as a result. An expression can be mathematical, relational (or logical) or it could be a compound expression depending upon the result it produces. Arithmetic Expressions: These can either be pure integer expressions or pure real expressions. Sometimes a mixed expression can also be formed which is a mixture of real and integer expressions. An arithmetic expression may include variables, constants and arithmetic operators to provide a meaningful result. a. Pure Arithmetic Expression: An expression that has all operands of same data type. These expressions can be pure integer or pure real. Here all operands are of the same type. b. Mixed Arithmetic Expression: If an expression contains both integer and real values, then it is a mixed arithmetic expression. Here operands are of mixed or different data type. Boolean Expressions: Evaluations of expressions that result into true or false are called Boolean expressions. These expressions are valid combinations of constants, variables, relational and logical operators. ( 6 )
Statements Statements direct the computer to perform an action. They can alter the value of variables, process input, generate output or respond to user mouse or keyboard actions. Statements are the smallest executable unit and are always terminated with a semi colon. Java Statements: o Expression Statements: These are valid Java expressions which are terminated by a semi-colon. Assignment expressions e.g: a=5; Any use of ++ or - - e.g: i++, i- -; Method calls e.g: System.out.println( Statement ); Object creation expressions e.g: School grade=new(school); o Assignment Statement: It is used to assign a value to a variable on the left of equals sign(=) with the value of expression given on the right side of equals sign. Declaration Statement: It is used to declare a variable. Control Statement: It is used to control the flow of a program.(if, ifelse,for,while break etc.) o Null Statement(;) :The simplest statement in Java is the empty or null statement. It is written as a single semi-colon. It can be used to indicate an empty loop. o Compound Statement: This is a sequence of statements grouped together within a pair of curly braces{ }. o Block Statement: A block is a group of zero or more statements between the braces and can be used anywhere in the program. ( 7 )