Control Structures
Control Structures
▪ switch-case Statements:
• Definition: switch-case statements evaluate a variable or expression and execute different
blocks of code based on its value.
• Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
// More cases...
default:
System.out.println("Invalid day");
}
• Arrays:
o Definition: Arrays are data structures that store multiple values of the same type in a
single variable.
o Declaration and Initialization:
▪ Definition: Arrays are declared using a specific data type followed by square brackets, and
can be initialized with values at the time of declaration or later.
▪ Example:
int[] numbers = new int[5]; // Declaration with size
int[] primes = {2, 3, 5, 7, 11}; // Declaration with initialization
o Accessing Elements:
▪ Definition: Elements in an array can be accessed using their index, which starts from 0.
▪ Example:
int[] numbers = {10, 20, 30, 40, 50};
int firstNumber = numbers[0]; // Accessing the first element
int thirdNumber = numbers[2]; // Accessing the third element
o Array Operations:
▪ Definition: Arrays support various operations such as traversal, searching, sorting, and
manipulation.
▪ Example:
j
int[] numbers = {5, 3, 8, 1, 9};
// Traversal
for (int num : numbers) {
System.out.println(num);
}
// Searching
int index = Arrays.binarySearch(numbers, 8); // Returns the index of 8
// Sorting
Arrays.sort(numbers); // Sorts the array in ascending order
// Manipulation
numbers[0] = 10; // Modifying the first element
• Methods:
o Definition: Methods are reusable blocks of code that perform specific tasks and enhance
code modularity and reusability.
o Method Definition and Invocation:
▪ Definition: Methods are defined using the method signature, which includes the method
name, return type, and parameters (if any). They can be invoked by calling their name
along with any required arguments.
▪ Example:
// Method definition
public static int add(int a, int b) {
return a + b;
}
// Method invocation
int result = add(5, 3); // Output: 8
o Return Types:
▪ Definition: Methods may or may not return a value. The return type specifies the type of
value that the method returns.
▪ Example:
j
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
o Constructors:
▪ Definition: Constructors have the same name as the class and are used to initialize objects.
▪ Example:
javaCopy code
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
}
Person person1 = new Person("John", 30);
• Inheritance:
o Definition: Inheritance is a mechanism in which a new class inherits properties and
behaviors from an existing class, promoting code reuse and establishing a parent-child
relationship between classes.
o Concept of Inheritance:
▪ *
Sure, let's continue with the detailed explanations and examples for the remaining topics:
• Inheritance:
o Definition: Inheritance is a mechanism in object-oriented programming where a new class
inherits properties and behaviors from an existing class. This promotes code reuse and
establishes a parent-child relationship between classes.
o Concept of Inheritance:
▪ Definition: Inheritance allows a class (subclass or child class) to inherit attributes and
methods from another class (superclass or parent class).
▪ Example:
// Parent class
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
o Types of Inheritance:
▪ Definition: There are different types of inheritance in Java, including single inheritance,
multi-level inheritance, hierarchical inheritance, and multiple inheritance (not supported in
Java).
▪ Example:
// Single Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}
// Multi-level Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}
class C extends B {
// Class members
}
// Hierarchical Inheritance
class A {
// Class members
}
class B extends A {
// Class members
}
class C extends A {
// Class members
}
• File Handling:
o Definition: File handling involves reading from and writing to files on a computer's file
system. It includes various operations such as creating, opening, reading, writing, and
closing files.
o Reading and Writing Files:
▪ Definition: File input/output streams are used to perform file operations in Java.
BufferedReader and BufferedWriter classes are commonly used for efficient file reading
and writing.
▪ Example (Reading from a file):
try (BufferedReader br = new BufferedReader(new
FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
o Exception Handling:
▪ Definition: Exception handling is important when dealing with file operations to handle
errors gracefully. It prevents the program from crashing and allows for proper error
recovery.
▪ Example:
try {
// File operations
} catch (IOException e) {
System.out.println("An error occurred while reading/writing the
file.");
e.printStackTrace();
}
• String Manipulation:
o Definition: String manipulation involves various operations such as concatenation,
substring extraction, searching, and comparison on strings in Java.
o Concatenation and Manipulation:
▪ Definition: Strings in Java are immutable, meaning their values cannot be changed after they
are created. However, you can create new strings by concatenating or manipulating existing
strings.
▪ Example:
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2; // Concatenation
System.out.println(result); // Output: Hello World
o String Methods:
▪ Definition: Java provides numerous built-in methods for string manipulation, including
length(), charAt(), substring(), indexOf(), toUpperCase(), toLowerCase(), etc.
▪ Example:
String str = "Hello, World!";
int length = str.length(); // Returns the length of the string
char firstChar = str.charAt(0); // Returns the character at index 0
String substring = str.substring(7); // Returns the substring from
index 7 to the end
int index = str.indexOf("World"); // Returns the index of the substring
"World"