Lecture Note: Introduction to Programming in Java
1. Overview of Java
• What is Java?
o Java is a high-level, object-oriented programming language
developed by Sun Microsystems (now owned by Oracle). It is
platform-independent, meaning Java applications can run on
any system with the Java Virtual Machine (JVM).
o Java is widely used for building web, mobile, desktop, and
enterprise applications.
• Key Features of Java:
o Platform Independence: "Write once, run anywhere"
(WORA) - Java code can run on any operating system with a
JVM.
o Object-Oriented: Supports concepts like classes, objects,
inheritance, encapsulation, polymorphism, and abstraction.
o Robust and Secure: Java has strong memory management
and provides built-in security features.
o Multi-threaded: Allows concurrent processing, making it
suitable for applications that require high performance.
2. Java Program Structure and Syntax
• Structure of a Java Program:
o Java programs consist of classes, methods, and fields
(variables).
o The main method serves as the entry point of any
standalone Java application.
Example of a Simple Java Program:
java
1
Lec UMOLU CHRIS
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
• Explanation:
o public class HelloWorld defines a class named HelloWorld.
o public static void main(String[] args) is the main method,
where program execution begins.
o System.out.println prints text to the console.
3. Data Types and Variables in Java
• Primitive Data Types:
o int: Used for integer values (e.g., int num = 10;)
o double: Used for floating-point numbers (e.g., double price =
19.99;)
o char: Represents a single character (e.g., char grade = 'A';)
o boolean: Represents true or false values (e.g., boolean
isActive = true;)
• Declaring Variables:
o In Java, variables are declared with a data type followed by
the variable name.
Example:
java
Copy code
int age = 25;
double salary = 30000.50;
String name = "Alice";
2
Lec UMOLU CHRIS
4. Operators in Java
• Arithmetic Operators: +, -, *, /, % (modulus)
• Comparison Operators: ==, !=, <, >, <=, >=
• Logical Operators: && (AND), || (OR), ! (NOT)
Example of Arithmetic Operations:
java
Copy code
int sum = 5 + 10;
int product = 5 * 10;
double quotient = 10 / 3.0;
5. Control Structures in Java
• Conditional Statements:
o If...Else Statement: Allows conditional execution of code.
java
Copy code
int age = 20;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
• Switch Statement: An alternative to multiple if-else statements.
java
Copy code
char grade = 'A';
switch (grade) {
case 'A':
3
Lec UMOLU CHRIS
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
default:
System.out.println("Try Harder");
break;
}
• Loops:
o For Loop: Executes a block of code a specified number of
times.
java
Copy code
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
o While Loop: Executes a block of code as long as a condition
is true.
java
Copy code
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
6. Methods in Java
• Defining and Using Methods:
4
Lec UMOLU CHRIS
o Methods are blocks of code that perform specific tasks.
Methods allow reusability and code organization.
Example of a Simple Method:
java
Copy code
public static void greet() {
System.out.println("Hello!");
}
• Method with Parameters and Return Type:
java
Copy code
public static int add(int a, int b) {
return a + b;
}
7. Object-Oriented Programming in Java
• Classes and Objects:
o Class: A blueprint for objects. It defines properties
(fields) and behaviors (methods).
o Object: An instance of a class.
Example of a Class and Object:
java
Copy code
public class Car {
String model;
public Car(String model) {
this.model = model;
5
Lec UMOLU CHRIS
}
public void displayModel() {
System.out.println("Model: " + model);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota");
myCar.displayModel();
}
}
8. Inheritance and Polymorphism
• Inheritance: Allows a new class (subclass) to inherit properties
and methods from an existing class (superclass).
java
Copy code
public class Vehicle {
public void start() {
System.out.println("Vehicle started");
}
}
public class Car extends Vehicle {
public void honk() {
System.out.println("Car honking");
}
}
6
Lec UMOLU CHRIS
• Polymorphism: Allows objects to be treated as instances of their
parent class.
9. Exception Handling in Java
• Try...Catch Block: Used to handle exceptions (runtime errors).
Example:
java
Copy code
try {
int number = Integer.parseInt("ABC");
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " +
e.getMessage());
}
10. Basic Java APIs
• Java Standard Library: Java provides a rich set of APIs for
handling I/O, collections, networking, and more.
• Common Packages:
o java.util: Contains utility classes like ArrayList, HashMap.
o java.io: For input and output operations.
o java.lang: Contains core classes like Math, String, System.
15 Exam Questions and Answers
Question 1: What is the main method in Java, and why is it
required?
7
Lec UMOLU CHRIS
Answer: The main method (public static void main(String[] args)) is the
entry point of any standalone Java application. It’s where execution
starts in a Java program.
Question 2: Explain what the JVM is.
Answer: The JVM (Java Virtual Machine) is an environment that
executes Java bytecode, enabling Java programs to run on any device
or operating system with a JVM installed.
Question 3: How do you declare a variable of type String with the
value "Hello" in Java?
Answer:
java
Copy code
String greeting = "Hello";
Question 4: What is the difference between == and .equals() in
Java?
Answer: == checks if two references point to the same memory
location, while .equals() checks if two objects are logically equivalent.
Question 5: Write a Java if statement to check if a number is
positive.
Answer:
8
Lec UMOLU CHRIS
java
Copy code
int num = 10;
if (num > 0) {
System.out.println("Positive");
}
Question 6: What is a for loop used for? Provide an example.
Answer: A for loop is used to iterate over a block of code a set number
of times.
Example:
java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Question 7: What does the break keyword do in a loop?
Answer: break exits the loop immediately, stopping further iterations.
Question 8: What is a class in Java?
Answer: A class is a blueprint for creating objects. It defines
properties (fields) and behaviors (methods).
Question 9: What is an object in Java?
9
Lec UMOLU CHRIS
Answer: An object is an instance of a class, representing a real-world
entity with properties and behaviors.
Question 10: What is inheritance in Java?
Answer: Inheritance allows a class (subclass) to inherit properties and
methods from another class (superclass).
Question 11: Define polymorphism in Java.
Answer: Polymorphism allows objects to be treated as instances of
their parent class, enabling flexibility in method calls.
Question 12: Write a simple method named add that returns the
sum of two integers.
Answer:
java
Copy code
public static int add(int a, int b) {
return a + b;
}
10
Lec UMOLU CHRIS