Skip to main content

In this blogging you know about digital world.

Function in C

  Function Function : In C programming, a function is a self-contained block of code that performs a specific task or a set of related tasks. Functions are used to break down a program into smaller, more manageable pieces, making the code more organized, readable, and reusable. C supports various types of functions, including library functions and user-defined functions. Below, I'll describe functions in C with examples and discuss their types. Function Syntax: return_type function_name(parameters) {     // Function body     // Statements to perform a task     return value; // (optional) Return value } Here's a breakdown of the elements: - `return_type`: Specifies the data type of the value the function returns. Use `void` if the function doesn't return anything. - `function_name`: A unique identifier for the function. - `parameters`: Input values that the function can accept (optional). - `function_body`: Conta...

Operators in Java


Operators in Java 

Operators in Java are symbols that perform operations on variables and values. They allow you to manipulate data and perform calculations in your Java programs. Java provides a variety of operators, which can be categorized into several groups:

1. **Arithmetic Operators:**

   - `+` (Addition): Adds two operands.

   - `-` (Subtraction): Subtracts the right operand from the left operand.

   - `*` (Multiplication): Multiplies two operands.

   - `/` (Division): Divides the left operand by the right operand.

   - `%` (Modulus): Returns the remainder of the division of the left operand by the right operand.

   Example:

   int a = 10, b = 5;

   int sum = a + b;     // sum is 15

   int difference = a - b; // difference is 5

   int product = a * b; // product is 50

   int quotient = a / b; // quotient is 2

   int remainder = a % b; // remainder is 0


2. **Assignment Operators:**

   - `=` (Assignment): Assigns a value to a variable.

   - `+=`, `-=`, `*=`, `/=`, `%=` (Compound Assignment): Perform an operation and assign the result to the left operand.


   Example:

   int x = 5;

   x += 3; // Equivalent to x = x + 3; x is now 8

  

3. **Comparison Operators:**

   - `==` (Equality): Tests if two values are equal.

   - `!=` (Inequality): Tests if two values are not equal.

   - `<` (Less Than): Tests if the left operand is less than the right operand.

   - `>` (Greater Than): Tests if the left operand is greater than the right operand.

   - `<=` (Less Than or Equal To): Tests if the left operand is less than or equal to the right operand.

   - `>=` (Greater Than or Equal To): Tests if the left operand is greater than or equal to the right operand.


   Example:

   int a = 5, b = 10;

   boolean isEqual = (a == b); // isEqual is false

   boolean isNotEqual = (a != b); // isNotEqual is true

   boolean isLessThan = (a < b); // isLessThan is true

 

4. **Logical Operators:**

   - `&&` (Logical AND): Returns true if both operands are true.

   - `||` (Logical OR): Returns true if at least one operand is true.

   - `!` (Logical NOT): Inverts the value of the operand.

   Example:

   boolean x = true, y = false;

   boolean result1 = x && y; // result1 is false

   boolean result2 = x || y; // result2 is true

   boolean result3 = !x;     // result3 is false

  

5. **Bitwise Operators:**

   - `&` (Bitwise AND): Performs a bitwise AND operation.

   - `|` (Bitwise OR): Performs a bitwise OR operation.

   - `^` (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.

   - `~` (Bitwise NOT): Inverts the bits of the operand.

   - `<<` (Left Shift): Shifts the bits to the left.

   - `>>` (Right Shift): Shifts the bits to the right.

   - `>>>` (Unsigned Right Shift): Shifts the bits to the right with zero fill.


   Example:

   int a = 5, b = 3;

   int result1 = a & b; // result1 is 1 (binary: 0101 & 0011 = 0001)

   int result2 = a | b; // result2 is 7 (binary: 0101 | 0011 = 0111)

   int result3 = a ^ b; // result3 is 6 (binary: 0101 ^ 0011 = 0110)

 

6. **Conditional (Ternary) Operator:**

   - `? :` (Conditional Operator): A shorthand way to express an if-else statement.

   Example:

   int x = 5, y = 10;

   int result = (x > y) ? x : y; // result is 10

  

7. **Instanceof Operator:**

   - `instanceof`: Used to test if an object is an instance of a particular class or interface.

   Example:

   Object obj = new String("Hello");

   boolean isString = obj instanceof String; // isString is true

  

These are the main categories of operators in Java. Understanding how to use them is crucial for writing effective and efficient Java programs.

Comments

Popular Posts