JAVA Prog Lecture COSC 212 Note
JAVA Prog Lecture COSC 212 Note
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.
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.
Example:
java
Copy code
int age = 25;
double salary = 30000.50;
String name = "Alice";
2
Lec UMOLU CHRIS
4. Operators in Java
java
Copy code
int sum = 5 + 10;
int product = 5 * 10;
double quotient = 10 / 3.0;
• 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");
}
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);
}
java
Copy code
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
6. Methods in Java
4
Lec UMOLU CHRIS
o Methods are blocks of code that perform specific tasks.
Methods allow reusability and code organization.
java
Copy code
public static void greet() {
System.out.println("Hello!");
}
java
Copy code
public static int add(int a, int b) {
return a + b;
}
java
Copy code
public class Car {
String model;
java
Copy code
public class Vehicle {
public void start() {
System.out.println("Vehicle started");
}
}
6
Lec UMOLU CHRIS
• Polymorphism: Allows objects to be treated as instances of their
parent class.
Example:
java
Copy code
try {
int number = Integer.parseInt("ABC");
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " +
e.getMessage());
}
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.
Answer:
java
Copy code
String greeting = "Hello";
Answer:
8
Lec UMOLU CHRIS
java
Copy code
int num = 10;
if (num > 0) {
System.out.println("Positive");
}
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 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