Java is a class-based, object-oriented programming language designed to have few implementation dependencies. Created by James Gosling at Sun Microsystems (now Oracle) and released in 1995, Java’s “write once, run anywhere” philosophy has made it one of the most widely used programming languages.
// Primitives (8 types)
int i = 42;
long l = 42L;
float f = 3.14f;
double d = 3.14;
boolean b = true;
char c = 'A';
byte bt = 127;
short s = 32767;
// Wrapper classes (autoboxing)
Integer boxed = 42; // int → Integer
int unboxed = boxed; // Integer → int
// Strings (immutable)
String s1 = "hello";
String s2 = new String("hello");
StringBuilder sb = new StringBuilder(); // Mutable
// Generic class
public class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
// Bounded type parameters
public <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) >= 0 ? a : b;
}
// Wildcards
List<? extends Number> nums; // Upper bounded (read)
List<? super Integer> ints; // Lower bounded (write)
flowchart LR
subgraph "CPU 1"
R1[Cache<br/>x=0]
end
subgraph "CPU 2"
R2[Cache<br/>x=0]
end
MEM[Main Memory<br/>x=0] --> R1
MEM --> R2
R1 -.->|visibility issue| MEM
R2 -.->|visibility issue| MEM
volatile: Guarantees visibility and ordering
happens-before: Defined by JMM for synchronization
public sealed class Shape permits Circle, Rectangle, Triangle {}
public final class Circle extends Shape { /* ... */ }
public final class Rectangle extends Shape { /* ... */ }
public non-sealed class Triangle extends Shape { /* ... */ }