What are main operators in java w3schools

Mary 154 Published: 10/15/2024

What are main operators in java w3schools

I'd be happy to help!

In Java, an operator is a symbol used to perform operations on variables and values. There are several types of operators in Java, including:

Arithmetic Operators

These operators are used for performing mathematical operations on numbers.

+ (Addition) - This operator adds two numbers together. - (Subtraction) - This operator subtracts one number from another. * (Multiplication) - This operator multiplies two numbers together.

/ (Division) - This operator divides one number by another. % (Modulus) - This operator returns the remainder of dividing one number by another. ++ and -- (Increment and Decrement) - These operators increment or decrement a variable by 1.

Relational Operators

These operators are used for comparing two values to determine if they satisfy certain conditions.

== (Equal To) - This operator checks if two values are equal. != (Not Equal To) - This operator checks if two values are not equal. < (Less Than) - This operator checks if one value is less than another. > (Greater Than) - This operator checks if one value is greater than another. <= and >= (Less Than or Equal To, Greater Than or Equal To) - These operators check if one value is less than or equal to, or greater than or equal to, another.

Logical Operators

These operators are used for performing logical operations on boolean values.

&& (And) - This operator returns true only if both operands are true. || (Or) - This operator returns true if either operand is true. ! (Not) - This operator returns the opposite of a boolean value.

Assignment Operators

These operators are used for assigning values to variables.

= (Assignment) - This operator assigns a value to a variable. +=, -=, *=, /=, %= (Compound Assignment) - These operators perform an operation and assign the result to a variable.

Bitwise Operators

These operators are used for performing operations on the binary level.

& (Bitwise And) - This operator performs a bitwise AND operation. | (Bitwise Or) - This operator performs a bitwise OR operation. ^ (Bitwise Xor) - This operator performs a bitwise XOR operation. ~ (Bitwise Not) - This operator inverts the bits of a value. << and >> (Shift Left and Right) - These operators shift the bits of a value to the left or right.

Miscellaneous Operators

, (Comma) - This operator is used as a separator between function arguments. = (Assignment) - This operator assigns a value to a variable. -> and <=> (Arrow and Arrow Assignment) - These operators are used for lambda expressions and method references.

These are the main operators in Java, and they can be used to perform various operations on variables and values.

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.