Let’s discover Java
Introduction
Java is a high-level, object-oriented programming language first released by Sun Microsystems in 1995. Its “write once, run anywhere” philosophy comes from compiling code into bytecode that runs on any device equipped with a Java Virtual Machine (JVM).
Key features
Strongly typed and object-oriented (classes, inheritance, interfaces)
Automatic memory management with garbage collection
Rich standard library (collections, I/O, networking, concurrency)
Built-in support for multithreading and parallelism
Common use cases
Enterprise applications and microservices (Spring Boot)
Android mobile development
Big data processing (Hadoop, Spark)
Embedded systems and IoT with Java SE Embedded
With its stability, portability, and extensive ecosystem, Java remains one of the most widely adopted languages in the world.
Primitive Types
Primitive types represent simple, non-object databyte
: 8 bits (-128 à 127)short
: 16 bits (-32,768 à 32,767)int
: 32 bits (-2,147,483,648 à 2,147,483,647)long
: 64 bits (-9,223,372,036,854,775,808 à 9,223,372,036,854,775,807)float
: 32 bits (1.4E-45 à 3.4028235E38)double
: 64 bits (4.9E-324 à 1.7976931348623157E308)char
: 16 bits (0 à 65,535, character Unicode)boolean
: true
or false
Example
int number = 5;
double price = 10.5;
char letter = ‘A’;
boolean isActive = true
Control Structures
Loops
Loops let you repeat a block of code multiple times. Java offers three primary loop constructs:
- for: when you know the exact number of iterations.
- while: repeats as long as its condition remains true.
- do-while: guarantees the loop body runs at least once before checking the condition.
Example
// for loop: prints 0 to 4
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// while loop: prints 0 to 4
int j = 0;
while (j < 5) {
System.out.println(j);
j++;
}
// do-while loop: prints 0 to 4
int k = 0;
do {
System.out.println(k);
k++;
} while (k < 5);
Conditionals
Conditionals control which code runs based on boolean expressions. Java supports:
- if / else if / else: branching logic for multiple conditions.
- switch: select one of many code paths based on an expression’s value.
Example
// if / else example
int x = 10; if (x > 5) {
System.out.println(« x is greater than 5 »);
} else { System.out.println(« x is 5 or less »);
}
// switch example
int day = 3;
switch (day) {
case 1: System.out.println(« Monday »);
break;
case 2: System.out.println(« Tuesday »);
break;
case 3: System.out.println(« Wednesday »);
break;
default: System.out.println(« Another day »);
}
Object-Oriented Programming (OOP)
Object-Oriented Programming is a paradigm centered on objects—instances of classes—that combine state (fields) and behavior (methods). Java’s OOP model rests on four core principles:
Encapsulation Hides internal details by bundling data and methods in a class and restricting access via access modifiers (private
, protected
, public
).
Inheritance Enables a class (subclass) to inherit fields and methods from another class (superclass), fostering code reuse.
Polymorphism Allows objects of different classes related by inheritance to be treated through a common interface or superclass reference.
Abstraction Simplifies complexity by exposing only essential features through abstract classes or interfaces.