Declaring variables

// Java
int number = 10;

// JavaScript
let number = 10;

Functions / Methods

// Java
public int add(int a, int b) {
    return a + b;
}

// JavaScript
function add(a, b) {
    return a + b;
}

Printing output

// Java
System.out.println("Hello World");

// JavaScript
console.log("Hello World");

Classes and constructors

// Java
public class Person {
    String name;
    public Person() {
        this("unknown");
    }
    public Person(String name) {
        this.name = name;
    }
}

// JavaScript
class Person {
    constructor(name) {
        this.name = name;
    }
}

Extending a class

// Java
public class Animal {
    public void speak() {
        System.out.println("Animal sound");
    }
}

public class Dog extends Animal {
    @Override
    public void speak() {
        System.out.println("Bark");
    }
}

// JavaScript
class Animal {
    speak() {
        console.log("Animal sound");
    }
}

class Dog extends Animal {
    speak() {
        console.log("Bark");
    }
}

Abstract classes

// Java
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

// JavaScript (no abstract keyword, emulate using error throwing)
class Shape {
    draw() {
        throw new Error("Method 'draw()' must be implemented");
    }
}

class Circle extends Shape {
    draw() {
        console.log("Drawing Circle");
    }
}

Interfaces

// Java
interface Drawable {
    void draw();
}

class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

// JavaScript (using TypeScript for proper interfaces)
interface Drawable {
    draw(): void;
}

class Circle implements Drawable {
    draw() {
        console.log("Drawing Circle");
    }
}

// Pure JavaScript alternative: define a "protocol" via convention
class Circle {
    draw() {
        console.log("Drawing Circle");
    }
}

Conditional statements

// Java
if (x > 0) {
    System.out.println("Positive");
} else {
    System.out.println("Negative");
}

// JavaScript
if (x > 0) {
    console.log("Positive");
} else {
    console.log("Negative");
}

Primitives and Wrapper Classes

Wrapper classes are objects wrapper of the primitives data

Primitive TypeWrapper ClassPrimitive Size (bytes)Approx. Wrapper Object Size (bytes)
byteByte1~16
shortShort2~16
intInteger4~16
longLong8~24
floatFloat4~16
doubleDouble8~24
charCharacter2~16
booleanBoolean1 (conceptually)~16

Comparing Wrapper Classes

  • objectVar & objectVar :
    • objectVar == , != objectVar will NOT behave as expected
    • objectVar < , > , ≥, ≤ objectVar behave like normal
  • objectVar & primitiveVar/constants :
    • objectVar == , != , < , > , ≥, ≤ objectVar behave like normal

Use Cases of Wrapper Classes

  • Can be null
  • Can be used where objects are required like in collections, reflection, serialization, streams
  • Contain useful utility methods
  • Java has autoboxing/unboxing which allow auto converting of primitive and wrapper between each other

Enum

enum is a special class that defines a fixed set of constants

// Java
public enum Direction {
    NORTH, SOUTH, EAST, WEST
}
---------------------------------
// use as Direction.NORTH, etc.

Lambda Expressions

Lambda expressions are a shorthand way to simplifying inline methods

  • Syntax: (parameters) -> expression or (parameters) -> statements
  • Used with Functional Interface
  • // Setting up new interface with only one method
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Clicked!");
        }
    });
    ----------------------------------------------------------------
    // Using lambda expression to simplify the above code
    button.setOnClickListener(v -> System.out.println("Clicked!"));
  • For iterating collections
  • List<String> names = List.of("Naruto", "Luffy", "Ichigo");
    ----------------------------------------------------------------
    // Old way
    for (String name : names) {
        System.out.println(name);
    }
    ----------------------------------------------------------------
    // Lambda way
    names.forEach(name -> System.out.println(name));
  • Used with Streams (filter, map, reduce) to act on collections
  • List<Integer> nums = List.of(1, 2, 3, 4, 5);
    
    List<Integer> evens = nums.stream()
        .filter(n -> n % 2 == 0)   // keep even numbers
        .map(n -> n * 10)         // multiply by 10
        .toList();

Memory Regions

  • Code: has programs instruction
  • Static Memory: has static fields
  • Stack (automatic memory): has local variables during method call
    • Method calls add local variable to the stack and a return removes them (revert the stack state to before the method was called)
    • Memory is automatically allocated and deallocated
  • Heap (free store): where objects are stored; where new operator allocates memory for objects