JavaScript vs Java: The Facts

Slået op d. - Sidst ændret d.

When you're new to programming and hear words like Java and JavaScript (JS), you'll naturally want to make an association between them.

We're always trying to make sense of the world, looking for patterns and correlations, and it's sometimes easy to see a pattern where there isn’t one. In this particular case, it may seem like JS is derived from Java. The two languages weren't born in their respective vacuum bubbles, of course, but JS as an entirely different kind of language from Java, because a Netscape employee named Brendan Eich built JS for a wholly different purpose.

However, both languages came out, almost back-to-back, in the same year. You may wonder which program to begin learning, but rest assured - the choice is easy to make.

What is Java?

Between the two name-sharing but otherwise distant cousins, Java came first when Sun Microsystems released the first implementation in 1995.

Java is an Object-Oriented Programming (OOP) language that was originally designed with to follow the mantra "write once, run anywhere." The tagline is a reference to the Java Virtual Machine (JVM), which allows any Java code to run on any computer that has the JVM.

Java went through a bit of an identity crisis during naming. It was Oak when it started out, named after the oak tree that stood outside core developer James Gosling's office. This changed to Green, and finally, the developers went with Java, meaning coffee.

Java is primarily a server-side programming language, used to create standalone programs. At the front-end, Java applets are little internet-based programs that can run in HTML, or on any computer. Let us take a look at a simple Java program:

/*

This is a simple Java program

Please save it as welcome.java

*/
class welcome

{

    //A Java program will start from here.

    public static void main(String args[])

    {

        system.out.println("Welcome to Java!!!");

    }

}

When you want to run the program, you have to compile it first using the javac compiler. In the command prompt, type:

C:\javac welcome.java

It will print the output as:

Welcome to Java!!!

You will see that OOP concepts, like the class in which data and methods are hidden to prevent inadvertent changes, are a part of Java just as they are a part of C++ and other OOP languages.

What is JavaScript?

JavaScript is different from Java on several counts. It is also an OOP language but, unlike Java, which is a programming language, JS is a scripting language. It cannot create standalone programs. It needs the support of another language like HTML. You'll commonly find JS inside HTML documents, making web pages more interactive than HTML alone can. So, JS code can only run on browsers.

Here's a look at how JS spices up HTML. Consider this basic HTML document:

<!DOCType HTML>

<html>

<head>

    <title>JavaScript Test</title>

</head>

<body>

    [Place content here]

</body>

</html>

When there's JS in an HTML document, it will usually be in the form of text between the <head></head> tags or <body></body> tags. The JS script will usually be placed between <script></script> tags like this:

<script type="text/javascript">
    document.write("Text written using JS code!");
</script>

When you switch from HTML view to browser view, you'll see this line on the page:

Text written using JS code!

The differences between Java and JS

Let's take a look at the differences between Java and JS, which have to do with the entirely different purposes they were created to serve.

  • Java is compiled, while JS is interpreted. What this means is, Java is compiled into bytecode or binary language that the virtual machine will interpret. JS is written in text and can be directly interpreted by the browser.

  • Both follow the OOP concept of inheritance, but Java follows class-based inheritance, in which the class defines properties, and these are inherited by members of the class. In JS-style prototype inheritance, all the objects can directly inherit properties from all other objects.

  • The Java compiler checks the types of variables in the program at the time of compilation, a behavior that is labeled static type checking. JavaScript, on the other hand, uses dynamic type checking, so the programmer doesn't need to specify the type of variable (double, integer, string, etc.) created in the program.

  • Java handles multiple instructions using multiple threads, so it runs faster. JS uses a queue system to handle concurrency, which works well for the most part.

The tenuous connection between Java and JS

For all the snide remarks you hear from developers along the lines of, “Java is to Javascript what car is to carpet,” there is an invisible connection between the two. JavaScript was originally called Mocha. Developer Eich's boss wanted him to create a language that looked like Java but would run on browsers and appeal to non-professional programmers. Eich created JavaScript in ten days.

After its naming confusion had cleared up, when the new client-side language finally made its appearance December 1995 on Netscape Navigator, it carried the name of Java, which was the hottest programming language at the time. 'JavaScript' was a marketing ploy that worked.

The connection doesn't end there. It is not uncommon to have people - both new to programming, as well as those who have some experience of either Java or JS - wondering whether to pick one over the other.

It appears that when Netscape and Sun entered into a license agreement, they intended JS as a scripting language to complement Java as a compiled language. Today, there are applications where Java and JS come together, such as in Android apps using Java with a NodeJS server or a client-side JS web app with a Java server.

If you have a project to be executed in Java, JS or both languages, you can find a programmer on Freelancer.com. They will be able to demonstrate how the benefits of Java, and JS can be utilized in business applications.

Other similarities between Java and JS

As different as Java and JS are, they have two further similarities besides both embracing the OOP techniques of encapsulation, inheritance, and polymorphism.

Front-end development

You can use both Java and JS for front-end development. Java at the front-end is usually in the form of an applet. Also at the front-end, JS is embedded into HTML.

Back-end development

Both Java and JS can also be used server-side. Java, for example, has been used to power technologies at the back end like JBoss and Apache. Similarly, JS powers Node.js, which is a cross-platform, free and lightweight plugin for Chrome that allows network applications to run easily.

In general, the language you choose to learn will depend on the applications you want to create, and the goals you're trying to achieve. Your individual requirements will determine which language you should use.

Should you learn Java or JavaScript?

Think about learning Java if your project will involve the following: scientific computing, Android apps, big data analysis, server-side techs like Apache, GlassFish, etc., enterprise software, and hardware programming.

Think about learning JavaScript if your project involves React NativePhoneGap and other similar mobile app development platforms, dynamic single-page applications, server-side techs like Node.js, Express.js, etc., and front-end JavaScript techs like jQuery and Ember.js.

When facing a choice to learn either of the two languages, make a decision based on your goals. Do you want to manipulate web pages or write programs that will run on server and client sides? The lists of applications above are not extensive.

Also, while Java continues to be industry standard, it is slowly being replaced by other languages. JavaScript, being a dynamic scripting language, is more forgiving and easier to approach. Keep these in mind when you make your decision.

Which do you think you will choose, and why? Let us know in the comments!

Oprettet 11 august, 2017

LucyKarinsky

Software Developer

Lucy is the Development & Programming Correspondent for Freelancer.com. She is currently based in Sydney.

Næste artikel

Could AI Replace Programmers?