Learn JAVA (Full Tutorial)

Java Introduction

What is Java?
Java is a popular programming language, created in 1995 and owned by Oracle. Over 3 billion devices run Java. It is used for:

  • Mobile apps (especially Android)

  • Desktop apps

  • Web apps and servers

  • Games

  • Database connections

Why Use Java?

  • Platform-independent (Windows, Mac, Linux, Raspberry Pi)

  • Widely used and in high demand

  • Easy to learn and use

  • Free and open-source

  • Secure, fast, and powerful

  • Strong community support

  • Object-oriented: reusable, structured code

  • Close to C++/C#: easy to switch between languages


Installing Java (Windows)

  1. Check if Java is installed:

java -version
  1. If not installed, download it from oracle.com.

  2. Set the Path environment variable:

    • Go to Control Panel > System > Advanced System Settings > Environment Variables

    • Edit Path under System variables

    • Add C:\Program Files\Java\jdk-11.0.1\bin (or your installation path)

  3. Verify installation:

java -version

Your First Java Program

Create Main.java with the following:

public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Run the program:

javac Main.java
java Main

Output:

Hello World

Java Basics

  • Class: Every Java program must have a class. The filename must match the class name.

  • main() method: Entry point of every Java program.

public static void main(String[] args) { }
  • Printing to screen:

System.out.println("Hello World");
  • Syntax notes:

    • Java is case-sensitive

    • Each statement ends with a semicolon ;

    • Curly braces {} mark code blocks



Java Output / Print

Print Text

Use System.out.println() to print text or values, adding a new line after each call:

System.out.println("Hello World!");
System.out.println("I am learning Java.");

Use System.out.print() to print on the same line:

System.out.print("Hello ");
System.out.print("World!");

Note: Text must be in double quotes:

System.out.println("This works!");
// System.out.println(This will error); → ERROR

Java Comments

  • Single-line: // This is a comment

  • Multi-line:

/*
This is a
multi-line comment
*/


Java Variables

Variables store data values. Syntax:

type variableName = value;

Examples:

String name = "John";
int myNum = 15;
float myFloat = 5.99f;
char myLetter = 'D';
boolean isJavaFun = true;
  • Change value:

int myNum = 15;
myNum = 20; // now 20
  • Final (constant) variable:

final int myNum = 15; // cannot be changed
  • Multiple variables in one line:

int x = 5, y = 6, z = 50;
int a, b, c;
a = b = c = 50;
  • Combine text and variables:

String name = "John";
System.out.println("Hello " + name);
  • Combine numbers:

int x = 5, y = 6;
System.out.println(x + y); // 11

Java Data Types

Primitive types (predefined):

Type Size Description
byte 1 B Whole numbers -128 to 127
short 2 B Whole numbers -32,768 to 32,767
int 4 B Whole numbers -2B to 2B
long 8 B Whole numbers, use L suffix
float 4 B Decimal numbers, use f suffix
double 8 B Decimal numbers, use d suffix
boolean 1 bit true / false
char 2 B Single character, use ' '

Examples:

int myNum = 100000;
long bigNum = 15000000000L;
float pi = 3.14f;
double e = 2.71828d;
boolean isJavaFun = true;
char grade = 'A';

Non-primitive types (reference types, objects):

  • String, Arrays, Classes

String text = "Hello World";

Java Type Casting

  • Widening (automatic): smaller → larger type

int myInt = 9;
double myDouble = myInt; // 9 → 9.0
  • Narrowing (manual): larger → smaller type

double myDouble = 9.78;
int myInt = (int) myDouble; // 9.78 → 9

Java Strings

  • Store text:

String greeting = "Hello";
  • Length:

System.out.println(greeting.length());
  • Upper / lower case:

System.out.println(greeting.toUpperCase()); // HELLO
System.out.println(greeting.toLowerCase()); // hello
  • Find index of a substring:

String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // 7


Java Cheat Sheet

1. Math

Math.max(5, 10); // 10
Math.min(5, 10); // 5
Math.sqrt(64); // 8
Math.abs(-4.7); // 4.7
Math.random(); // 0.0–1.0
int randNum = (int)(Math.random()*101); // 0–100

2. Booleans

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(10 > 9); // true
System.out.println(10 == 15); // false
int age = 25;
System.out.println(age >= 18); // true

3. Conditionals

int x = 20;
if(x > 18) { System.out.println("x>y"); }
else if(x==18){System.out.println("x==18");}
else{System.out.println("x<18");}

int day=4;
switch(day){
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
default: System.out.println("Other");
}


4. Loops

