Java vs. JavaScript

Java vs JavaScript

Assuming you know Java pretty well and know a little bit about JavaScript, how are you going to answer the interview question “What is the difference between Java and JavaScript”?

You may have used JavaScript in different situations, but do not have deep understanding of the language. This question could be hard. The following lists some points you can talk about to show your knowledge of programming languages in general. It may also show that you can quickly pick up the language, even if you don’t know much about it.

1. Key Differences in High Level

Java is an OOP programming language while JavaScript is an OOP scripting language. Java creates applications that run in a virtual machine or browser, while JavaScript code is run on a browser only. Java code needs to be compiled while JavaScript code does not. JavaScript does not create any stand-alone applications. In its most common form, it resides inside HTML and provide levels of interactivity to web pages that are not achievable with simple HTML.

2. Inheritance

In Java, there are classes, and instances, as separate concepts. In order to do inheritance, you have to use the base class to create a new class, which can then be used to produce derived instances.

Like Java, JavaScript is also an object oriented language, but JavaScript does not use classes.
In JavaScript you don’t define classes and create objects from these classes. JavaScript is prototype based, not class based. To do inheritance, you can use any object instance as a prototype. The following example gives a taste of JavaScript Inheritance.

hello.html

<script>
// define the Person Class
function Person() {}
 
Person.prototype.sayHello = function(){
    alert ('Hello!');
};
 
 
// define the Student class
function Student() {}
 
// inherit Person
Student.prototype = new Person();
 
// replace the sayHello method
Student.prototype.sayHello = function(){
    alert('Hello, I am a student!');
};
 
// add a new method
Student.prototype.sayGoodbye = function(){
    alert('Goodbye!');
};
 
var student = new Student();
student.sayHello();
student.sayGoodbye();
 
// check inheritance
alert(student instanceof Person); // true
alert(student instanceof Student); // true
 
</script>

3. Other Fun Facts

Java and Javascript are similar like Car and Carpet are similar.

Two-Stage vs. Runtime-only Debugging

Both Have Libraries and Frameworks.

4. What Else Is INTERESTING to Mention?

There are so many thing we can say, but saying something important and interesting would be the best.

References:
1. Prototype-based vs. Class-based Inheritance
2. Most Frequently Asked Questions about JavaScript
3. Introduction to Object Oriented JavaScript

7 thoughts on “Java vs. JavaScript”

  1. Java and JavaScript refer to the same language OOP, they use same control structures, operators etc; and comments of JavaScript are same as Java.

  2. Java is a finger while Javascript is fingerlings. Totally different and unrelated – only the first 2 syllables are the same.

  3. “while JavaScript code is run on a browser only … JavaScript does not create any stand-alone applications”: I think javascript code runs in a javascript engine. That’s why we have node.js.
    Not very informative.

Leave a Comment