Chap 03
Operators can be defined as special types of functions which take two or more arguments and returns a valid output.
There are 5 types of operators :
1. Arithmetic Operator
2. Bitwise Operator
3. Assignment Operator
4. Conditional Operator
5. Logical Operator
ARITHMETIC OPERATOR
Arithmetic operator can be defined as programming operator which are used to perform arithmetic operations such as additions , subtraction , multiplication , division, and modulus functions.
CODE:
package operators;
public class Arithmetic Operators
{
public static void main(String[] args)
{
int a = 3 ;
int b = 5 ;
int c = a + b ; // performing addition
System. out. println(c); //Output : 8
int d = b/ a ; // Performing division
System. out. println(d); // Output : 1
int e = a * b ; //Performing Multiplication
System. out. println(e); //Output : 15
int f = b - a ; //Performing subtraction
System. out. println(f); // Output : 2
int g = b % a ; //Performing modulus
System.out.println(g); // Output : 2
}
}
What did we learn in the above code?
1. In the above code, we discussed 4 types of operators addition, subtraction, multiplication, division.
2. “+” is a operator used to return the sum of the two numbers input by the user.
3. “-” is a operator used to return the difference of two numbers ,“*” is used to return product of two number , “/” is used to return quotient of two numbers ,“%” is used to return the remainder of two numbers.
NOTE : Operands are the the numbers on which operation is being performed.
BITWISE OPERATORS : Bitwise operators are used to perform operations on ints and units at the binary level . Bitwise operators are the easiest in use for computers because it perform operation in “0” and “1” which is helpful and more proficient for computer.
TYPES OF BITWISE OPERATORS:
1. NOT
2. AND
3. OR
4. XOR
5. Left shift and Right Shift
CODE:
package operators ;
public class Bitwise operators{
public static void main(String[] args)
{
int a = 5 ;
int b = 6 ;
int c = a & b ; // AND OPERATOR
int d = a | b ; // OR OPERATOR
int e = a >> 2; // RIGHT SHIFT OPERATOR
int f = a<<2 ; //LEFT SHIFT OPERATOR
int g = !a ; // NOT OPERATOR
}
}
NOTE : These are some bitwise operators. The concept of working in bitwise operator is lengthy as it would be covered in later blogs. Bitwise operators work on ‘0’ and ‘1’ binary language so to understand conversion mechanism is a different concept to cover.
ASSIGNMENT OPERATORS: These operators are used to assign the value to the variable .
For example :
1. int a = 5 ;
2. double b = 6.034 ;
3. float c = 3.4444 ;
4. char g = ‘d’
Logical Operators: Logical operators are allowed to make decision on the based on the multiple conditions . Each operand is considered a condition that can be evaluated to a true or false value. Some of the logical operators are &&(AND) , ||(OR) , !(NOT) , &(BITWISE AND) .
CODE:
package operators;
public class LogicalOperators {
public static void main(String[] args) {
int number = 120;
if (number>1 && number <= 200) {
System.out.println("True");
}
if (number>1 & number <100){
System.out.println("False");
}
}
}
CONDITIONAL OPERATORS: Conditional operators helps in working if and else resulting a Boolean expression. Ternary operator is a example of conditional operators .
CODE:
If (conditon1)
{
//block of the code is executed if the condition is True
}
else if (condition 2 )
{
// block of the code to be executed if the condition 1 is false and condition 2 is true .
}
else
{
// executed if both condition 1 and condition 2 both are false.
}
TERNARY OPERATORS : Ternary operator is a short hand of if- else statement . If the condition 1 is true , then true is executed . If condition 1 is false , then false statement is executed .
SYNTAX : int age = Condition ? TRUE : FALSE
In next blog, I would discuss about Switch Statement and types of loops such as for, while, do-while statement.
Comments
Appreciate the author by telling what you feel about the post 💓
No comments yet.
Be the first to express what you feel 🥰.
Please Login or Create a free account to comment.