Home / Java Tutorials and OOP
FREE ENGINEERING LEARNING RESOURCES
Java Tutorials and OOP
Learn Java programming, JVM basics, classes, objects and OOP concepts.
Weeks 1–2 Java foundations
Concept & theory
Java code compiles to bytecode and runs on the JVM. Classes contain methods; main is the program entry point; primitive types include int and boolean.
Example code
import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(n % 2 == 0 ? "Even" : "Odd");
sc.close();
}
}
Code explained
Scanner reads an integer. % computes remainder; ternary operator selects Even or Odd. Closing Scanner releases its resource.
Try it yourself
Read three numbers and print their maximum.
Weeks 3–4 OOP with Java
Concept & theory
Encapsulation bundles data and methods. A constructor initializes an object. private fields protect invariants; getters expose controlled access.
Example code
class Account {
private int balance;
Account(int initial){balance = Math.max(0, initial);}
void deposit(int amount){if(amount > 0) balance += amount;}
int getBalance(){return balance;}
}
class Main {public static void main(String[] a){
Account x = new Account(100); x.deposit(50);
System.out.println(x.getBalance());
}}
Code explained
new creates an Account. Constructor starts balance at 100; deposit adds 50 only if positive; getter returns 150.
Try it yourself
Add withdraw(amount) that rejects negative and overdraft requests.