What are main operators in java with examples

Zoe 159 Published: 09/30/2024

What are main operators in java with examples

I'd be happy to explain Java operators and provide examples. Here's the information you requested:

Operators in Java are used to perform operations on values, such as arithmetic, comparison, logical, assignment, bitwise, and unary operations.

Here are some of the main operators in Java with examples:

Arithmetic Operators

Addition: a + b (e.g., int a = 5; int b = 3; System.out.println(a + b);) Subtraction: a - b Multiplication: a * b Division: a / b Modulus (remainder): a % b

Example code:

public class Main {

public static void main(String[] args) {

int a = 5;

int b = 3;

System.out.println("Addition: " + (a + b));

System.out.println("Subtraction: " + (a - b));

System.out.println("Multiplication: " + (a * b));

System.out.println("Division: " + ((double) a / b)); // use double for division

System.out.println("Modulus: " + (a % b));

}

}

Comparison Operators

Equal to: a == b Not equal to: a != b Greater than: a > b Less than: a < b Greater than or equal to: a >= b Less than or equal to: a <= b

Example code:

public class Main {

public static void main(String[] args) {

int a = 5;

int b = 3;

System.out.println("Equal to: " + (a == b));

System.out.println("Not equal to: " + (a != b));

System.out.println("Greater than: " + (a > b));

System.out.println("Less than: " + (a < b));

System.out.println("Greater than or equal to: " + (a >= b));

System.out.println("Less than or equal to: " + (a <= b));

}

}

Logical Operators

And (logical): a && b (short-circuit evaluation) Or (logical): a || b (short-circuit evaluation) Not (logical): !a

Example code:

public class Main {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

System.out.println("And: " + (a && b));

System.out.println("Or: " + (a || b));

System.out.println("Not: " + (!a));

}

}

Assignment Operators

Assign: a = b Add and assign: a += b (equivalent to a = a + b) Subtract and assign: a -= b (equivalent to a = a - b) Multiply and assign: a *= b (equivalent to a = a * b) Divide and assign: a /= b (equivalent to a = a / b) Modulus and assign: a %= b (equivalent to a = a % b)

Example code:

public class Main {

public static void main(String[] args) {

int a = 5;

int b = 3;

System.out.println("Assign: " + (a = b));

System.out.println("Add and assign: " + ((a += b)));

System.out.println("Subtract and assign: " + ((a -= b))));

System.out.println("Multiply and assign: " + ((a *= b))));

System.out.println("Divide and assign: " + ((a /= b))));

System.out.println("Modulus and assign: " + ((a %= b))));

}

}

Bitwise Operators

And (bitwise): a & b (performs a bitwise AND operation) Or (bitwise): a | b (performs a bitwise OR operation) XOR (bitwise): a ^ b (performs a bitwise XOR operation) Complement (bitwise): ~a Left shift: a << b (shifts the bits of a to the left by b positions) Right shift: a >> b (shifts the bits of a to the right by b positions)

Example code:

public class Main {

public static void main(String[] args) {

int a = 5; // binary: 101

int b = 3; // binary: 11

System.out.println("And (bitwise): " + ((a & b))); // binary: 001

System.out.println("Or (bitwise): " + ((a | b))); // binary: 111

System.out.println("XOR (bitwise): " + ((a ^ b))); // binary: 110

System.out.println("Complement (bitwise): " + (~a)); // binary: 010

System.out.println("Left shift: " + ((a << 1))); // binary: 1010

System.out.println("Right shift: " + ((a >> 1)))); // binary: 0101

}

}

Unary Operators

Plus: +a (no operation, equivalent to just a) Minus: -a Complement: ~a

Example code:

public class Main {

public static void main(String[] args) {

int a = 5;

System.out.println("Plus: " + (+a));

System.out.println("Minus: " + (-a));

System.out.println("Complement: " + (~a));

}

}

