Write a  Java Operators Program

Java Operators Program

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Operators Program
Java Operators Program

             Unleashing Java Magic: Where Every Line of Code Comes Alive

1.Java Arithmetic Operators

Operator Name Symbol Description Example
Addition + Adds two values 10 + 5 = 15
Subtraction Subtracts right operand from left 10 – 5 = 5
Multiplication * Multiplies two values 10 * 5 = 50
Division / Divides left operand by right 10 / 5 = 2
Modulus % Returns remainder of division 10 % 3 = 1
Increment ++ Increases value by 1 (pre/post increment) a++, ++a
Decrement Decreases value by 1 (pre/post decrement) a–, –a

Write a  Java Operators Program

PROGRAM: Java Operators Program

/* Java Operators Program */

class Main {
public static void main(String[] args) {

// declare variables
int a = 12, b = 5;

// addition operator
System.out.println(“a + b = ” + (a + b));

// subtraction operator
System.out.println(“a – b = ” + (a – b));

// multiplication operator
System.out.println(“a * b = ” + (a * b));

// division operator
System.out.println(“a / b = ” + (a / b));

// modulo operator
System.out.println(“a % b = ” + (a % b));
}
}

2.Java Assignment Operators

Operator Description Example Equivalent To
= Assigns value to a variable a = 10
+= Adds and assigns a += 5 a = a + 5
-= Subtracts and assigns a -= 5 a = a – 5
*= Multiplies and assigns a *= 5 a = a * 5
/= Divides and assigns a /= 5 a = a / 5
%= Modulus and assigns a %= 3 a = a % 3
&= Bitwise AND and assign a &= 5 a = a & 5
= Bitwise OR and assign a
^= Bitwise XOR and assign a ^= 5 a = a ^ 5
<<= Left shift and assign a <<= 2 a = a << 2
>>= Right shift and assign a >>= 2 a = a >> 2
>>>= Unsigned right shift and assign a >>>= 2 a = a >>> 2

Write a  Java Assignment  Operators Program

PROGRAM: Java Assignment Operators Program

/* Java Assignment  Operators Program */

class Main {
public static void main(String[] args) {

// create variables
int a = 4;
int var;

// assign value using =
var = a;
System.out.println(“var using =: ” + var);

// assign value using =+
var += a;
System.out.println(“var using +=: ” + var);

// assign value using =*
var *= a;
System.out.println(“var using *=: ” + var);
}
}

3. Java Relational Operators

Operator Description Example Result
== Checks if two values are equal 10 == 5 false
!= Checks if two values are not equal 10 != 5 true
> Checks if left value is greater than right 10 > 5 true
< Checks if left value is less than right 10 < 5 false
>= Checks if left value is greater or equal 10 >= 10 true
<= Checks if left value is less or equal 10 <= 5 false

Write a  Java Relational  Operators Program

PROGRAM: Java Relational Operators Program

/* Java Relational  Operators Program */

class Main {
public static void main(String[] args) {

// create variables
int a = 7, b = 11;

// value of a and b
System.out.println(“a is ” + a + ” and b is ” + b);

// == operator
System.out.println(a == b); // false

// != operator
System.out.println(a != b); // true

// > operator
System.out.println(a > b); // false

// < operator
System.out.println(a < b); // true

// >= operator
System.out.println(a >= b); // false

// <= operator
System.out.println(a <= b); // true
}
}

4. Java Logical Operators

Operator Description Example Result
&& Logical AND — true only if both are true (10 > 5) && (5 > 2) true
|| Logical OR — true if any one is true (10 > 5) || (5 < 2) true
! Logical NOT — reverses the boolean value !(10 > 5) false

Write a  Java Logical  Operators Program

PROGRAM: Java Logical Operators Program

/* Java Logical Operators Program */

class Main {
public static void main(String[] args) {

// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}

5. Java Unary Operators

Operator Name Description Example Result
++ Increment Operator Increases value by 1 (pre or post) a = 5; ++a a becomes 6
Decrement Operator Decreases value by 1 (pre or post) a = 5; –a a becomes 4
+ Unary Plus Indicates positive value (mostly redundant) +a +a
Unary Minus Negates value -a value becomes negative
! Logical NOT Reverses boolean value !true false
~ Bitwise NOT Flips each bit of the number ~5 Depends on binary

Write a  Java Unary  Operators Program

PROGRAM: Java Unary Operators Program

/* Java Unary Operators Program */

class Main {
public static void main(String[] args) {

// declare variables
int a = 12, b = 12;
int result1, result2;

// original value
System.out.println(“Value of a: ” + a);

// increment operator
result1 = ++a;
System.out.println(“After increment: ” + result1);

System.out.println(“Value of b: ” + b);

// decrement operator
result2 = –b;
System.out.println(“After decrement: ” + result2);
}
}

6. Java Bitwise Operators

Operator Name Description Example Result (Binary)
& Bitwise AND Sets each bit to 1 if both bits are 1 5 & 3 0101 & 0011 = 0001 (1)
| Bitwise OR Sets each bit to 1 if any one bit is 1 5 | 3 0101 | 0011 = 0111 (7)
^ Bitwise XOR Sets each bit to 1 if bits are different 5 ^ 3 0101 ^ 0011 = 0110 (6)
~ Bitwise NOT Inverts each bit ~5 Depends (Two’s complement)
<< Left Shift Shifts bits left (adds zeros on right) 5 << 1 0101 → 1010 (10)
>> Right Shift Shifts bits right (preserves sign bit) 5 >> 1 0101 → 0010 (2)
>>> Unsigned Right Shift Shifts right, fills left with zeros (no sign bit kept) 5 >>> 1 0101 → 0010 (2)

Write a  Java Bitwise Operators Program

PROGRAM: Java Bitwise Operators Program

/* Java Bitwise Operators Program */
Bitwise complement Operation of 35

35 = 00100011 (In Binary)

~ 00100011 
  ________
   11011100  = 220 (In decimal)

Other operators

Java instanceof Operator

The instanceof operator in Java is used to check whether an object belongs to a specific class or implements a specific interface. It returns true if the object is an instance of that class (or its subclass), otherwise false.

class Main {
public static void main(String[] args) {

String str = “Programiz”;
boolean result;

// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println(“Is str an object of String? ” + result);
}
}

Java Ternary Operator

The ternary operator in Java is a shorthand way of writing an if-else statement.
It uses the symbols ? : and evaluates a condition to return one of two values.

class Java {
public static void main(String[] args) {

int februaryDays = 29;
String result;

// ternary operator
result = (februaryDays == 28) ? “Not a leap year” : “Leap year”;
System.out.println(result);
}
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *