Java vs JavaScript: What's the difference?
Published: 2023-05-02 18:27:00

Like JavaScript, Java is a widely used programming language used to create web applications.  Though these languages serve a similar purpose, a few key differences set them apart.  Before we dive into where these languages contrast, let's talk about something they share in common: their names.

## Java vs JavaScript
While their names are similar, JavaScript and Java are completely unrelated. In fact, they were created by two separate companies. Java was released first in by Sun Microsystems, Inc, with the slogan: "Write once, run anywhere." This reflected the ability of a Java codebase to be compiled in a multitude of ways for a plethora of platforms. Java was well-received and widely adopted from the get-go. Netscape took note of this as they developed their language, which would serve a similar purpose. In an attempt to gain the attention of Java users, JavaScript decided to copy Java's name. As of 2022, roughly 98% of websites use JavaScript for client-side programming.

## scripting vs programming
Now that we know why their names are similar, let's discuss how they are different. While JavaScript is a scripting language, Java is a programming language. This means that when Java code is run, it is sent directly to a compiler. A compiler is essentially a translator that converts source code, a more human language of written words, into machine code, usually binary or hexadecimal. I'll put a link below this article for those who would like to inquire more about machine code. In contrast, JavaScript is sent to an interpreter instead of a compiler. An interpreter's job is virtually the same as a compiler's. Though compilers and interpreters both transform source code into machine code, they accomplish this goal in very different ways.

While a compiler scans the entire program before it runs, an interpreter moves through code one line at a time, running each statement along the way. This behavior gives compilers the advantage of being able to run programs more quickly. Though slower, interpreters are able to stop mid-way through the code's execution. Stopping code during execution streamlines debugging practices and allows developers to make changes on the spot without having to wait for the program to finish running.


## Object-oriented programming
Java and JavaScript do have similarities besides their name. Both utilize object-oriented programming. JavaScript is, however, less confined, allowing for functional programming as well. Object-oriented programming is an archetype where objects and classes are heavily relied upon. For those who may be new to programming, objects are storage containers that can hold both data and code. Data held within an object is referred to as a property and code stored within an object is referred to as a method. A class can be thought of similarly to a vague blueprint to use to create more defined objects. When an object is created from a class, it comes with the properties and methods from its parent class. Likewise, a new class, called a subclass, can also be created from another class. The methods and properties are given to the subclass in the same way. This behavior is called inheritance and, both languages use it in nearly the exact same way. Those who are familiar with JavaScript ES6 class syntax and inheritance patterns will be pleasantly surprised by the similarities in Java's class syntax, should they adopt Java, and visa versa. Here is how class creation looks in each language respectively.


## Java class pattern
```
package com.journaldev.inheritance;

// Creates class Animal
public class Animal {
  // Giving properties to the class
  private boolean vegetarian;
  private String eats;
  private int noOfLegs;

  // Empty constructor incase a new object is created without 
  arguments passed to it
  public Animal(){}

  // Constructor to handle when arguments are passed
  public Animal(boolean veg, String food, int legs){
    this.vegetarian = veg;
    this.eats = food;
    this.noOfLegs = legs;
  }
  // Adding methods to the class
  public boolean isVegetarian() {
    return vegetarian;
  }

  public void setVegetarian(boolean vegetarian) {
    this.vegetarian = vegetarian;
  }

  public String getEats() {
    return eats;
  }

  public void setEats(String eats) {
    this.eats = eats;
  }

  public int getNoOfLegs() {
    return noOfLegs;
  }

  public void setNoOfLegs(int noOfLegs) {
    this.noOfLegs = noOfLegs;
  }

}

// Creates subclass of Animal 
public class Cat extends Animal{
  // adding a new property
  private String color;
  
  // Constructor uses super to gain access to the parents 
  methods and properties
  public Cat(boolean veg, String food, int legs) {
    super(veg, food, legs);
    this.color="White";
  }

  public Cat(boolean veg, String food, int legs, String color){
    super(veg, food, legs);
    this.color=color;
  }
  // Adding methods to the subclass
  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

}
```

## JavaScript

```
// Creates Animal class
class Animal {
  constructor(vegetarian, eats, noOfLegs) {
    this.vegetarian = vegetarian;
    this.eats = eats;
    this.noOfLegs = noOfLegs;
  }

  isVegetarian = () => this.vegetarian;
  setVegetarian = (vegetarian) => { this.vegetarian = vegetarian; }
  // Adding methods to the class
  getEats = () => this.eats;
  setEats = (eats) => { this.eats = eats; }
  getNoOfLegs = () => this.noOfLegs;
  setNoOfLegs = (noOfLegs) => { this.noOfLegs = noOfLegs; }
}

// Creates subclass of Animal
class Cat extends Animal {
  // Constructor uses super to get the properties and methods from parent class
  constructor(vegetarian, eats, noOfLegs, color = "White") {
    super(vegetarian, eats, noOfLegs);
    // Setting a new property on Cat subclass
    this.color = color;
  }
  // Adding methods to Cat subclass
  getColor = () => this.color;
  setColor = (color) => { this.color = color; }
}
```

I bet you were able to understand that Java code better than you predicted. Many different programming languages use similar processes to accomplish similar goals. When picking up a new language, having already mastered another, you will find that you can intuit certain aspects of a language's functionality quite easily. This is your cue to go and explore a new language. Happy Coding.


More on machine code:
https://www.codecademy.com/resources/docs/general/machine-code

Work Cited:

https://www.galvanize.com/blog/story-behind-java-javascript-whats-difference/#:~:text=Java%20Came%20First&text=Later%2C%20the%20project%20went%20by%20the%20name%20Green.

https://www.techopedia.com/definition/7793/interpreter#:~:text=As%20the%20name%20suggests%2C%20an,converts%20or%20executes%20it%20directly.

https://kb.iu.edu/d/agsz#:~:text=The%20difference%20between%20an%20interpreted,program%20written%20in%20assembly%20language.

https://www.baeldung.com/java-pattern-prototype

https://www.w3schools.com/java/java_constructors.asp