I hope this helps! Let me know if you have any questions or need further clarification.

What are main operators in java javatpoint

I'm happy to help! Here's the information on main operators in Java:

In Java, operators are special symbols used to perform operations on variables and values. There are various types of operators in Java, including arithmetic, comparison, logical, bitwise, shift, and assignment operators.

Arithmetic Operators

Java provides five basic arithmetic operators that perform addition, subtraction, multiplication, division, and modulus operations:

+ (Addition): This operator adds two numbers together. For example, a + b returns the sum of a and b. - (Subtraction): This operator subtracts one number from another. For example, a - b returns the difference between a and b. * (Multiplication): This operator multiplies two numbers together. For example, a * b returns the product of a and b. / (Division): This operator divides one number by another. For example, a / b returns the quotient of a divided by b. % (Modulus): This operator returns the remainder of dividing one number by another. For example, a % b returns the remainder of a divided by b.

Comparison Operators

Java provides six basic comparison operators that compare values:

== (Equal): This operator checks if two values are equal. For example, a == b returns a boolean indicating whether a and b are equal. != (Not Equal): This operator checks if two values are not equal. For example, a != b returns a boolean indicating whether a and b are not equal. < (Less Than): This operator checks if one value is less than another. For example, a < b returns a boolean indicating whether a is less than b. > (Greater Than): This operator checks if one value is greater than another. For example, a > b returns a boolean indicating whether a is greater than b. <= (Less Than or Equal To): This operator checks if one value is less than or equal to another. For example, a <= b returns a boolean indicating whether a is less than or equal to b. >= (Greater Than or Equal To): This operator checks if one value is greater than or equal to another. For example, a >= b returns a boolean indicating whether a is greater than or equal to b.

Logical Operators

Java provides three basic logical operators that combine two conditions:

&& (And): This operator checks if both conditions are true. For example, a && b returns a boolean indicating whether both a and b are true. || (Or): This operator checks if at least one condition is true. For example, a || b returns a boolean indicating whether either a or b is true. ! (Not): This operator negates the result of another condition. For example, !a returns a boolean that is the opposite of a.

Bitwise Operators

Java provides five basic bitwise operators that operate on binary values:

& (And): This operator performs a bitwise AND operation. For example, a & b returns an integer that is the result of performing a bitwise AND operation on a and b. | (Or): This operator performs a bitwise OR operation. For example, a | b returns an integer that is the result of performing a bitwise OR operation on a and b. ^ (Exclusive Or): This operator performs a bitwise XOR operation. For example, a ^ b returns an integer that is the result of performing a bitwise XOR operation on a and b. ~ (Not): This operator inverts the bits of an integer. For example, ~a returns an integer that is the opposite of a. << (Left Shift): This operator shifts the bits of an integer to the left. For example, a << b returns an integer that is the result of shifting the bits of a to the left by b positions. >> (Right Shift): This operator shifts the bits of an integer to the right. For example, a >> b returns an integer that is the result of shifting the bits of a to the right by b positions.

Assignment Operators

Java provides several assignment operators that assign a value to a variable:

= (Equal): This operator assigns a value to a variable. For example, a = b sets the value of a to be equal to the value of b. += (Add and Assign): This operator adds a value to a variable and assigns the result. For example, a += b adds b to a and assigns the result. -= (Subtract and Assign): This operator subtracts a value from a variable and assigns the result. For example, a -= b subtracts b from a and assigns the result. *= (Multiply and Assign): This operator multiplies a value by a variable and assigns the result. For example, a *= b multiplies a by b and assigns the result. /= (Divide and Assign): This operator divides a value by a variable and assigns the result. For example, a /= b divides a by b and assigns the result. %= (Modulus and Assign): This operator calculates the remainder of dividing a value by a variable and assigns the result. For example, a %= b returns the remainder of a divided by b and assigns the result.

These are the main operators in Java that you can use to perform various operations on variables and values.