Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Operators in Java

Java Operators in Java

Operators in Java

Operators in Java are special symbols used to perform operations on variables and values. Java supports a wide range of operators, which can be categorized into several types based on their functionality.


1. Arithmetic Operators

These operators are used to perform basic arithmetic operations on numeric values.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

Example:

int a = 10, b = 5;System.out.println(a + b);  // Outputs: 15 (Addition)System.out.println(a - b);  // Outputs: 5  (Subtraction)System.out.println(a * b);  // Outputs: 50 (Multiplication)System.out.println(a / b);  // Outputs: 2  (Division)System.out.println(a % b);  // Outputs: 0  (Modulus)

2. Relational (Comparison) Operators

These operators are used to compare two values and return a boolean result (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

int a = 10, b = 5;System.out.println(a == b);  // Outputs: false (Equal)System.out.println(a != b);  // Outputs: true  (Not equal)System.out.println(a > b);   // Outputs: true  (Greater than)System.out.println(a < b);   // Outputs: false (Less than)System.out.println(a >= b);  // Outputs: true  (Greater than or equal to)System.out.println(a <= b);  // Outputs: false (Less than or equal to)

3. Logical Operators

These operators are used to combine multiple boolean expressions.

OperatorDescriptionExample
&&Logical AND (both conditions must be true)a && b
``
!Logical NOT (inverts the boolean value)!a

Example:

boolean a = true, b = false;System.out.println(a && b);  // Outputs: false (Logical AND)System.out.println(a || b);  // Outputs: true  (Logical OR)System.out.println(!a);      // Outputs: false (Logical NOT)

4. Assignment Operators

These operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns a valuea = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5

Example:

int a = 10;a += 5;  // a = a + 5 => 15a -= 3;  // a = a - 3 => 12a *= 2;  // a = a * 2 => 24a /= 4;  // a = a / 4 => 6a %= 3;  // a = a % 3 => 0

5. Unary Operators

These operators are used to operate on a single operand, typically modifying its value.

OperatorDescriptionExample
+Unary plus (indicates positive value)+a
-Unary minus (negates value)-a
++Increment (adds 1 to a variable)a++ or ++a
--Decrement (subtracts 1 from a variable)a-- or --a
!Logical NOT (inverts a boolean value)!a

Example:

int a = 5;System.out.println(++a);  // Outputs: 6 (Pre-increment)System.out.println(a++);  // Outputs: 6 (Post-increment)System.out.println(--a);  // Outputs: 5 (Pre-decrement)System.out.println(a--);  // Outputs: 5 (Post-decrement)

6. Bitwise Operators

Bitwise operators are used to perform bit-level operations on integral types (e.g., int, long).

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise complement (inverts bits)~a
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example:

int a = 5;  // 0101 in binaryint b = 3;  // 0011 in binarySystem.out.println(a & b);  // Outputs: 1  (Bitwise AND, 0101 & 0011 = 0001)System.out.println(a | b);  // Outputs: 7  (Bitwise OR, 0101 | 0011 = 0111)System.out.println(a ^ b);  // Outputs: 6  (Bitwise XOR, 0101 ^ 0011 = 0110)System.out.println(~a);     // Outputs: -6 (Bitwise complement, ~0101 = 1010)System.out.println(a << 1); // Outputs: 10 (Left shift, 0101 << 1 = 1010)System.out.println(a >> 1); // Outputs: 2  (Right shift, 0101 >> 1 = 0010)

7. Ternary Operator

The ternary operator is a shorthand for an if-else statement and is used to assign a value based on a condition.

OperatorDescriptionExample
? :Ternary conditional operatorcondition ? value_if_true : value_if_false

Example:

int a = 10, b = 5;int max = (a > b) ? a : b;  // If a > b, max = a, else max = bSystem.out.println(max);  // Outputs: 10

8. Instanceof Operator

The instanceof operator is used to check whether an object is an instance of a specific class or implements a particular interface.

OperatorDescriptionExample
instanceofChecks if an object is an instance of a class or implements an interfaceobject instanceof ClassName

Example:

String s = "Hello";System.out.println(s instanceof String);  // Outputs: true

Conclusion

Java provides a variety of operators to perform different kinds of operations. Understanding and effectively using these operators will help you manipulate data and control the flow of your program. Here's a summary of the operator categories:

  • Arithmetic Operators: Used for mathematical calculations.

  • Relational Operators: Used for comparisons.

  • Logical Operators: Used for combining boolean expressions.

  • Assignment Operators: Used to assign values to variables.

  • Unary Operators: Used for modifying or operating on a single operand.

  • Bitwise Operators: Used for bit-level operations on integers.

  • Ternary Operator: A shorthand for if-else statements.

  • Instanceof Operator: Used to test object types.

Let me know if you need more details on any of these operators! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql