10 difference between Java and JavaScript for Programmers

Programmers, developers and internet users  have always been confused between Java and JavaScript.  Many people still thinks that JavaScript is part of Java platform, which is not true. In truth, JavaScript has nothing to do with Java, only common thing between them is word "Java", much like in Car and Carpet, or Grape and Grapefruit. JavaScript is a client side scripting language for HTML, developed by Netscape, Inc, while Java is a programming language, developed by Sun Microsystems. James Gosling is Inventor of Java, popularly known as father of Java. While in today's world calling JavaScript just a client side scripting language would not be good, as its now been used in servers also using node.js and people are doing object oriented development in JavaScript, but that was what it was originally developed. There are several difference between Java and JavaScript, from how they are written, compiled and executed. Even capability of Java and JavaScript vary significantly. Java is full feature Object oriented programming language, used in almost everywhere, starting from programming credit card to server side coding. Android uses Java as 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, though there are other alternatives like Flash, JavaScript is the most popular one and regaining lots of ground lost earlier with 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 lot of interactive stuff e.g. reacting on button click, mouse movement, image click etc. In this article, I will share some key differences between Java and JavaScript, mostly from a programmers perspective.



Difference between Java vs JavaScript

Here is my list of key differences between JavaScript and Java as programming languages. I have worked both on them, mainly used Java for all Server Side development, Android and JavaScript for writing client side scripts to do validation, interactivity, animation and ajax calls.
Difference between Java and JavaScript

1) Execution Environment

First difference between Java and JavaScript is that Java is compiled + interpreted language, Java code is fist compiled into class files containing byte code and than executed by JVM, on the other hand JavaScript code is directly executed by browser. One more difference which comes form this fact is that, Java is run inside JVM and needs JDK or JRE for running, on there other hand JavaScript runs inside browser and almost every modern browser supports JavaScript.



2) Static vs Dynamic Typed language

Another key difference between JavaScript and Java is that, JavaScript is a dynamic typed language, while Java is a statically typed language. Which means, variables are declared with type at compile time, and can only accept values permitted for that type, other hand variables are declared using vary keyword in JavaScript, and can accept different kinds of value e.g. String, numeric and boolean etc. When one variable or value is compared to other using == operator, JavaScript performs type coercion. Though it also provides === operator to perform strict equality check, which checks for type as well. See here for more differences between == and == operator in JavaScript.



3) 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 method as first class citizen and only way to simulate closure is by using 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. It's very easy to write expressive code without much clutter in Java 8.



4) OOP

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 then JavaScript. Java provides packages to group related class together, provides much better deployment control using JAR, WAR and EAR as well.



5) Right Once Run Anywhere

Java uses byte code to achieve platform independence, JavaScript directly runs on browser, but code written in JavaScript is subject to browser compatibility issue i.e. certain code which work in Mozilla Firefox, may not work in Internet Explorer 7 or 8. This is because of browse based implementation of JavaScript. This was really bad until jQuery comes. Its a JavaScript library which helps to free web developers from this browser compatibility issues. This is why I prefer to write code using jQuery rather than using plain old JavaScript code, even if its as simple as calling getElementById() or getElementByName() methods to retrieve DOM elements.



7) Block vs Function based Scoping

Java mainly uses block based scoping i.e. a variable goes out of scope as soon as control comes out of the block, unless until its not a instance or class variable. On the other hand JavaScript mainly uses function based scoping, a variable is accessible in the function they are declared. If you have a global variable and local variable with same name, local will take precedence in JavaScript.



8) Constructors

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



9) 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.



10) Applicability

Last but not the least, JavaScript has it's own space, sitting cozy along with HTML and CSS in Web development, while Java is everywhere. Though both has good number of open source libraries to kick start development, but jQuery has certainly brings JavaScript on fore front.


That's all on difference between Java and JavaScript language. As I said, they are totally different language, one is a general purpose programming language, while other is scripting language for HTML. Though you can do lot of fancy stuffs using JavaScript, you still don't have features like multithreading, as compared to Java. By the way JavaScript was originally named as Livescrpit, may be due to the fact that it makes your HTML pages live, and programming world would certainly be free of this confusion, had Netscape hadn't renamed LiveScript as JavaScript.



7 comments :

oops said...

This post gives me a feeling that its pretty out of date.
Javascript has broken beyond the boundaries of browser and a lot of things mentioned are no more applicable with the advent of nodeJS. (Ex: 5 states that JavaScript directly runs on browser).
Makes me feel that you tried Javascript, did not like and this a page about why one should stick to Java and Javascript only belongs to browser.

Anonymous said...

Dude its 2015 not 2005.

Nimnio said...

@oops, you did not read the entire post. You missed "While in today's world calling JavaScript just a client side scripting language would not be good, as its now been used in servers also using node.js and people are doing object oriented development in JavaScript, but that was what it was originally developed."

I believe this article was written for those who truly do not know what the difference is between Javascript and Java. Javascript developers are probably surprised that those people exist, but they do. I have an equally fun time explaining what "Java EE" is compared to Java.

Anonymous said...

> almost every modern browser supports JavaScript.
*All* modern browsers support JS.

> JavaScript supports closures, in form of anonymous function.
Anonymous functions have nothing to do with closures. Any function can have a closure.

> It's much easier to structure code of large enterprise application in Java then JavaScript
How? You have one module per file, same thing both languages.

> JavaScript directly runs on browser, but code written in JavaScript is subject to browser compatibility issue i.e. certain code which work in Mozilla Firefox, may not work in Internet Explorer 7 or 8
You are confusing JavaScript the language and the Document Object Model.

> If you have a global variable and local variable with same name, local will take precedence in JavaScript.
Variable shadowing works exactly the same in Java as it does in JavaScript.

> JavaScript is much more forgiving than Java, you don't have NullPointerException in JavaScript
Although JavaScript doesn't have NPE's, the circumstances where you would get an NPE will produce an error. It's simply not called NullPointerException, it's otherwise the same thing.

> JavaScript has it's own space, sitting cozy along with HTML and CSS in Web development, while Java is everywhere.
I have a phone that has an OS where the apps are written in JS. You can program robots with JS. You can program watches with JS.

> By the way JavaScript was originally named as Livescrpit, may be due to the fact that it makes your HTML pages live
Netscape also originally had a server side LiveScript environment, so I'd say your guess is wrong.

Anonymous said...

As someone who just learned JS for fun this is a great article with some great discussion points afterwards. Thanks for helping to bring things into perspective guys!

subodh garg said...

I found this post outdated and misleading. With the advent of Node.js, JavaScript is now everywhere(front-end, back-end and even mobile development).
Don't believe me! Go and explore: "MEAN stack" for web development and Ionic/Cordova frameworks for hybrid app development.

P.S. @Javarevisited, Sorry for some harsh feedback here. I am a great fan of your blog and your Java teachings. I myself has learned a lot from your posts. Thank you for these posts. They are really helpful but for this post particularly, I found it quite misleading. Finally, we all are learners in this vast ocean of knowledge ;)

Sanyam koul said...

Where is point 6 ?

Post a Comment