Day 2 Java Notes
Day 2 Java Notes
- Unary Operators:
- Unary operators act on a single operand.
- Example: `++x` (increment), `--y` (decrement), `!flag` (logical NOT).
- Arithmetic Operators:
- Arithmetic operators perform mathematical operations.
- Examples: `+` (addition), `-` (subtraction), `*` (multiplication), `/`
(division), `%` (modulo).
- Assignment Operators:
- Assignment operators are used to assign values to variables.
- Examples: `=` (assignment), `+=` (add and assign), `-=` (subtract and assign),
`*=` (multiply and assign).
- Short-Hand Operators:
- Short-hand operators combine an operation with assignment.
- Example: `x += 5` is short for `x = x + 5`.
- Comparison Operators:
- Comparison operators compare two values and return a boolean result.
- Examples: `==` (equal to), `!=` (not equal to), `<` (less than), `>` (greater
than).
- Logical Operators:
- Logical operators perform logical operations.
- Examples: `&&` (logical AND), `||` (logical OR), `!` (logical NOT).
- Bitwise Operators:
- Bitwise operators perform operations on individual bits.
- Examples: `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `<<` (left
shift), `>>` (right shift).
- Ternary Operator:
- The ternary operator is a conditional operator that returns a value based on a
condition.
- Example: `int result = (x > y) ? x : y;`
Control Structures:
- If Statement:
- The `if` statement is used for conditional execution of code.
- Example:
```
if (condition) {
// Code to execute if the condition is true
}
```
- If-Else Statement:
- The `if-else` statement allows for two branches of code, one for when the
condition is true and one for when it's false.
- Example:
```
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
```
- Nested If-Else:
- Nested `if-else` statements are `if-else` statements inside other `if-else`
statements.
- Example:
```
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
} else {
// Code to execute if condition1 is true but condition2 is false
}
} else {
// Code to execute if condition1 is false
}
```
- If-Else Ladder:
- An `if-else` ladder consists of multiple `if-else` statements in a sequence.
- Example:
```
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
// Code for condition3
} else {
// Code if none of the conditions are true
}
```
- Switch Statement:
- The `switch` statement is used to select one of many code blocks to execute.
- Example:
```
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if none of the cases match
}
```
Loops:
- While Loop:
- The `while` loop executes a block of code repeatedly as long as a condition is
true.
- Example:
```
while (condition) {
// Code to execute while the condition is true
}
```
- Do-While Loop:
- The `do-while` loop is similar to the `while` loop but ensures that the code
block is executed at least once.
- Example:
```
do {
// Code to execute at least once
} while (condition);
```
- For Loop:
- The `for` loop is used for iterating over a range or collection.
- Example:
```
for (int i = 0; i < 10; i++) {
// Code to execute in each iteration
}
```
- Break Statement:
- The `break` statement is used to exit a loop prematurely.
- Example: `break;`
- Continue Statement:
- The `continue` statement is used to skip the current iteration of a loop and
continue with the next one.
- Example: `continue;`
- Nested Loops:
- Loops can be nested inside one another to create complex patterns or iterate
over multi-dimensional data structures.
- ForEach Loop:
- The `forEach` loop (or enhanced for loop) is used for iterating over elements
in an array or collection.
- Example:
```
for (int item : array) {
// Code to process each item in the array
}
```
- Pattern Programs:
- Pattern programs involve printing specific patterns of characters or numbers
using loops.