let num = 5;
num = "Hello"; // This is valid in JavaScript
int num = 5;
// num = "Hello"; // This will cause a compilation error in Java
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
let person = new Person("John");
person.sayHello();
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, my name is " + name);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("John");
person.sayHello();
}
}
function add(a, b) {
return a + b;
}
- **Java**:
public class Main {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(3, 5);
System.out.println(result);
}
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
- **Java**:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
.java
files) is compiled into bytecode (.class
files) using the javac
compiler. Then, the bytecode is executed by the Java Virtual Machine (JVM).# Compile Java code
javac Main.java
# Run the compiled code
java Main
try - catch - finally
blocks for error handling.try {
let result = 5 / 0;
} catch (error) {
console.log(error.message);
} finally {
console.log("This will always execute");
}
try - catch - finally
blocks, but it has checked and unchecked exceptions. Checked exceptions must be either caught or declared in the method signature.public class Main {
public static void main(String[] args) {
try {
int result = 5 / 0;
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("This will always execute");
}
}
}
async/await
.function asyncOperation() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Operation completed");
}, 1000);
});
}
async function main() {
try {
let result = await asyncOperation();
console.log(result);
} catch (error) {
console.log(error);
}
}
main();
Thread
class or implementing the Runnable
interface.class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
// module.js
export function greet(name) {
return `Hello, ${name}`;
}
// main.js
import { greet } from './module.js';
console.log(greet("John"));
src/
└── com/
└── example/
├── Main.java
└── util/
└── GreetUtil.java
// GreetUtil.java
package com.example.util;
public class GreetUtil {
public static String greet(String name) {
return "Hello, " + name;
}
}
// Main.java
package com.example;
import com.example.util.GreetUtil;
public class Main {
public static void main(String[] args) {
System.out.println(GreetUtil.greet("John"));
}
}
ArrayList
vs LinkedList
), avoiding unnecessary object creation, and optimizing database queries.Transitioning from JavaScript to Java requires a solid understanding of the fundamental concept differences, usage methods, common practices, and best practices of both languages. By grasping these aspects, developers can make a smoother transition and start building high - quality Java applications. While the initial learning curve may be steep, the skills acquired from both languages can make developers more versatile and valuable in the software development industry.