
07 Oct 2024
•
Ernesto Ballon
•
5 min read
Comprehensive comparison between Java and JavaScript in terms of declaration, data structures, loops, and other key features.
| Feature | Java | JavaScript |
|---|---|---|
| Language Type | Statically typed, compiled language | Dynamically typed, interpreted language |
| Execution | Runs on the Java Virtual Machine (JVM) | Runs in the browser or Node.js environment |
| Typing | Strongly typed (variables must have a specific type) | Loosely typed (variable types are inferred dynamically) |
| Feature | Java | JavaScript |
|---|---|---|
| Variable Declaration | Must specify the type (e.g., int, String) | var, let, and const are used, no type declaration required |
| Example | int age = 25; | let age = 25; (or var / const) |
int, boolean, String).| Feature | Java | JavaScript |
|---|---|---|
| Function Declaration | You must declare the return type (e.g., void, int) | Functions are declared with function keyword or arrow functions |
| Example | java public void greet() { System.out.println("Hi"); } | js function greet() { console.log("Hi"); } |
function or arrow syntax (()=>).| Feature | Java | JavaScript |
|---|---|---|
| Primitive Types | int, float, double, boolean, char, byte, etc. | number, string, boolean, undefined, null, symbol, bigint |
| String Type | String class | string (primitive type) |
| Example | int num = 10; | let num = 10; |
number to represent all numeric types.| Feature | Java | JavaScript |
|---|---|---|
| Objects | Defined using classes (Object is base class) | Objects are key-value pairs (dynamic, no class needed) |
| Example | Person person = new Person(); | let person = { name: "John", age: 30 }; |
| Feature | Java | JavaScript |
|---|---|---|
| Arrays | Fixed-size arrays (int[], String[]) | Dynamic arrays (Array object) |
| List | ArrayList, LinkedList, List interface | JavaScript has Array, which acts like List |
| Map | HashMap, TreeMap, Hashtable | JavaScript’s Object and Map for key-value pairs |
| Set | HashSet, TreeSet, LinkedHashSet | JavaScript’s Set for unique values |
List<String> fruits = new ArrayList<>(); fruits.add("Apple");let fruits = ["Apple", "Banana"];Both Java and JavaScript provide similar looping constructs, but there are slight differences in syntax and features.
| Feature | Java | JavaScript |
|---|---|---|
| Basic For Loop | for (int i = 0; i < 10; i++) | for (let i = 0; i < 10; i++) |
| For-Each Loop | for (String item : list) | for (let item of array) |
| Feature | Java | JavaScript |
|---|---|---|
| While Loop | while (condition) | while (condition) |
| Do-While Loop | do { ... } while (condition) | do { ... } while (condition) |
for (String fruit : fruits) { System.out.println(fruit); }for (let fruit of fruits) { console.log(fruit); }| Feature | Java | JavaScript |
|---|---|---|
| Class Declaration | class MyClass {} | class MyClass {} (ES6 introduced class syntax) |
| Object Instantiation | MyClass obj = new MyClass(); | let obj = new MyClass(); |
| Inheritance | class SubClass extends SuperClass {} | class SubClass extends SuperClass {} |
Both languages support object-oriented programming with classes, inheritance, and encapsulation. However:
public class Car { private String model; public Car(String model) { this.model = model; } public void drive() { System.out.println("Driving " + model); } }class Car { constructor(model) { this.model = model; } drive() { console.log(`Driving ${this.model}`); } }| Feature | Java | JavaScript |
|---|---|---|
| Try-Catch | try { ... } catch (Exception e) { ... } | try { ... } catch (err) { ... } |
| Finally | finally block for cleanup | finally block for cleanup |
| Checked Exceptions | Yes (e.g., IOException) | No (only runtime errors, all are unchecked) |
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Error: Division by zero"); } finally { System.out.println("Cleanup"); }try { let result = 10 / 0; } catch (err) { console.log("Error: Division by zero"); } finally { console.log("Cleanup"); }| Feature | Java | JavaScript |
|---|---|---|
| Multithreading | Yes (supports multithreading with Thread class or ExecutorService) | No real multithreading (single-threaded, event-driven) |
| Asynchronous | Handled via Runnable, Callable, Future | Handled with callbacks, Promises, and async/await |
async/await and Promises, asynchronous code can look cleaner.async function fetchData() { let data = await fetch('https://api.example.com/data'); console.log(data); }Runnable task = () -> { System.out.println("Running task"); }; new Thread(task).start();module-info.java), allowing modularized development.import and export statements), as well as CommonJS (require) for Node.js.finalize() and manual techniques.| Feature | Java | JavaScript |
|---|---|---|
| Typing | Strong (static) | Weak (dynamic) |
| Compilation | Compiled | Interpreted |
| Classes | Strict OOP (class-based inheritance) | Prototype-based, class support since ES6 |
| Threads | Real multithreading | Single-threaded (event-driven with async) |
| Data Structures | Rich and strict (List, Map, Set, Array) | Dynamic (Array, Object, Map, Set) |
| Exception Handling | Checked and unchecked exceptions | Only unchecked runtime errors |
Both languages are powerful but serve different purposes: Java for large-scale, strongly-typed enterprise systems, and JavaScript for web development and event-driven environments.