Difference Between Java and JavaScript

Let’s see, What is the difference between Java and JavaScript. Many of us think that JavaScript is part of Java platform, which is not true. In truth, JavaScript has nothing to do with Java, the only common thing between them is word “Java” Java is high-level Programing language and JavaScript is a scripting language.

 

JavaScript is a client-side scripting language for HTML, developed by Netscape Inc, while Java is a programming language, developed by Sun Microsystems. There is the difference between Java and JavaScript, from how they are written, compiled and executed. Even capability of Java and JavaScript vary significantly.

Java is an Object oriented programming language, used in almost everywhere mainly used for server side programming. Android uses Java as a programming language for creating Android apps, Swing is a Java API used to create desktop applications and Java EE is a Java platform for developing web and enterprise applications.
On the other hand, JavaScript is primarily used to bring interactivity into web pages. JavaScript is the most popular one and regaining lots of ground lost earlier with the introduction of powerful and easy to use libraries like jQuery and jQuery UI. You can use JavaScript to validate user input, create animation and cool effects in HTML page and can do a lot of interactive stuff e.g. reacting on button click, mouse movement, image click etc. In this article, I am sharing some key differences between Java and JavaScript, mostly from a programmers perspective.

 

Difference Between Java and JavaScript

1. OOPS –

Java is an Object Oriented Programming language, and though JavaScript also supports class and object, it’s more like an object-oriented scripting language. It’s much easier to structure code of large enterprise application in Java than JavaScript. Java provides packages to group related class together, provides much better deployment control using JAR, WAR and EAR as well.

 

2. Write Once Run Anywhere –

Java uses bytecode for execution, which is platform independent because java application byte code is executed by JVM and have no interaction with OS. JavaScript directly runs on the browser, but code written in JavaScript is subject to browser compatibility issue i.e. certain code which works in Mozilla Firefox, may not work in Internet Explorer 7 or 8.

 

3. Static vs Dynamic Typed language

JavaScript is a dynamically typed language, while Java is a statically typed language. It means, In Java variables are declared with type at compile time, and can only accept values permitted for that type, other hand in JavaScript variables are declared using vary keyword and can accept different kinds of value e.g. String, numeric and boolean etc.

 

4. Support of Closures

JavaScript supports closures, in form of anonymous function. In simple words, you can pass a function as an argument to another function. Java doesn’t treat the method as first class citizen and the only way to simulate closure is by using an anonymous class. By the way Java 8 has brought real closure support in Java in form of lambda expression and this has made things much easier.

 

5. Constructors

Java has a concept of constructors, which has some special properties e.g. constructor chaining and ensuring that super class constructor runs before subclass, on the other hand, JavaScript constructors are just another function. There are no special rules for constructors in JavaScript e.g. they cannot have return type or their name must be same as the class.

 

6. NullPointerException

JavaScript is much more forgiving than Java, you don’t have NullPointerException in JavaScript, your variable can accept different kinds of data because of JavaScript is dynamically typed language. In javaScript, You get the errors like – ‘name’ is undefined.

 

There are much other difference as well but we have picked the main difference between Java and JavaScript. If you have any query or question regarding this then you can write us inside the comments.

0 Comment

Leave a Reply