Content
Java crash course for javascript devs
Hey! Do you want to be employed forever in your life? Then learn Java, the programming language that everybody hates but for some reason refuses to die.
I prepped this small post for anyone with interest in Java.
The good news are: As Java is a C based programming language, there’s a lot similitudes between javascript, c#, C and C++!
The bad news are that you are learning Java.
Without any further ado, let’s get down to business.
Hello World
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Lets break this block down:
/*
We are declaring a public class.
*/
public class Main{}
/*
- Main is the entry point of the entire app, this is like main() in languages like C or Go!
- Static is a reserved word that indicates a member (variable, method, block, or nested class)
belongs to the class itself rather than to any specific instance (object) of that class.
- Void represents the type that the function itself will be returning, in this case is nothing,
so void makes sense.
- String[] args represent the type of arguments that the function will have, which will be an array of strings.
*/
public static void main(String[] args) {}
/*
System.out.println is simply the equivalent of console.log()
*/
System.out.println("Hello, world!");
Variables and Types
int age = 30;
double price = 19.99;
boolean canFly = true;
String name = "Superman";
Java is strongly typed. You must declare the type.
No “let” or “const”; Java uses “final” instead of “const”.
final int maxScore = 100; // like const
So as you can imagine, something like this is valid:
int age = 30;
age = 31;
We don’t need to re-write the variable type when changing its value.
Control Flow
if (admin) {
System.out.println("hello!");
} else {
System.out.println("F*ck off");
}
for (int i = 0; i < 420; i++) {
System.out.println(i);
}
while (condition) {
//do something
}
- Similar to JavaScript
- No “forEach” on arrays directly—you use streams or loops.
Methods
public static int add(int a, int b) {
return a + b;
}
- You have to declare the return type, which goes before the function name.
- “static” = as I wrote before, it means this method belongs to class, not object
Classes and Objects
The fundations of java are Classes and Objects.
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void greet() {
System.out.println("Hello, my name is " + name);
}
}
Person p = new Person("Dennis Reynolds", 40);
p.greet();
- () => No arrow functions
- “this” refers to instance fields
- No top-level code—everything must be inside a class
Arrays and Lists
int[] numbers = {1, 2, 3}; // Fixed-size array
import java.util.ArrayList;
ArrayList<String> tvShows = new ArrayList<>();
list.add("Always Sunny");
list.add("The Office");
- “ArrayList” is dynamic like JS arrays
- Use “.get(i)” and “.set(i, value)” for “ArrayList”
Packages (like modules)
package com.myapp.utils;
import java.util.ArrayList;
import com.myapp.utils.MyHelper;
Inheritance
public class Camera {
public void record() {
System.out.println("Recording at 1080p");
}
}
public class SonyFX30 extends Camera {
@Override
public void record() {
System.out.println("Recording at 4k");
}
}
- Use “@Override” decorator when overriding methods while in JS we just re-write the method.
Interfaces (like TypeScript interfaces)
public interface Drawable {
void draw();
}
public class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle");
}
}
- Interfaces have method signatures only
- A class can “implement” multiple interfaces
Conclusion
C based languages rules!