University of Johannesburg
Department of Computer Science and Informatics
Course: Computer Science 2B
Topic: Java Basics
Year: 2025
Computer Science 2B – Java Basics
1. Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun
Microsystems in 1995. It is widely used for web applications, mobile development, and
enterprise solutions. Its core philosophy, 'Write Once, Run Anywhere,' emphasizes
platform independence achieved through the Java Virtual Machine (JVM).
2. Java Platform Components
The Java platform consists of three main components: • Java Development Kit (JDK) –
provides tools for developing Java applications. • Java Runtime Environment (JRE) –
supplies libraries and the JVM to run applications. • Java Virtual Machine (JVM) –
executes compiled Java bytecode, enabling portability.
3. Structure of a Java Program
A Java program is structured around classes and methods. The main() method serves as
the entry point: public class HelloWorld { public static void main(String[] args) {
System.out.println("Hello, World!"); } } This program defines a public class and a main
method that prints text to the console.
4. Data Types and Variables
Java supports primitive and reference data types. Primitive types include byte, short, int,
long, float, double, boolean, and char. Variables must be declared with a specific data
type, ensuring type safety and efficient memory management.
5. Operators in Java
Java provides several operators: arithmetic (+, -, *, /, %), relational (==, !=, >, <), logical
(&&, ||, !), assignment (=, +=, -=), and conditional operators. These operators enable
expression evaluation and logical decision-making within programs.
6. Control Flow Statements
Control structures guide program execution. Common types include: • if-else statements
for conditional logic. • switch statements for multi-branch control. • for, while, and do-while
loops for iteration. Example: for (int i = 0; i < 5; i++) { System.out.println(i); }
7. Methods in Java
Methods are reusable blocks of code that perform specific tasks. They may accept
parameters and return values. Example: public static int add(int a, int b) { return a + b; }
Methods enhance modularity, readability, and code reusability.
8. Arrays and Strings
Arrays store multiple values of the same type in contiguous memory locations. Strings
represent sequences of characters and are immutable. Example: int[] numbers = {1, 2, 3,
4, 5}; String name = "Johannesburg";
9. Input and Output Operations
Java utilizes the java.util.Scanner class for console input and System.out for output.
Example: Scanner sc = new Scanner(System.in); String name = sc.nextLine();
System.out.println("Hello, " + name);
10. Summary and Best Practices
Java’s structure emphasizes readability, reliability, and reusability. Programmers are
encouraged to use meaningful variable names, follow indentation standards, and apply
comments for documentation purposes.
Short Revision Questions
1. Define the term 'platform independence' in the context of Java. 2. Differentiate between
the JVM, JRE, and JDK. 3. Write the syntax for a simple Java program that prints your
name. 4. List four primitive data types in Java and give an example for each. 5. Explain the
role of control flow statements in program execution. 6. Provide an example of a Java
method and describe its components. 7. Describe how arrays differ from Strings in Java. 8.
Identify and explain three Java operators. 9. Demonstrate how to read input from a user in
Java. 10. Discuss the importance of code readability and modularity in Java programming.