Today marks the first step in my Java Backend Development journey, and what an exciting start it has been! From setting up the environment to writing my first structured Java code, I’ve explored the foundational concepts that form the backbone of Java development. Let’s dive deep into the details of Day 1.
🛠️ JVM, JRE, and JDK
Before writing a single line of code, it’s crucial to understand how Java executes programs:
JVM (Java Virtual Machine): The JVM is the heart of Java’s “write once, run anywhere” philosophy. When we compile a Java program, it’s translated into bytecode (
.class
files). The JVM interprets this bytecode into machine code specific to the host operating system, making Java platform-independent.JRE (Java Runtime Environment): The JRE includes the JVM plus the standard libraries (like
java.lang
,java.util
, etc.). It provides everything required to run Java programs but doesn’t include tools to develop them.JDK (Java Development Kit): The JDK is the full development package. It contains the JRE, compiler (
javac
), debugger, and other development tools. Without the JDK, you cannot write and compile Java code.
👉 Formula to remember: JDK = JRE + Development Tools; JRE = JVM + Libraries
✍️ Basic Syntax & Building Blocks
The basic structure of every Java program revolves around classes and methods:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Class: Java is an object-oriented language, so every piece of code resides inside a class.
- main() method: The entry point for any standalone Java application.
Variables & Data Types
Java is statically typed, meaning you must declare the type of variable before using it.
int age = 25; // integer
double price = 99.99; // decimal
char grade = 'A'; // single character
String name = "Alice"; // sequence of characters
boolean isActive = true; // logical values
- Primitive data types: byte, short, int, long, float, double, char, boolean.
- Non-primitive data types: String, arrays, objects, etc.
Operators
Operators allow us to manipulate variables:
int sum = 10 + 5; // Arithmetic operator
int remainder = 10 % 3; // Modulus
boolean result = (5 > 2) && (3 < 4); // Logical operators
🔄 Control Structures
Programs need decision-making and loops:
If-Else
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
Switch
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
default:
System.out.println("Keep working hard");
}
Loops
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
int count = 1;
while (count <= 3) {
System.out.println("While loop count: " + count);
count++;
}
🧠 Memory Model Basics
Java abstracts memory management but understanding it helps avoid pitfalls:
- Stack Memory: Stores method calls, primitive variables, and references to objects. It’s fast and managed per thread.
- Heap Memory: Stores objects and class instances. All objects live here.
- Garbage Collection: JVM automatically reclaims memory from objects no longer referenced. Developers don’t need to manually free memory, unlike in C/C++.
⚙️ Methods & Functions
Methods improve reusability and modularity:
public class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) { // Method overloading
return a + b;
}
}
- Return type defines what value a method returns.
- Parameters allow passing data into methods.
- Overloading provides flexibility with different parameter lists.
🏛️ Object-Oriented Programming (OOPs) Basics
Java’s strength lies in OOP principles:
Encapsulation
Binding variables and methods inside a class:
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Inheritance
Reusing code by extending classes:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Polymorphism
The same method behaves differently based on context (method overriding, overloading).
Abstraction
Abstract classes and interfaces hide complexity and expose essential features.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with a key");
}
}
⚡ Exception Handling
Exceptions prevent runtime errors from crashing applications:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This block always executes.");
}
- try: Code that may throw an exception.
- catch: Handles the exception.
- finally: Executes regardless of exception.
- throw/throws: Used to declare and propagate exceptions.
🎯 Day 1 Takeaway
Day 1 was all about establishing a solid Java foundation. I now understand:
- How Java executes programs (JVM, JRE, JDK).
- Core syntax, variables, operators, and control structures.
- The memory model and how Java handles garbage collection.
- Writing reusable methods and applying OOP principles.
- Handling exceptions gracefully.
I feel ready to build upon these fundamentals and move foward
💡 Stay tuned for Day 2
Top comments (0)