Monthly Archives: August 2012

What is the Difference Between Java and JavaScript?

If you’re a computer user, especially one who uses the Internet on a regular basis, chances are that you have heard of both Java and JavaScript, two programming languages that some people may unintentionally confuse, due to the similarity of their names.

However, these two programming languages are not the same, and are actually used to do different things, though they do share some of the same syntax and other features. They are also both frequently used by programmers, and in fact, you may use them both everyday and not even know it!

Java

Java, though it shares some syntax and other features with JavaScript, is a totally different programming language, and is used for different purposes than one might use JavaScript for. And actually, Java was created first, before JavaScript existed, which may be where the inspiration of the name “JavaScript” came from.

One of the major differences between Java and JavaScript is that Java is a compiled, high-level, multi-purpose programming language, which can be used for a wide variety of different tasks, both online and in a browser and offline and running on a computer’s operating system, independent of whether or not the computer has an Internet connection.

Unlike JavaScript, which can be written and then immediately executed with a user’s browser, after writing Java code, it must first be compiled, or turned into code that can be understood by the computer, or more specifically, the Java Runtime Environment’s, or JRE’s, virtual machine (JVM), since Java code runs with the help of a virtual machine.

Once a programmer writes their Java code (a “.java” file), and the complies it, it turns into a “.class” file that contains the compiled Java bytecode, which can then be read and understood by the Java Virtual Machine. The JVM, in turn, takes the compiled Java bytecode and then runs, or executes, the code, thus causing the computer to do what the programmer originally asked it to do.

Since Java runs with the help of the Java Runtime Environment and its virtual machine, Java code can be run on any device that supports the installation and running of the JRE and JVM, which includes computers, cell phones, routers, cameras, and other devices!

Another difference between Java and JavaScript is that, when writing code in Java, a programmer has to define which datatype they will be using, which includes defining the type of variable that they would like to use, where as in JavaScript, this is not usually the case.

For example, in Java, there are a variety of different datatypes, such as: String, int, float, and double. (There are also others, which can be found by doing a search online.) In JavaScript, the variable type var can contain all of the above datatypes that one has to specifically define in Java, thus making the writing of Java code a bit trickier.

JavaScript

JavaScript is a type of programming language, as its name implies, called a “scripting language”, and is mainly used in web development, as it allows programmers to add more interactivity and functionality to webpages than just HTML and CSS alone. JavaScript is also frequently used with languages like PHP and MySQL, in the form of AJAX or by use of the jQuery library.

To be more specific, JavaScript is considered a “client side scripting language”, which means, unlike languages, such as PHP, which is a “server side scripting language”, it can be used to interact with a user’s web-browser, adding interactivity that otherwise would not be present with just HTML or PHP alone.

For example, when used in conjunction with the jQuery JavaScript library, JavaScript enables users to enter data into a database without having to refresh the page that they are currently on. This helps to reduce the number of page loads, thus making interactions on a website quicker than they would otherwise be.

An example of how data can be entered and retrieved from a database without having to refresh the page would be with Facebook’s chat service. When a user types a message to another user, the data is entered into a database, but is also then retrieved from the database relatively quickly and shown to the receiving user or users whom the message was intended for.

Because of languages like JavaScript, especially in the form of jQuery and AJAX, the user does not have to refresh the page they are on, making the user experience faster and easier for the user.

Additionally, JavaScript can be used on its own to create alert, or pop-up, boxes, which we have all seen while using the Internet. Also, using JavaScript, programmers and web designers can create redirects, so that a user does not have to enter in another URL, should they need to be transferred or directed to another webpage – the JavaScript code will do this for them automatically.

However, programmers can not create full-featured stand-alone programs that can be compiled and run on a computer’s operating system alone, as JavaScript is intended for use within web-browsers and for other, similar, applications — this is where languages like C, C++, and Java can be useful. 

Similarities

In addition to all of the differences between Java and JavaScript, there are also similarities between the two languages (and other programming languages, as well)!

For example, in both Java and JavaScript, much of the syntax is the same, especially when it comes to writing basic code, such as with loops, if/elseif/else statements, methods, and more!

Also, both languages are object oriented, which means that they both support the use of the various features of object oriented programming, such as the creation and instantiation of classes, manipulating objects, encapsulation, polymorphism, as well as some other features.

However, there may be some slight differences between the various things that can be done when related to OOP, since the two languages are different and serve different purposes, but for the most part, the two languages support much of the same features when it comes to writing object oriented code.

If you were someone who was unsure of what the differences between Java and JavaScript were before reading this post, hopefully you are now in the know as to what the differences and similarities are when it comes to writing and using both Java and JavaScript code!

Also, should you have any questions, comments, or suggestions on this post, or any other post or topic, please feel free to contact me!

Advertisements