10 Difference between Java and JavaScript for Programmers (original) (raw)
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 JavaScrip_t, 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.
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.