0% found this document useful (0 votes)
2 views8 pages

Tutorial 1 Operators in Java Lab Manual

The document provides a comprehensive overview of various operator types in Java, including arithmetic, unary, assignment, relational, logical, bitwise, shift, ternary, and type comparison operators. Each section includes a table with operators, corresponding statements, and their purposes, along with example Java programs demonstrating their usage. Additionally, it includes home assignments for practicing these concepts.

Uploaded by

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

Tutorial 1 Operators in Java Lab Manual

The document provides a comprehensive overview of various operator types in Java, including arithmetic, unary, assignment, relational, logical, bitwise, shift, ternary, and type comparison operators. Each section includes a table with operators, corresponding statements, and their purposes, along with example Java programs demonstrating their usage. Additionally, it includes home assignments for practicing these concepts.

Uploaded by

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

Operators in Java

Practice Statements for All Operator Types

1. Arithmetic Operators
Sr. Operator Statement Purpose

1 + int add = a + b; Addition

2 - int sub = a - b; Subtraction

3 * int mul = a * b; Multiplication

4 / int div = a / b; Division

5 % int mod = a % b; Modulus

2. Unary Operators
Sr. Operator Statement Purpose

1 Post Increment a++; Increase after use

2 Pre Increment ++a; Increase before use

3 Post Decrement a--; Decrease after use

4 Pre Decrement --a; Decrease before use

5 Unary + int pos = +a; Positive value

6 Unary - int neg = -a; Negative value

7 Logical NOT boolean not = !flag; Reverse boolean

3. Assignment Operators
Sr. Operator Statement Purpose

1 = a = 10; Simple assignment

2 += a += 5; Add and assign

3 -= a -= 5; Subtract and assign

4 *= a *= 2; Multiply and assign


5 /= a /= 2; Divide and assign

6 %= a %= 2; Modulus and assign

4. Relational Operators
Sr. Operator Statement Purpose

1 == boolean eq = (a == b); Equal to

2 != boolean ne = (a != b); Not equal to

3 > boolean gt = (a > b); Greater than

4 < boolean lt = (a < b); Less than

5 >= boolean ge = (a >= b); Greater or equal

6 <= boolean le = (a <= b); Less or equal

5. Logical Operators
Sr. Operator Statement Purpose

1 && boolean and = (a > 10 Logical AND


&& a < 20);

2 || boolean or = (a == 10 Logical OR
|| a == 20);

3 ! boolean not = !(a == Logical NOT


10);

6. Bitwise Operators
Sr. Operator Statement Purpose

1 & int andBit = a & b; Bitwise AND

2 | int orBit = a | b; Bitwise OR

3 ^ int xorBit = a ^ b; Bitwise XOR

4 ~ int notBit = ~a; Bitwise NOT

7. Shift Operators
Sr. Operator Statement Purpose
1 << int leftShift = a << 1; Left shift

2 >> int rightShift = a >> 1; Right shift

3 >>> int unsignedShift = a Unsigned right shift


>>> 1;

8. Ternary Operator
Sr. Operator Statement Purpose

1 ?: int max = (a > b) ? a : Find maximum


b;

2 ?: String evenOdd = (a % Even or Odd


2 == 0) ? "Even" :
"Odd";

3 ?: String eligible = (age Voting eligibility


>= 18) ? "Eligible" :
"Not Eligible";

9. Type Comparison Operator


Sr. Operator Statement Purpose

1 instanceof boolean isString = obj Check object type


instanceof String;

Arithmetic Operators

Write a Java program to perform addition, subtraction, multiplication, division, and


modulus of two numbers.

class ArithmeticDemo {

public static void main(String[] args) {

int a = 10, b = 3;

[Link]("Addition = " + (a + b));


[Link]("Subtraction = " + (a - b));

[Link]("Multiplication = " + (a * b));

[Link]("Division = " + (a / b));

[Link]("Modulus = " + (a % b));

2. Unary Operators

Demonstrate pre-increment, post-increment, pre-decrement and post-decrement.

class UnaryDemo {

public static void main(String[] args) {

int a = 5;

[Link](a++); // post increment

[Link](++a); // pre increment

[Link](a--); // post decrement

[Link](--a); // pre decrement

3. Assignment Operators

Write a Java program to demonstrate compound assignment operators.

class AssignmentDemo {

public static void main(String[] args) {

int a = 10;
a += 5;

a -= 3;

a *= 2;

a /= 4;

a %= 3;

[Link]("Final value of a = " + a);

4. Relational Operators

Write a Java program to compare two numbers.

class RelationalDemo {

public static void main(String[] args) {

int a = 10, b = 20;

[Link](a == b);

[Link](a != b);

[Link](a > b);

[Link](a < b);

[Link](a >= b);

[Link](a <= b);

}
5. Logical Operators

Check whether a number lies between 10 and 50.

class LogicalDemo {

public static void main(String[] args) {

int a = 25;

if (a > 10 && a < 50) {

[Link]("Number is in range");

} else {

[Link]("Number is out of range");

6. Bitwise Operators

Demonstrate bitwise AND, OR, XOR and NOT.

class BitwiseDemo {

public static void main(String[] args) {

int a = 5, b = 3;

[Link](a & b);

[Link](a | b);

[Link](a ^ b);

[Link](~a);

}
}

7. Shift Operators

Demonstrate left shift and right shift operations.

class ShiftDemo {

public static void main(String[] args) {

int a = 8;

[Link](a << 1);

[Link](a >> 1);

[Link](a >>> 1);

8. Ternary Operator

Find the maximum of two numbers using ternary operator.

class TernaryDemo {

public static void main(String[] args) {

int a = 10, b = 20;

int max = (a > b) ? a : b;

[Link]("Maximum = " + max);

9. Type Comparison Operator (instanceof)

Check whether an object belongs to String class.


class InstanceOfDemo {

public static void main(String[] args) {

Object obj = "Hello";

if (obj instanceof String) {

[Link]("Object is of type String");

Home Assignment

1. Write a program to check whether a given number is even or odd.


2. Write a program to find the greater number between two given numbers.
3. Write a program to increase a salary by 10% and display the updated salary.
4. Write a program to check whether a student is eligible for voting based on age.
5. Write a program to check whether a given year is a leap year.

You might also like