Last year, I revisited programming, focusing on Java after a break. My goal was a more challenging language than Python. I dedicated 3-4 months to mastering Java's core concepts: its operational mechanics, object-oriented programming (OOP), memory management, and database connectivity via JDBC.
Recently, I transitioned into web development, beginning with JavaScript. After a few days exploring JavaScript, I'm eager to share my observations and insights on the key distinctions between Java and JavaScript, and how a smooth transition is possible.
The most striking difference lies in data type declaration. JavaScript is dynamically typed, unlike Java's static typing. While both approaches have merits and drawbacks, we'll focus on the key distinction: JavaScript's single-threaded nature contrasts with Java's multi-threaded capabilities.
Imagine Java as a meticulous, detail-oriented soldier. Instructions must be precise, specifying both the task and its rationale. JavaScript, conversely, resembles a relaxed friend; it completes tasks efficiently without excessive detail—simply provide the task, and it handles the execution.
Java mandates explicit data type declaration (e.g., String
, int
, float
, boolean
) when defining variables. JavaScript, however, is flexible; variables are declared using let
or const
(for constants), with type determination occurring at runtime. While var
served a similar purpose, let
and const
are now preferred.
Java Example:
class Main {
public static void main(String[] args) {
int a = 5;
int b = 6;
int result = a b;
System.out.println(result);
}
}
JavaScript Example:
let a = 5;
let b = 6;
let sum = a b;
console.log("The sum is: " sum);
Understanding OOP in JavaScript is crucial, given its widespread use. Beyond JavaScript's dynamic typing, it's prototype-based, inheriting properties via prototypes. Post-ES6, JavaScript fully supports OOP with classes, but fundamentally relies on prototypes.
The following code snippets illustrate syntactic differences in class and method creation:
Java:
class Person {
String name;
Person(String name) {
this.name = name;
}
void greet() {
System.out.println("Hello, " name);
}
public static void main(String[] args) {
Person person = new Person("John");
person.greet();
}
}
JavaScript:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}`);
}
}
const person = new Person("John");
person.greet();
In Java, object creation requires a class definition. JavaScript offers flexibility; objects can be created directly without explicit class declarations.
Java demands that every object be an instance of a class. JavaScript allows direct object creation:
JavaScript
const person = {
name: "John",
greet: function() {
console.log(`Hello, ${this.name}`);
}
};
person.greet();
Inheritance syntax shows similarities across both languages.
Java:
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
JavaScript:
class Animal {
sound() {
console.log("Some sound");
}
}
class Dog extends Animal {
sound() {
console.log("Bark");
}
}
const dog = new Dog();
dog.sound();
JavaScript's encapsulation is weaker than Java's; it lacks access modifiers (private
, protected
, public
). However, the #
(private field) notation, introduced in ES2022, provides a degree of private field control:
class Person {
#name;
constructor(name) {
this.#name = name;
}
greet() {
console.log(`Hello, ${this.#name}`);
}
}
const person = new Person("John");
person.greet();
console.log(person.#name); // Error: Private field '#name' must be declared in an enclosing class
Java supports compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).
JavaScript doesn't support method overloading due to its interpreted nature and lack of compile-time checks. Method overriding, however, is supported:
class Animal {
sound() {
console.log("Some sound");
}
}
class Dog extends Animal {
sound() {
console.log("Bark");
}
}
const dog = new Dog();
dog.sound(); // Bark
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3