Introduction to Programming - Types of Languages, Memory Management

Abheeshta PAbheeshta P
5 min read

🧠 1. Procedural Programming

πŸ” Definition:

A programming paradigm based on procedures (or routines/functions). It’s like following a step-by-step recipe.

πŸ”‘ Key Features:

  • Top-down execution

  • Global state

  • Functions act on data

  • Code is structured into procedures, not objects

βœ… Examples:

  • C, Pascal, Bash, Early versions of Python, JavaScript

πŸ§ͺ Code Example (JS-style procedural):

let total = 0;

function addToCart(price) {
  total += price;
}

addToCart(100);
addToCart(50);

console.log(total); // 150

⚠️ Downsides:

  • Global state is risky

  • Harder to scale

  • Code reuse is limited

🧠 2. Object-Oriented Programming (OOP)

πŸ” Definition:

A paradigm where everything revolves around objects β€” bundles of data (properties) and behaviors (methods).

πŸ”‘ Key Features:

  • Classes and Objects

  • Encapsulation (hide internal state)

  • Inheritance

  • Polymorphism

  • Mutability

βœ… Examples:

  • Java, C++, Python (OOP style), TypeScript

πŸ§ͺ Code Example:

class Cart {
  constructor() {
    this.total = 0;
  }

  add(price) {
    this.total += price;
  }

  getTotal() {
    return this.total;
  }
}

const cart = new Cart();
cart.add(100);
cart.add(50);

console.log(cart.getTotal()); // 150

βœ… Benefits:

  • Modular code

  • Easier to manage complex systems

  • Reusability via inheritance

⚠️ Downsides:

  • Can become bloated

  • Inheritance abuse

  • Mutability can lead to bugs

🧠 3. Functional Programming (FP)

πŸ” Definition:

A paradigm where functions are first-class citizens. Everything is treated as pure functions and immutable data.

πŸ”‘ Key Features:

  • Pure functions (no side effects)

  • Immutability

  • Higher-order functions

  • Declarative style

  • Composability

βœ… Examples:

  • Haskell, Elixir, Clojure, JS (FP style)

πŸ§ͺ Code Example (Same cart):

const addToCart = (cartTotal, price) => cartTotal + price;

const total1 = addToCart(0, 100);
const total2 = addToCart(total1, 50);

console.log(total2); // 150

βœ… Benefits:

  • Highly testable

  • Easy to debug

  • Parallelism-friendly (no shared state)

  • Reusable, composable functions

⚠️ Downsides:

  • Verbose or less intuitive at first

  • Can be harder to write stateful apps

πŸ” Comparison Table

FeatureProceduralOOPFunctional
Basic unitProcedure / FunctionObject / ClassFunction
StateGlobal or local varsEncapsulated in objectsAvoided, immutable
ReuseFunction reuseInheritance, compositionFunction composition
StyleStep-by-step (how)Object interactionsDeclarative (what to do)
Side EffectsCommonCommonAvoided
Data MutabilityOften mutableOften mutableImmutable
Real ExampleCJava, C++, TS classesHaskell, Elixir, FP-style JS
TestingModerateSometimes harder (mocking)Easy (pure functions)

πŸ”š Summary

  • Procedural β†’ Good for simple tasks, scripting, linear logic

  • OOP β†’ Best for large apps, user-based systems (e.g., ecommerce, games)

  • Functional β†’ Great for data processing, predictable logic, concurrency (e.g., search engines, real-time data)


πŸ”· Static Typed Languages

πŸ“Œ Definition:

In statically typed languages, the type of a variable is known at compile-time. You have to define or infer the type before running the program.

🧠 Key Features:

  • Type checking happens before code runs

  • Errors are caught early

  • Requires you to declare or infer types

βœ… Examples:

TypeScript, Java, C, C++, Rust, Go, Kotlin

πŸ”Ž Example:

let age: number = 25;
age = "twenty"; // ❌ Type error during compilation

βœ… Pros:

  • Fewer bugs at runtime

  • Better auto-completion (intellisense)

  • Safer refactoring

  • IDE support is πŸ”₯

⚠️ Cons:

  • More boilerplate

  • Slower for quick scripts

πŸ”Ά Dynamic Typed Languages

πŸ“Œ Definition:

In dynamically typed languages, types are determined at runtime. You don’t declare types; they’re inferred when the code runs.

🧠 Key Features:

  • Type checking happens during execution

  • More flexible

  • Variables can change type

βœ… Examples:

JavaScript, Python, Ruby, PHP, Lua

πŸ”Ž Example:

let age = 25;
age = "twenty"; // βœ… No error β€” works at runtime (might cause bugs)

βœ… Pros:

  • Less code to write (faster to prototype)

  • Great for small scripts or quick tasks

  • Very flexible

⚠️ Cons:

  • Bugs can occur at runtime

  • Harder to maintain large codebases

  • Type-related bugs go unnoticed till crash

πŸ†š Summary Table

FeatureStatic TypingDynamic Typing
Type CheckingCompile-timeRuntime
Type DeclarationRequired or inferredNot required
Error DetectionEarly (before running)Late (during execution)
Speed of DevSlower setup, safer long-termFast prototyping, less safety
FlexibilityLess flexibleMore flexible
Ideal ForLarge apps, critical systemsSmall apps, quick scripts

Memory management

  • stack and heap memory

    β˜• Stack vs Heap

    πŸ”· Stack (Thread Stack)

    • Stores method calls, local variables, and reference pointers

    • Memory is automatically managed

    • Each thread has its own stack

    • Faster but smaller

βœ… Example:

    void greet() {
      int x = 10;         // x stored in stack
      String name = "Abhee"; // reference in stack, actual string in heap
    }

When greet() finishes β†’ local vars are removed from stack

πŸ”Ά Heap

  • Stores objects, arrays, class instances

  • Shared across all threads

  • Cleaned up by Java’s Garbage Collector (GC)

  • Larger but slower access

πŸ” How Java Manages Memory

AreaUsed ForManaged By
StackMethod calls, primitives, refsAutomatically (per thread)
HeapObjects, instance vars, arraysGarbage Collector (GC)

♻️ Garbage Collector (In Java)

  • Frees heap memory for unreachable objects

  • Automatically runs in background

  • No manual free() like in C/C++

πŸ”₯ TL;DR

πŸ—‚ Stack = Per-thread, fast, for temporary stuff (primitives, refs)
🧺 Heap = Shared, for objects, slow but flexible, GC-managed

Thanks to kunalkushwaha for great video!

0
Subscribe to my newsletter

Read articles from Abheeshta P directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abheeshta P
Abheeshta P

I am a Full-stack dev turning ideas into sleek, functional experiences πŸš€. I am passionate about AI, intuitive UI/UX, and crafting user-friendly platforms . I am always curious – from building websites to diving into machine learning and under the hood workings ✨. Next.js, Node.js, MongoDB, and Tailwind are my daily tools. I am here to share dev experiments, lessons learned, and the occasional late-night code breakthroughs. Always evolving, always building.