// while
int i=0; while(i<5){ System.out.println(i); i++; }
// do/while
i=0; do{ System.out.println(i); i++; } while(i<5);
// for
for(int j=0;j<5;j++){ System.out.println(j); }
// break & continue
for(int k=0;k<10;k++){ if(k==4) break; System.out.println(k);}
for(int k=0;k<10;k++){ if(k==4){continue;} System.out.println(k);}

5. Arrays

String[] cars = {"Volvo","BMW","Ford"};
System.out.println(cars[0]); // Volvo
cars[0]="Opel"; // modify
System.out.println(cars.length); // 3

6. Methods

static void myMethod(){ System.out.println("Hi"); }
myMethod(); // call

// Parameters
static void greet(String name){ System.out.println(name+"!"); }
greet("Liam");

// Return
static int add(int a,int b){ return a+b; }
int z = add(5,3); // 8

// If inside method
static void checkAge(int age){
if(age<18) System.out.println("Denied");
else System.out.println("Granted");
}

// Overloading
static int plus(int a,int b){return a+b;}
static double plus(double a,double b){return a+b;}


7. Scope

int x=100; // method scope
{ int y=5; } // block scope

8. Recursion

static int sum(int k){ if(k>0) return k+sum(k-1); else return 0; }
System.out.println(sum(10)); // 55

static int sum(int start,int end){
if(end>start) return end+sum(start,end-1);
else return end;
}


9. Classes & Objects

class Main{ int x=5; }
Main obj = new Main();
System.out.println(obj.x); // 5

// Multiple objects
Main obj1=new Main(), obj2=new Main();
obj2.x=25;


10. Attributes

class Main{
String fname="John"; int age=24;
}
Main obj = new Main();
obj.fname="Doe"; // modify

11. Methods in Classes

class Car{
void fullThrottle(){ System.out.println("Fast!"); }
void speed(int max){ System.out.println("Max: "+max); }
}
Car myCar = new Car();
myCar.fullThrottle(); myCar.speed(200);

12. Constructors

class Main{ int x; Main(){ x=5; } }
Main obj = new Main(); System.out.println(obj.x); // 5

13. Modifiers

final int x=10; // can't change
static void st(){ System.out.println("Static"); } // no object
public void pub(){ System.out.println("Public"); } // object needed

14. Abstract Classes

abstract class Main{ abstract void study(); }
class Student extends Main{
void study(){ System.out.println("Studying"); }
}
Student s = new Student(); s.study();

15. Encapsulation

class Person{
private String name;
public String getName(){return name;}
public void setName(String n){this.name=n;}
}
Person p = new Person();
p.setName("John");
System.out.println(p.getName());

16. Inner Classes

class Outer{
int x=10;
class Inner{ int y=5; }
}
Outer o=new Outer();
Outer.Inner i=o.new Inner();
System.out.println(o.x+i.y); // 15


Java Abstraction: Abstract Classes & Interfaces

1. Abstract Classes

  • Used to hide details and provide a template.

  • Cannot create objects directly: must be inherited.

  • Can have abstract methods (no body) and regular methods (with body).

abstract class Animal {
public abstract void animalSound(); // abstract
public void sleep() { System.out.println("Zzz"); } // regular
}

class Pig extends Animal {
public void animalSound() { System.out.println("The pig says: wee wee"); }
}

class Main {
public static void main(String[] args){
Pig myPig = new Pig();
myPig.animalSound(); // The pig says: wee wee
myPig.sleep(); // Zzz
}
}

Key points:

  • Abstract class = blueprint, cannot instantiate.

  • Subclass must implement all abstract methods.


2. Interfaces

  • Fully abstract: all methods are abstract by default.

  • Attributes are public, static, final by default.

  • Use implements to provide method bodies.

  • Supports multiple inheritance (a class can implement multiple interfaces).

interface Animal {
void animalSound();
void sleep();
}

class Pig implements Animal {
public void animalSound(){ System.out.println("The pig says: wee wee"); }
public void sleep(){ System.out.println("Zzz"); }
}

class Main {
public static void main(String[] args){
Pig myPig = new Pig();
myPig.animalSound(); // The pig says: wee wee
myPig.sleep(); // Zzz
}
}


3. Multiple Interfaces

interface FirstInterface { void myMethod(); }
interface SecondInterface { void myOtherMethod(); }

class Demo implements FirstInterface, SecondInterface {
public void myMethod(){ System.out.println("Some text.."); }
public void myOtherMethod(){ System.out.println("Some other text..."); }
}

class Main {
public static void main(String[] args){
Demo obj = new Demo();
obj.myMethod(); // Some text..
obj.myOtherMethod(); // Some other text...
}
}

Notes:

  • Interfaces cannot have constructors.

  • Must override all methods when implementing.

  • Allows security and multiple inheritance.