ROCKVIEW UNIVERSITY
DEPARTMENT OF
COMPUTER SCIENCE JAVA
PROGRAMMING: UNIT 1
JANUARY 1, 2025
ROCKVIEW UNIVERSTY
By Mr Kalaluka Nyambe
Java Programming Lecturer Notes
1. Introduction to Java
• Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle).
• It follows the Write Once, Run Anywhere (WORA) principle.
• Java applications are compiled into bytecode, which runs on the Java Virtual Machine
(JVM).
• Java is used for web applications, mobile applications (Android), desktop software, and
enterprise systems.
Key Features of Java
• Platform Independence – Runs on any system with a JVM.
• Object-Oriented – Supports principles like Encapsulation, Inheritance, and
Polymorphism.
• Robust and Secure – Includes memory management, exception handling, and a security
model.
• Multi-threading Support – Allows concurrent execution of tasks.
• Rich API – Comes with built-in libraries for networking, data structures, and GUI
development.
2. Setting Up Java Development Environment
Installation
• Download and install Java Development Kit (JDK) from Oracle.
• Set up Java PATH environment variable.
• Install an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or
NetBeans.
Writing and Running a Java Program
1. Create a file: [Link]
2. Write the following code:
3. public class HelloWorld {
4. public static void main(String[] args) {
5. [Link]("Hello, World!");
6. }
7. }
8. Compile: javac [Link]
9. Run: java HelloWorld
3. Java Syntax and Basic Constructs
Variables and Data Types
• Primitive data types:
o int (e.g., int age = 25;)
o double (e.g., double pi = 3.14;)
o char (e.g., char letter = 'A';)
o boolean (e.g., boolean isJavaFun = true;)
• Non-primitive data types:
o String (e.g., String name = "John";)
o Arrays
o Objects
Operators
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
• Assignment: =, +=, -=, *=, /=
• Increment/Decrement: ++, --
Control Flow Statements
Conditional Statements
• if, if-else, switch
• if (age > 18) {
• [Link]("Adult");
• } else {
• [Link]("Minor");
• }
Loops
• for, while, do-while
• for (int i = 0; i < 5; i++) {
• [Link]("Iteration: " + i);
• }
4. Object-Oriented Programming (OOP) in Java
Java follows OOP principles:
1. Encapsulation – Wrapping data and methods in a class.
2. Inheritance – One class can inherit properties of another.
3. Polymorphism – Method overloading and method overriding.
4. Abstraction – Hiding implementation details from the user.
Classes and Objects
class Car {
String brand;
int speed;
void displayInfo() {
[Link]("Brand: " + brand + ", Speed: " + speed);
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
[Link] = "Toyota";
[Link] = 120;
[Link]();
Constructors
• Special methods called when an object is created.
• Default constructor and parameterized constructor.
class Student {
String name;
// Constructor
Student(String studentName) {
name = studentName;
void show() {
[Link]("Student Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("John");
[Link]();
5. Exception Handling
• Used to handle runtime errors and prevent program crashes.
• Try-Catch Block
• try {
• int result = 10 / 0; // This will cause an exception
• } catch (ArithmeticException e) {
• [Link]("Cannot divide by zero!");
• }
6. Java Collections Framework
Java provides data structures like:
• List (ArrayList, LinkedList)
• Set (HashSet, TreeSet)
• Map (HashMap, TreeMap)
Example:
import [Link];
public class ListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link](names);
7. File Handling in Java
• Reading and writing files using FileReader and FileWriter.
• import [Link];
• import [Link];
• public class FileExample {
• public static void main(String[] args) {
• try {
• FileWriter writer = new FileWriter("[Link]");
• [Link]("Hello, Java File Handling!");
• [Link]();
• } catch (IOException e) {
• [Link]("An error occurred.");
• }
• }
• }
8. Multithreading
• Allows concurrent execution of tasks.
• Implemented using Thread class or Runnable interface.
Example using Thread:
class MyThread extends Thread {
public void run() {
[Link]("Thread is running...");
public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
9. Java GUI with Swing
Java Swing provides GUI components:
import [Link].*;
public class GUIExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple GUI");
JButton button = new JButton("Click Me");
[Link](button);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
10. JDBC – Connecting Java to Databases
• Use JDBC (Java Database Connectivity) to interact with databases.
• Example: Connecting to MySQL
import [Link];
import [Link];
import [Link];
public class DatabaseExample {
public static void main(String[] args) {
try {
Connection con = [Link]("jdbc:mysql://localhost:3306/mydb",
"root", "password");
Statement stmt = [Link]();
[Link]("INSERT INTO students (name, age) VALUES ('Alice', 20)");
[Link]("Data inserted!");
} catch (Exception e) {
[Link](e);
}
Conclusion
Java is a powerful language with a wide range of applications. Mastering its core concepts will
allow you to develop desktop, web, and mobile applications efficiently.
Would you like additional examples, exercises, or practical projects?