DEV Community

Ajay Raja
Ajay Raja

Posted on

class_Till_Now(P-3)

1)Operators:

  • Operators in Java are special symbols that perform operations on variables and values.
  • They are used to manipulate data and variables within expressions

Types of Operators in Java:

1.Arithmetic operators
2.Assignment operators
3.Relational operators
4.Logical operators
5.Bitwise operators
6.Ternary operator
7.Shift Operators
Enter fullscreen mode Exit fullscreen mode

1.Arithmetic:

  • used to perform simple arithmetic operations on primitive and non-primitive data types.

    • : Multiplication / : Division % : Modulo
    • : Addition
    • : Subtraction

2.Assignment:

  • '=' The assignment operator is used to assign a value to any variable.
  • the assignment operator can be combined with others to create shorthand compound statements. Example: example, a += 5 replaces a = a + 5.
1. += , Add and assign.
2. -= , Subtract and assign.
3. *= , Multiply and assign.
4. /= , Divide and assign.
5. %= , Modulo and assign.
Enter fullscreen mode Exit fullscreen mode

3.Relational:

  • used to check for relations like equality, greater than, and less than
  • They return boolean results

Relational operators compare values and return Boolean results:

1. == , Equal to.
2. != , Not equal to.
3. < , Less than.
4. <= , Less than or equal to.
5. > , Greater than.
6. >= , Greater than or equal to.
Enter fullscreen mode Exit fullscreen mode

4.Logical:

  • used to perform "logical AND" and "logical OR" operations.
  • second condition is not evaluated if the first is false.

Conditional operators are:

  1. &&, Logical AND: returns true when both conditions are true.
  2. ||, Logical OR: returns true if at least one condition is true.
  3. !, Logical NOT: returns true when a condition is false and vice-versa

5.Bitwise:

  • used when performing update and query operations of the Binary indexed trees
  • used to perform the manipulation of individual bits of a number and with any of the integer types.
1. & (Bitwise AND): returns bit-by-bit AND of input values.
2. | (Bitwise OR): returns bit-by-bit OR of input values.
3. ^ (Bitwise XOR): returns bit-by-bit XOR of input values.
4. ~ (Bitwise Complement): inverts all bits (one's complement).
Enter fullscreen mode Exit fullscreen mode

6.Ternary:

  • It is a shorthand version of the if-else statement
  • s a shorthand version of the if-else statement

       **condition ? if true : if false**
    

7.Shift Operators:

  • are used to shift the bits of a number left or right.
  • They can be used when we have to multiply or divide a number by two.

1.<< (Left shift): Shifts bits left, filling 0s (multiplies by a power of two).
2.>> (Signed right shift): Shifts bits right, filling 0s (divides by a power of two), with the leftmost bit depending on the sign.
3.>>> (Unsigned right shift): Shifts bits right, filling 0s, with the leftmost bit always 0.

2)Datatypes:

  • Data types in Java are of different sizes and values that can be stored in a variable

Java has two categories:
1.Primitive
2.Non primitive

Primitive Data Types in Java:

Non-Primitive Data Types in java:

  • contain a memory address of variable values
  • Reference types won’t store the variable value directly in memory. They are strings, objects, arrays, etc.

https://gitlab.com/ajayraja1817-group/javabeginers.git

In this I made some example programs for primitive and non primitive
Data types.

3)Initialization

  • Initialization is the process of assigning an initial value to a declared variable.
  • This is the first time a value is stored in the memory location reserved for the variable.
  • Initialization assigns initial values to variables.

Example:

age = 30; // Initializes the 'age' variable with the value 30
name = "Alice"; // Initializes the 'name' variable with the value "Alice"
isActive = true; // Initializes the 'isActive' variable with the value true

Declaration:

  • Declaration is the process of defining a variable by specifying its data type and a unique name.
  • This informs the Java compiler about the type of data the variable will hold and reserves memory space for it.
  • Declaration declares the creation of variables and functions

Example:

int age; // Declares an integer variable named 'age'
String name; // Declares a String variable named 'name'
boolean isActive; // Declares a boolean variable named 'isActive'

4)Instance variable:

  • These variables are declared within a class but outside a method
  • It always get default value.
  • Created when an object is created and destroyed when the object is destroyed.
  • Accessed through objects (e.g., obj.variableName).

Local variable:

  • These variables are declared within a method but do not get any default value.
  • Created when the method is invoked and destroyed once method execution ends.
  • Must be initialized before use (Java does not assign default values).
  • Accessed directly (no need for object).

https://gitlab.com/ajayraja1817-group/javabeginers.git

In this i done the sample program by seeing sites not chatgpt.
I took some examples from there.

Top comments (0)