Introductory for Java
What is Java?
• Java is a high-level, object-oriented programming language.
• Created by James Gosling and released in 1995 by Sun Microsystems (now
owned by Oracle).
• Known for its portability ("Write Once, Run Anywhere"), security, and
performance.
• Used for Web Applications, Mobile Apps (Android), Game Development,
Backend Systems, and more.
Basic Features of Java
• Strongly typed (must declare variable types).
• Compiled and Interpreted (Java code is compiled into bytecode, then executed
by the JVM).
• Platform-independent (Runs on any OS with Java Virtual Machine - JVM).
• Object-Oriented Programming (OOP) (focuses on classes and objects).
• Automatic memory management (Garbage Collection).
Java Syntax Basics
Writing a Basic Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
• public class HelloWorld → Defines a class named HelloWorld.
• public static void main(String[] args) → The main method, the entry point of
Java programs.
• System.out.println("Hello, World!"); → Prints text to the console.
Comments in Java
// This is a single-line comment
/*
This is a
multi-line comment
*/
Variables & Data Types
int age = 25; // Integer
double price = 19.99; // Floating-point
char grade = 'A'; // Character
boolean isJavaFun = true; // Boolean
String name = "Alice"; // String
Type Example Size
int 25 4 bytes
double 19.99 8 bytes
char 'A' 2 bytes
boolean true/false 1 bit
String "Hello" Varies
Input and Output
import java.util.Scanner; // Import Scanner
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create Scanner object
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Read input
System.out.println("Hello, " + name);
scanner.close(); // Close scanner
Operators in Java
Operator Example Meaning
+ a+b Addition
- a-b Subtraction
* a*b Multiplication
/ a/b Division
% a%b Modulus (remainder)
== a == b Equal to
!= a != b Not equal to
Conditional Statements
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
Loops in Java
For Loop
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
While Loop
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
Functions (Methods)
public class MethodsExample {
// Method definition
static void greet() {
System.out.println("Hello!");
public static void main(String[] args) {
greet(); // Method call
}
Method with Parameters & Return Value
public class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int sum = add(5, 3);
System.out.println("Sum is: " + sum);
Object-Oriented Programming (OOP)
Defining a Class & Object
class Car {
String brand = "Toyota";
void honk() {
System.out.println("Beep! Beep!");
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Create object
System.out.println(myCar.brand);
myCar.honk();
Example Program: Simple Calculator
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
System.out.println("Sum: " + (a + b));
scanner.close();