Java for Beginners
Java is a high-level, object-oriented programming language developed by Sun Microsystems
(now Oracle). It is one of the most widely used programming languages for building desktop
applications, Android apps, web applications, enterprise software, and backend systems.
One of Java's biggest strengths is:
"Write Once, Run Anywhere (WORA)"
This means Java programs can run on any operating system that has a Java Virtual Machine
(JVM).
Why Learn Java?
Beginner-friendly
Object-Oriented Programming (OOP)
Platform independent
Strongly typed language
Excellent for learning programming fundamentals
Widely used in industry
Java Architecture
Java Code (.java)
Compiler (javac)
Bytecode (.class)
Java Virtual Machine (JVM)
Runs on Windows, Linux, macOS
Installing Java
Check if Java is installed:
java -version
javac -version
Compile a program:
javac [Link]
Run it:
java Main
Your First Program
public class Main {
public static void main(String[] args) {
[Link]("Hello World!");
Output:
Hello World!
Understanding the Code
public class Main
public → Accessible from anywhere.
class → Defines a class.
Main → Class name (must match the filename).
public static void main(String[] args)
This is the entry point of every Java program.
public → Can be accessed by the JVM.
static → Can run without creating an object.
void → Doesn't return a value.
main() → The first method executed.
String[] args → Stores command-line arguments.
Printing Output
[Link]("Hello");
Prints with a new line.
[Link]("Hello");
Prints without a new line.
[Link]("Age: %d", 20);
Formatted output.
Variables
int age = 20;
double salary = 25000.50;
char grade = 'A';
boolean passed = true;
String name = "Noel";
Primitive Data Types
Type Example
byte 10
short 200
int 1000
long 100000L
float 3.14f
double 3.14159
char 'A'
boolean true
Taking User Input
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
[Link]();
Operators
Arithmetic
int a = 10;
int b = 3;
[Link](a + b);
[Link](a - b);
[Link](a * b);
[Link](a / b);
[Link](a % b);
Comparison
a == b
a != b
a>b
a<b
a >= b
a <= b
Logical
&&
||
If Statement
int age = 20;
if (age >= 18) {
[Link]("Adult");
} else {
[Link]("Minor");
Switch Statement
int day = 3;
switch(day){
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Other Day");
Loops
For Loop
for(int i = 1; i <= 5; i++){
[Link](i);
While Loop
int i = 1;
while(i <= 5){
[Link](i);
i++;
Do-While Loop
int i = 1;
do{
[Link](i);
i++;
}while(i <= 5);
Arrays
int[] numbers = {10,20,30,40};
[Link](numbers[0]);
Loop through an array:
for(int num : numbers){
[Link](num);
Methods
public static int add(int a, int b){
return a + b;
public static void main(String[] args){
int result = add(5,7);
[Link](result);
Classes and Objects
class Student{
String name;
void display(){
[Link](name);
}
public class Main{
public static void main(String[] args){
Student s = new Student();
[Link] = "Alice";
[Link]();
Constructor
class Student{
String name;
Student(String n){
name = n;
The Four Pillars of OOP
Encapsulation
Keep data private and provide controlled access using getters and setters.
Inheritance
One class inherits features from another.
class Animal{
void sound(){
[Link]("Animal Sound");
class Dog extends Animal{
Polymorphism
class Animal{
void sound(){
[Link]("Sound");
class Dog extends Animal{
@Override
void sound(){
[Link]("Bark");
}
Abstraction
Hide implementation details while exposing only the necessary functionality.
Exception Handling
try{
int x = 10/0;
catch(Exception e){
[Link]("Error");
finally{
[Link]("Done");
File Input
import [Link];
import [Link];
File file = new File("[Link]");
Scanner input = new Scanner(file);
while([Link]()){
[Link]([Link]());
Collections
ArrayList
import [Link];
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link](names);
Common Java Packages
Package Purpose
[Link] Collections, Scanner
[Link] File handling
[Link] Date and time
[Link] Large number calculations
Mini Project: Calculator
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("First Number: ");
double a = [Link]();
[Link]("Second Number: ");
double b = [Link]();
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
if (b != 0) {
[Link]("Division = " + (a / b));
} else {
[Link]("Cannot divide by zero.");
[Link]();