DEV Community

Cover image for Java vs JavaScript. Key differences and similarities
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com

Java vs JavaScript. Key differences and similarities

The software programming world has progressed rapidly over time and has a lot of programming languages to offer. You might have heard about programming languages Java and JavaScript that sound similar in name and might have got confused about whether they're different names for the same language or are they different. Well, all your confusion will be clear after this brief yet full of knowledge blog.

What is Java?

Java is an OOP (Object-orientated programming language), class-based, and multi-platform programming language having a VM (Virtual Machine) platform. OOP is a programming paradigm based on the concept of objects containing the code and data. The Virtual Machine helps you create compiled programs that have the flexibility to run anywhere, on any platform. Java refers to this concept as Write Once, Run Anywhere.

How to get started with writing code in Java

There are two simple set-up steps you need to follow:

  1. Download and set up the Java SE Development kit
  2. Choose any text editor that you like

I recommended you choose Notepad as a complete beginner since it's the most basic one. A different text editor or an IDE will also work, just try not to get too fancy, as you want to focus on the essentials now.

Writing a simple code in Java

Let's see how you can simply print a hello world program in Java.

// A Java Program used for printing a simple string (character input type, e.g. HelloWorld),

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the code

  • The first line you see starts with the generic comment statement that describes what our program does. You can see that it starts with // which is a symbolic representation of a single line comment (a comment that's restricted to a single line).

  • Before we move onto the next line, let's explore the curly braces. Opening curly brace { refers to the opening of the respective class or function, whereas ending curly brace } refers to where it ends.

  • class refers to an object constructor or, in simple words, a blueprint from which we create objects. Each class has a name, and in this case, our class name is HelloWorld.

  • Now coming towards the main method of our program, that is

  public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

. Let's break this down into chunks:

  1. public refers to this method being accessible to anyone from anywhere, which means it is globally available.
  2. static is a keyword that simply defines that this method is static and can't be changed.
  3. void is also a keyword that refers to a method not returning anything.
  4. main refers to the name of the Java main method.
  5. String args[] refers to the arguments that are being passed to the main method. args refers to the name of the string. You can name args with any other string name, it's just a standard naming used for it.
  6. System.out.println("Hello, World!"); This is the actual output line where our Hello, World! gets printed. Individual statements and variable declarations end with a semi-colon.

Sounds pretty complex for a program that just prints out a single string to the screen? That's true, and it's one of the reasons why learning JavaScript is a much better choice for beginner devs.

What is JavaScript?

JavaScript is a high-level scripting programming language that is lightweight and is normally used to make web pages dynamic and interactive. It can add dynamic text into HTML and is known for being the browser's language. We will discuss its detailed features in its comparison with Java.

How to get started with writing code in JavaScript

You just need to have the following 2 things to get started with JavaScript:

  1. Browser (Google Chrome, Safari, Firefox, Internet Explorer)
  2. Text editor (Notepad, VS Code, Atom)

How to write your first JavaScript program

Let's see how you can write your first hello world program in JavaScript.

// A JavaScript Program used for printing a simple string (character input type, e.g. HelloWorld),

console.log('Hello World');
Enter fullscreen mode Exit fullscreen mode

Explanation of the code

  • The first line is again the same single line comment, as we previously saw for the Java program.

  • In JavaScript, we simply use the statement

  console.log
Enter fullscreen mode Exit fullscreen mode

to print anything on the console (an object which provides access to the browser debugging console). In short, you can view it when:

  1. You right-click on your mouse
  2. Click on inspect
  3. Select the console tab
  4. View the logged statement in the console

Now tell me, how much easier it is when compared to Java?

You're right, it's not even close.

Differences between Java and JavaScript

We can note the following major differences amongst the two languages:

  • Programming Paradigm: Java strictly follows the object-oriented programming paradigm, whereas JavaScript follows a multi-paradigm that includes object-oriented programming, procedural, and scripting programming language.
  • Code Execution: Java applications have the flexibility of running on the JVM (virtual run time environment for Java), whereas JavaScript runs only on Browser-specific application-specific environments (Node.js is a different story).
  • Objects: Java objects are purely class-based whereas JavaScript objects are prototype-based.
  • Type Checking: Java ensures strong type checking of its variables and functions before compilation, which makes the probability run or compile-time run quite low. However, JavaScript is weakly typed, the type of the variables is unknown until they get compiled, and hence, the chances of run or compile-time errors increase.
  • File Extension: Java has a .java file extension, whereas JavaScript has a .js file extension.
  • Multithreading: Java supports multithreading (a process of executing two or more threads simultaneously to maximum utilization of CPU), whereas JavaScript doesn't.
  • Memory Usage: Since Java has a lot going around in it, it occupies more space, whereas JavaScript occupies less.
  • Language Dependency: Both languages can work independently on their own and can also pair up with other languages.
  • Concurrency Approach: Java takes advantage of its multithreading ability and has a thread-based approach, whereas JavaScript follows an event-driven approach.
  • Performance: Scripting languages are always more efficient than pure programming languages because of their nature of engagement, and hence, Java is less efficient and slow as compared to JavaScript.

Similarities between Java and JavaScript

Any of the differences, right? Let's also explore what do these two have in common.

  • Browser Compatibility: Both languages can run on a browser.

  • Support: Both languages have a lot of online support community.

  • Syntax and Programming Concepts:

Though both are two different languages, both share the same core programming concepts and some syntax concepts such as:

  1. Use of programming loops such as for loop, while loop.
  2. Use of conditional statements such as if and else if.
  3. Use of Math libraries such as Math.pow.
  4. Common shared syntax signs such as code block delimiters { }, semi-colon to end code statements.
  • Similar Name: This one's strange, but both Java and JavaScript have "Java" in common, though both languages are completely different.

Conclusion

To sum it up, Java and JavaScript are two different programming languages. A few similarities in them mainly come from the core programming principles, but apart from that, they represent two different worlds that share a similar name but diverge from each other majorly.

Get my free e-book to prepare for the technical interview or start to Learn Full-Stack JavaScript

Top comments (11)

Collapse
 
leoloopy profile image
Leo • Edited

Nice write-up.
My time learning JS as a complete beginner was hell..lots of confusing concept, got even more mindbending when I was learning asynchronous JavaScript, with all this experienced I've had I don't see myself learning a language that uses 3-4 lines in printing "Hello world", probably because of my laziness 😁. Good luck with y'all struggling to get a hold of it 🤞

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

JS is way easier to get started when compared to Java.

Collapse
 
devdufutur profile image
Rudy Nappée • Edited

Browser Compatibility: Both languages can run on a browser.

Really ?

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Well, of course JS is native to all modern web browsers, but Java could be enabled as well 👉 details from java.com

Collapse
 
devdufutur profile image
Rudy Nappée

If you think applets, they are deprecated since Jdk 9 and removed in Jdk 11.

Thread Thread
 
coderslang profile image
Coderslang: Become a Software Engineer

I know projects that still run on JDK 5. Enterprise is hard.

Thread Thread
 
devdufutur profile image
Rudy Nappée

You can control the jdk version on your server, but you can't force your users installing an outdated version.

Collapse
 
reergymerej profile image
ᴙɘɘᴙgYmɘᴙɘj

My favorite quote on the subject

Java is to JavaScript as car is to carpet.

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Absolutely 😂. But if you're just starting out and choosing the first programming language to learn the terminology is quite confusing.

Collapse
 
beendra profile image
Alexandra

Great write up!

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer

Thank you 🙏