Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Java p2

The document discusses the different types of operators in Java including arithmetic, unary, assignment, relational, logical, ternary, and bitwise operators. Code examples are provided to demonstrate the usage of each operator type. The types of operators covered are arithmetic operators (+, -, *, /, %), unary operators (++, --), assignment operators (=, +=, etc.), relational operators (<, >, etc.), logical operators (&&, ||, !), ternary operator (?:), and bitwise operators (&, |, ^).

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Java p2

The document discusses the different types of operators in Java including arithmetic, unary, assignment, relational, logical, ternary, and bitwise operators. Code examples are provided to demonstrate the usage of each operator type. The types of operators covered are arithmetic operators (+, -, *, /, %), unary operators (++, --), assignment operators (=, +=, etc.), relational operators (<, >, etc.), logical operators (&&, ||, !), ternary operator (?:), and bitwise operators (&, |, ^).

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program No-2 :-Write a program in Java is showing the uses of different operator.

1 Introduction
Operator in Java:-
Operators in Java are the symbols used for performing specific operations in Java.
Operators make tasks like addition, multiplication, etc which look easy although the
implementation of these tasks is quite complex.
There are multiple types of operators in Java all are mentioned below:

1. Arithmetic Operators +, - , * , /, %
2. Unary Operators ++ , --, - ,+
3. Assignment Operator = , += , -= , *= , /= , %=
4. Relational Operators < ,> ,<= ,>=, == , !=
5. Logical Operators && , || , !
6. Ternary Operator ? :
7. Bitwise Operators &,|,~,^
8. Shift Operators >> , <<
9. Instance of operator instanceof
1.1 Arithmetic operators
Class Arith {
Public static void main (String[] args) {
// Arithmetic operators
Int a = 20;
Int b = 3;
System.out.println(“a + b = “ + (a + b));
System.out.println(“a – b = “ + (a – b));
System.out.println(“a * b = “ + (a * b));
System.out.println(“a / b = “ + (a / b));
System.out.println(“a % b = “ + (a % b));
}
}
//Output
a + b = 33
a - b = 17
a * b = 60
a/b=6
a%b=2

1.2 Unary operator


Class Unary { // main function ↓
Public static void main(String[] args)
{
Int a = 10;
Int b = 10;
// Using uniary operators
System.out.println(“Postincrement : “ + (a++));
System.out.println(“Preincrement : “ + (++a));
System.out.println(“Postdecrement : “ + (b--));
System.out.println(“Predecrement : “ + (--b));
}
}
//Output
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8

1.3 Assignment Operator

Class Assig {
Public static void main(String[] args)
{
Int f = 7;
System.out.println(“f += 3: “ + (f += 3));
System.out.println(“f -= 2: “ + (f -= 2));
System.out.println(“f *= 4: “ + (f *= 4));
System.out.println(“f /= 3: “ + (f /= 3));
System.out.println(“f %= 2: “ + (f %= 2));
System.out.println(“f &= 0b1010: “ + (f &= 0b1010));
System.out.println(“f |= 0b1100: “ + (f |= 0b1100));

}
//Output
F += 3: 10
F -= 2: 8
F *= 4: 32
F /= 3: 10
F %= 2: 0
F &= 0b1010: 0
F |= 0b1100: 12

1.4 Relational Operators


Class Relation {
// main function
Public static void main(String[] args)
{
Int a = 10;
Int b = 3;
Int c = 5;
System.out.println(“a > b: “ + (a > b));
System.out.println(“a < b: “ + (a < b));
System.out.println(“a >= b: “ + (a >= b));
System.out.println(“a <= b: “ + (a <= b));
System.out.println(“a == c: “ + (a == c));
System.out.println(“a != c: “ + (a != c));
}
}
//Output
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true

1.5 Logical Operator


Class Logic {
Public static void main (String[] args) {
Boolean x = true;
Boolean y = false;
System.out.println(“x && y: “ + (x && y));
System.out.println(“x || y: “ + (x || y));
System.out.println(“!x: “ + (!x));
System.out.println(“ y && x: “+(y && x));
System.out.println(“y || x: “+(y||x));
System.out.println(“y ||y : “ +(y||y));
}
}
//Output
x && y: false
x || y: true
!x: false
y && x : false
y || x : true
y || y : false

1.6 Ternary Operator


Public class Ternary{
Public static void main(String[] args)
{
Int a = 200, b = 100, c = 300, result;
result = (a>b) ? (a>c ? a : c) : (b>c ? b : c ) ;
System.out.println(“Max of three numbers = “ result);
}
}
//Output
Max of three numbers = 300

1.7 Bitwise Operator


Class Bit {
// main function
Public static void main(String[] args)
{
// Bitwise operators
Int d = 0b1010;
Int e = 0b1100;
System.out.println(“d & e: “ + (d & e));
System.out.println(“d | e: “ + (d | e));
System.out.println(“d ^ e: “ + (d ^ e));
}
}
//Output
d & e: 8
d | e: 14
d ^ e: 6

You might also like