Lecture 2 Notes
Type Casting:
Type casting is the process of converting one data type into another.
Types of Casting:
1. Widening (Implicit) Casting:
Automatically converts a smaller data type to a larger data type (no data loss).
Example:
int num = 100;
double d = num; // int to double (widening)
System.out.println(d); // Output: 100.0
2. Narrowing (Explicit) Casting:
Converts a larger data type to a smaller data type (may cause data loss).
Syntax:
dataType smallerVariable = (dataType) largerVariable;
Example:
double pi = 3.14159;
int roundedPi = (int) pi; // Explicit cast from double to int
System.out.println(roundedPi); // Output: 3
Operators:
Operators in Java are special symbols that perform operations on variables and values.
Types of operators in Java:
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Example:
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
2. Relational Operators
Relational operators are used to compare two values.
Example:
int a = 10, b = 5;
System.out.println("a == b: " + (a == b)); // false
System.out.println("a != b: " + (a != b)); // true
System.out.println("a > b: " + (a > b)); // true
System.out.println("a < b: " + (a < b)); // false
3. Logical Operators
Logical operators are used to combine multiple boolean expressions.
Example:
boolean a = true, b = false;
System.out.println("a && b: " + (a && b)); // false
System.out.println("a || b: " + (a || b)); // true
System.out.println("!a: " + (!a)); // false
4. Bitwise Operators
Bitwise operators operate on bits and perform bit-level operations.
Example:
int a = 5, b = 3; // a = 0101, b = 0011
System.out.println("a & b: " + (a & b)); // 1 (0001)
System.out.println("a | b: " + (a | b)); // 7 (0111)
System.out.println("a ^ b: " + (a ^ b)); // 6 (0110)
System.out.println("~a: " + (~a)); // -6 (inverts all bits)
5. Assignment Operators:
Assignment operators are used to assign values to variables.
Example:
int a = 10;
a += 5; // equivalent to a = a + 5
System.out.println("After += : " + a); // 15
a -= 3; // equivalent to a = a - 3
System.out.println("After -= : " + a); // 12
6. Unary Operators
Unary operators operate on a single operand and perform operations like incrementing,
decrementing, negating, etc.
Example:
int a = 10;
System.out.println("Increment: " + (++a)); // 11 (prefix)
System.out.println("Decrement: " + (a--)); // 11 (postfix)
System.out.println("Value after decrement: " + a); // 10
7. Ternary Operator
The ternary operator is a shorthand for the if-else statement.
Syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
int a = 10;
String result = (a > 5) ? "Greater than 5" : "Less than or equal to 5";
System.out.println(result); // Greater than 5
Control Flow Statements:
These are the statements which change the default flow of execution of the program.
There are three types :
1. Conditional statements
2. Looping / Iterative statements
3. Jumping: break and continue
Conditional statements:
Conditional statements in Java allow you to execute different actions based on certain
conditions. They are essential for controlling the flow of a program.
Types of conditional statements:
1. If Statement
The if statement executes a block of code if the specified condition is true.
Syntax:
if (condition) {
// code to be executed if condition is true
}
Example:
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
2. If-Else Statement
The if else statement allows you to execute one block of code if the condition is true and
another block if it is false.
Syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example:
int number = 5;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
3. Else If Statement
You can use else if to check multiple conditions. If the first condition is false, it checks
the next one.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition 2 is true
} else {
// code to be executed if both conditions are false
}
Example:
int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
4. Switch Statement
The switch statement is a cleaner alternative to using multiple if else statements when
you need to evaluate a single expression against several values.
Syntax:
switch (expression) {
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// you can have any number of case statements
default:
// code to be executed if expression doesn't match any case
}
Example:
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
System.out.println("The day is: " + dayName);
5. Ternary Operator: The ternary operator is a shorthand for the if else statement. It has
the following syntax:
Syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
int number = 7;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("The number is " + result);