Thursday, July 21, 2022

10 Difference between Java and JavaScript for Programmers

Programmers, developers, and internet users have always been confused between Java and JavaScript.  Many people still think that JavaScript is part of the Java platform, which is not true. In truth, JavaScript has nothing to do with Java, the only common thing between them is the 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 the Inventor of Java, popularly known as the father of Java. 

While in today's world calling JavaScript just a client-side scripting language would not be good, as it's 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 differences between Java and JavaScript, from how they are written, compiled, and executed. Even the capability of Java and JavaScript vary significantly. Java is a full feature Object-oriented programming language, used almost everywhere, starting from programming credit cards to server-side coding.

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, though there are other alternatives like Flash, 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 on an 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 will share some key differences between Java and JavaScript, mostly from a programmer's perspective. And, If you want to learn Modern JavaScript or level up your skills then I suggest you join the best JavaScript online courses from Udemy, Coursera, and Pluralsight. It's one of the best and hands-on courses to learn ES 6 and other new JavaScript features.


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.

1. Execution Environment

The first difference between Java and JavaScript is that Java is compiled + interpreted language, Java code is first compiled into class files containing byte code and then executed by JVM, on the other hand, JavaScript code is directly executed by the browser. One more difference that comes to from this fact is that Java is run inside JVM and needs JDK or JRE for running, on there other hand JavaScript runs inside the 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 dynamically typed language, while Java is a statically typed language. This means, variables are declared with type at compile-time, and can only accept values permitted for that type, other hand variables are declared using var 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 another using == operator, JavaScript performs type coercion. Though it also provides === operator to perform a 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 functions. In simple words, you can pass a function as an argument to another function. Java doesn't treat methods as first-class citizens and the only way to simulate closure is by using an anonymous classes. 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.

Difference between Java and JavaScript



4. OOP

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



5. Right Once Run Anywhere

Java uses byte code to achieve platform independence, 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. 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 the 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 the 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 a dynamically typed language.



10. Applicability

Last but not the least, JavaScript has its own space, sitting cozy along with HTML and CSS in Web development, while Java is everywhere. Though both have a good number of open source libraries to kick start development, jQuery has certainly brought JavaScript on the forefront.


That's all about the difference between Java and JavaScript language. As I said, they are totally different languages, one is a general-purpose programming language, while the other is a scripting language for HTML. Though you can do lot of fancy stuff using JavaScript, you still don't have features like multithreading, as compared to Java. 

By the way, JavaScript was originally named as Livescrpit, which may be due to the fact that it makes your HTML pages live, and the programming world would certainly be free of this confusion, had Netscape hadn't renamed LiveScript as JavaScript.


12 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.

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!

Unknown 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 ;)

Unknown said...

Where is point 6 ?

Unknown said...

Yes, very basic difference from old times.

Hasan Shahriar said...

point 6?

Unknown said...

Point 6 is missing

Aman said...

Yes ! You are correct

Unknown said...

Point 6 is not showing in this above points

Unknown said...

Point 6is not showing here please show this for more information

Post a Comment