What is Java
Java is a high-level, object-oriented, platform-independent programming language used to build applications.
Types of Computer Languages
Type Example Description
Machine Language 01010101 1010… Pure 0 and 1 → computer understands only this
Assembly Language MOV A, B Short symbolic codes
High-Level Language C, Java, Python Human-friendly, easy to write
Types of High-Level Languages
1. Procedural Languages
✔ Used step-by-step
✔ Functions and procedures Examples:
C, BASIC, FORTRAN
2. Object-Oriented Languages
✔ Uses classes and objects
✔ Good for large projects
Examples: C++, Java, Python
3. Scripting Languages
✔ Run line-by-line (interpreted)
✔ Used for automation & websites
Examples: JavaScript, PHP, Python
4. Functional Languages
✔ Based on mathematical functions
✔ No changing data
Examples: Haskell, Lisp
5. Logic Languages
✔ Uses rules, facts, and reasoning
Examples: Prolog
6. Query Languages
✔ Used to manage and search data
Examples: SQL
Why C++
Problem in C Solution in C++
No OOP Classes, objects
No data security private, public
Hard to manage large code OOP structure
No exception handling try-catch
No overloading function & operator overloading
No templates Generic programming
In one line, C++ = C + OOP + Safety + Better structure
Why do we need Java even after C++
Java was created to fix the remaining problems of C and C++ and to make programming safer, portable,
and more modern.
1. C++ is NOT platform–independent
C++ programs run differently on Windows, Linux, Mac.
• You must recompile on each OS.
• Binary files change based on system architecture.
➡ Java Solution: “Write Once, Run Anywhere”
Java uses JVM (Java Virtual Machine).
Same Java code runs everywhere without recompiling.
2. C++ has manual memory management
You must use:
new
delete
If you forget delete, memory leak happens.
➡ Java Solution: Automatic Garbage Collection
Java automatically removes unused objects → safer + fewer bugs.
3. C++ uses pointers (unsafe for beginners)
Pointers can crash the program easily:
int *p = NULL;
*p = 10; // crash
➡ Java Solution:
Java completely removed pointers from the syntax.
Internally uses references (safe).
4. C++ has complicated features
C++ is powerful but complex:
• Multiple inheritance
• Operator overloading
• Memory corruption issues
• Header files & linker errors
• Templates (hard for beginners)
➡ Java Solution:
Java is simpler and cleaner.
No header files, no pointer arithmetic, no multiple inheritance of classes.
5. C++ does not have strong security
C++ allows memory access using pointers → risky.
➡ Java Solution:
Java has:
• Bytecode verification
• Sandboxing
• No direct memory access
• Exception handling
Java is used for banking, enterprise apps, secure systems.
6. C++ programs are not fully portable
Different compilers, architectures → different results.
➡ Java Solution:
Java bytecode runs on JVM → fully portable.
7. C++ is good for system-level programming, NOT for enterprise apps
C++ is used for:
• OS
• Drivers
• Game engines
• Embedded systems
Java is used for:
• Web apps
• Android apps
• Enterprise software
• Cloud applications
Java is built for large, distributed, network-based applications.
Why Java is Popular
Java has these important features:
✔ Simple
Easy to learn, similar to C/C++ but simpler.
✔ Object-Oriented
Everything is based on classes and objects.
✔ Platform Independent
“Write Once, Run Anywhere” (WORA).
Java code runs on any machine that has JVM.
✔ Portable
Java programs can move across OS (Windows, Linux, Mac).
✔ Secure
Strong security features (bytecode verification, no pointer manipulation).
✔ Robust
Strong memory management + error handling.
✔ Architecture Neutral
Same output on all systems.
✔ High Performance
JIT compiler makes it faster.
✔ Distributed
Supports networking and distributed apps.
✔ Multithreaded
Multiple tasks can run at the same time.
✔ Dynamic
Java can load classes dynamically.
Java was created by James Gosling and his team at Sun Microsystems. In 1991, a small team called
Green Team started a project to create a language for:
• Smart devices
• TVs
• Microwave ovens
• Remote controls
The language was first named Oak.
Why Oak
Because there was an oak tree outside James Gosling’s office.
Oak could not be trademarked (name already existed).
So in 1995, they renamed it Java.
Why the name Java?
Because the developers loved Java coffee (from Indonesia).
That’s why Java’s logo is a coffee cup
1. JVM (Java Virtual Machine)
JVM = Engine that runs Java program
• It executes the bytecode (.class file)
• It gives Java → Platform Independence
• JVM exists for Windows, Linux, Mac etc.
➡ You write Java once → JVM runs it anywhere
JVM does 3 important jobs:
1. Loads the code
2. Verifies the code
3. Executes the code
➡ Think JVM as the brain of Java.
2. JRE (Java Runtime Environment)
JRE = JVM + Libraries needed to run Java programs
JRE includes:
• JVM
• Java libraries
• Java packages
• Runtime tools
➡ You can RUN Java programs with JRE
➡ But you cannot write Java programs with JRE.
3. JDK (Java Development Kit)
JDK = JRE + Tools for developers
JDK is for people who want to write Java programs.
JDK includes:
• JRE
• JVM
• Compilers (javac)
• Debuggers
• Development tools
➡ You can WRITE + COMPILE + RUN Java programs using JDK
Very Simple Comparison
Component What it Does Used For
JVM Runs the Java bytecode Execution
JRE Contains JVM + libraries Running Java programs
JDK Contains JRE + tools Writing & running Java programs
Simple Example
You write a Java file : [Link]
Step 1: JDK
Compiler (javac) converts it into:
[Link] (bytecode)
Step 2: JVM
JVM reads the .class file and runs it.
This whole process is possible because of:
• JDK → compiles
• JRE → provides libraries
• JVM → executes code
Simple Java Program
class Hello {
public static void main(String[] args) {
[Link]("Hello Java!");
How it works?
• class Hello → class name
• main() → starting point
• [Link]() → print text
WORD-BY-WORD EXPLANATION
class
• In Java, everything must be written inside a class.
• A class is like a container or blueprint for your code.
Hello
• This is the name of the class.
• The file name must match the class name.
Example: [Link]
{ } (curly braces)
• These braces create a block.
• Everything inside { } belongs to that class or method.
Now Inside the Class
public static void main(String[] args)
This is the main method.
Every Java program starts running from here.
Let’s explain each word:
public
• This is an access modifier.
• It means anyone can access this method.
• The JVM needs to access it, so it must be public.
static
• Means you do NOT need to create an object to run this method.
• JVM can run it directly.
void
• The method returns nothing.
• The main method’s job is only to run the program.
main
• This is the starting point of the program.
• JVM looks for main() to start execution.
➡ If you change the name, the program will NOT run.
(String[] args)
• This is a String array named args.
• It stores command line arguments (inputs given during program run).
Example:
java Hello Prodosh
Then:
• args[0] = "Prodosh"
Inside main() → The Print Statement
[Link]("Hello Java!");
Break down each part:
System
• A built-in Java class.
• Provides access to system-related features.
out
• A static object of System class.
• Used to send output to the screen.
➡ [Link] = standard output stream
println()
• A method that prints text.
• It also moves the cursor to the next line.
➡ print() → prints without new line
➡ println() → prints with new line
"Hello Java!"
• A String literal.
• This text will appear on the screen.
; (semicolon)
• Java uses semicolon to end a statement.
Closing Braces
• First } closes the main() method.
• Second } closes the Hello class.
Take input from user & print it
import [Link];
class UserInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
}
}
Explanation
import [Link];
• Scanner is a built-in Java class used to read user input.
• We must import it before using.
class UserInputExample
• Creating a class named UserInputExample.
• File name should be: [Link]
Scanner sc = new Scanner([Link]);
• Creating a Scanner object named sc.
• [Link] means input from keyboard.
[Link]("Enter your name: ");
• Prints text without moving to the next line.
• Asks user to enter name.
String name = [Link]();
• Reads one line of text from the user.
• Stores it in variable name.
[Link]("Hello, " + name + "!");
• Prints greeting using the name entered by user.
What is a Class in Java
A class is a blueprint, template, or design from which objects are created.
✔ A class defines:
• Properties (variables / data)
• Behaviors (methods / functions)
✔ An object is created from a class.
Real-Life Example
Think of a class as a house blueprint and an object as the actual house built using the blueprint.
• Blueprint = class
• House = object
• Many houses can be created from one blueprint → Many objects from one class
Basic Structure of a Class
class ClassName {
// properties (variables)
int age;
String name;
// behaviors (methods)
void display() {
[Link]("Hello!");
}
}
What is an Object in Java
Object is a real-world thing that has Properties + Behaviors.
Object in Java Definition
Object is an instance of a class.
• Class = blueprint / design
• Object = real thing created from the blueprint
Example:
• Class: Student
• Objects: Rahim, Suman, Prodosh
An object has three characteristics:
State: represents the data (value) of an object.
Behaviour: represents the behaviour (functionality) of an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. However, it is used internally by the JVM to identify each object
uniquely.
Identity
Identity = Unique ID of each object.
Even if two objects have the same data, they are not the same object.
Example:
Student s1 = new Student();
Student s2 = new Student();
Both are separate objects in memory.
Java gives every object a unique internal identity
Example:
[Link](s1);
[Link](s2);
Outputs something like:
Student@6d06d69c
Student@7852e922
These codes are unique identities of each object.
There are many ways to create an object in java.
By new keyword
By newInstance() method
By clone() method
By deserialization
By factory method etc.
An object is created using the new keyword.
Basic Syntax:
ClassName objectName = new ClassName();
Let’s break it:
Part Meaning
ClassName The class from which the object is created
objectName The variable that will refer to the object
new Allocates memory for the object
ClassName() Calls the constructor of the class
Simple Object Creation
class Student {
// data (properties)
int age = 20;
String name = "Prodosh";
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // creating object
[Link]([Link]);
[Link]([Link]);
}
}
Explanation
• Student s1 → declaring a reference variable
• new Student() → creating an object in memory
• [Link] → accessing object's data
• [Link] → accessing object's data
Object Creation With Methods
class Calculator {
int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator(); // creating object
int result = [Link](5, 10);
[Link](result);
}
}