Variable & Operators | Unit 2 | Class 11

0

Variable

A Java variable is a piece of memory that can contain a data value. A variable thus has a data type. Variables are typically used to store information which your Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.

public class MyClass {

  public static void main(String[] args) {
    int number = 10;
    number = number + 20;
  }
}

Java Variable Types
In Java there are four types of variables:

  • Non-static fields

    A non-static field is a variable that belongs to an object. Objects keep their internal state in non-static fields. Non-static fields are also called instance variables, because they belong to instances (objects) of a class.

  • Static fields

    A static field is a variable that belongs to a class. A static field has the same value for all objects that access it. Static fields are also called class variables.

  • Local variables

    A local variable is a variable declared inside a method. A local variable is only accessible inside the method that declared it.

  •  Parameter variable

    A parameter is a variable that is passed to a method when the method is called. Parameters are also only accessible inside the method that declares them, although a value is assigned to them when the method is called.

Operators

They are used to manipulate primitive data types. Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively. Java has eight different operator types: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.

In Java, operators are classified into different types based on their functionality. Here are the types of operators you mentioned along with examples:

1. Assignment Operators: These are used to assign values to variables.

Example:

   int x = 10; // Assigns the value 10 to the variable x

2. Arithmetic Operators: These are used for basic arithmetic operations.

Example:

   int a = 10;
   int b = 20;
   int sum = a + b; // Addition
   int difference = a - b; // Subtraction
   int product = a * b; // Multiplication
   int quotient = a / b; // Division
   int remainder = a % b; // Modulus

3. Relational Operators: These are used to compare values.

Example:

int x= 10;
    int y = 20;
    boolean result1 = x > y; // Greater than
    boolean result2 = x < y; // Less than
    boolean result3 = x >= y; // Greater than or equal to
    boolean result4 = x <= y; // Less than or equal to
    boolean result5 = x == y; // Equal to
    boolean result6 = x != y; // Not equal to

4. Logical Operators: These are used for logical operations.

Example:

   boolean p = true;
   boolean q = false;
   boolean result1 = p && q; // Logical AND
   boolean result2 = p || q; // Logical OR
   boolean result3 = !p; // Logical NOT

5. Bitwise Operators: These are used for bitwise operations on individual bits of data.

Example:

    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int result1 = a & b; // Bitwise AND (Binary: 0001)
    int result2 = a | b; // Bitwise OR (Binary: 0111)
    int result3 = a ^ b; // Bitwise XOR (Binary: 0110)
    int result4 = ~a;    // Bitwise NOT (Binary: 1010)
    int result5 = a << 1; // Left shift (Binary: 1010, Decimal: 10)
    int result6 = b >> 1; // Right shift (Binary: 0001, Decimal: 1)
  

6. Compound Assignment Operators: These combine assignment and arithmetic or bitwise operations.

Example:

    int x = 10;
    x += 5; // Equivalent to: x = x + 5;
    x -= 5; // Equivalent to: x = x - 5;

7. Conditional Operator (Ternary Operator): This is used for conditional expressions.

Example:

    int a = 10;
    int b = 20; 
    int max = (a > b) ? a : b; // If a is greater than b, max = a, else max = b

8. Type Cast Operator: This is used to convert one data type to another.

Example:

    int x = 10;
    double y = (double) x; // Explicitly converts x to a double data type

Leave a Reply

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