DEV Community

Sydney Andre
Sydney Andre

Posted on

Comparing Java and JavaScript

In this article, we will explore the similarities and differences between Java and JavaScript. As a software developer, I learned JavaScript first and have recently begun digging into Java concepts. This article will explain some history about each language, the key differences between them, and provide code snippets for some Java basics.

About the Languages

Both Java and JavaScript are object oriented programming languages that utilize C-style syntax. Both were developed in the 1990s; James Gosling began creating Java in 1991 and Brenden Eich invented what is known today as JavaScript in 1995. The first version of each was released in 1996. At their conceptions, Java was primarily used for back-end development and JavaScript for front-end. As they have evolved over the last few decades, the uses for both have expanded. Java now is primarily used in Android mobile application development, but still continues to be used for server applications, as well as software and web development. Similarly, JavaScript's uses have continued to grow allowing developers to create full applications with JavaScript and its related frameworks.

Key Feature Differences

Now that we have reviewed some of their similarities, let's look at a few differences. One of the most significant differences between the languages is how the machine runs the code. Java is a compiled, then interpreted language; whereas, JavaScript is just interpreted. Java code is run through a compiler that creates bytecode that is then interpreted by the Java Virtual Machine. This allows Java much more portability than other compiled languages whose generated machine code is platform-specific. Because of this, Java often gets the tagline "write once, run anywhere". Java gets the best of both worlds--speed of a compiled language with the flexibility of an interpreted language. Interpreted languages run code by reading each line one by one then translating to machine code. When interpreted languages first came out, they were much slower than compiled languages, but as they have evolved performance gap between the two has shrunk.

Another difference is that Java is multi-threaded and JavaScript is single-threaded. This means Java can execute multiple tasks simultaneously; whereas, JavaScript can only run one line of code at a time. This sounds like a big downfall for JavaScript, but by utilizing asynchronous techniques, its performance is not hindered as much as it may seem.

Lastly, Java is class-based and JavaScript is prototype based. This refers to an inheritance style in object-oriented programming. In JavaScript objects do not need to be defined in a class to inherit properties of another object as they can inherit from the prototype property. On the other hand, object inheritance in Java, occurs when an object created in a class.

Beginner Java Concepts

Next we are going to walk through some basic Java concepts. First, when setting up a Java file, you will need to define the package which refers to the directory. All other code must be wrapped inside of a class and the name of the class must match the file name.

package JavaExample;

public class Tutorial {
   //start writing in Java

}
Enter fullscreen mode Exit fullscreen mode

Other basics in Java feel similar to JavaScript such as operators, string and array methods like .length() and accessing elements in arrays and strings with bracket notation. Variable declaration is a bit different as they are declared and defined with keywords that indicate their datatype. Lets look at some examples in our Tutorial class.

package JavaExample;

public class Tutorial {
    //primitive variables
    int myNumber = 0;
    char myChar = 's';
    boolean myBoolean = true;

    //non-primitive variables
    String myString = "Java is Fun!";
    int[] myArray = {5, 3, 6, 8};
}
Enter fullscreen mode Exit fullscreen mode

Methods, or functions as you may know them from JavaScript, are also defined with some additional keywords that indicate how they can be accessed and what datatype it is expected to return. Methods declared with the keyword static can be accessed without creating an object of the class but cannot be accessed in other classes. Those declared with public can be accessed in other classes but must be accessed by objects of the class. If the method is not returning a value, you will use the keyword void after static or public, but if it is returning a value, you will use the datatype keyword.

package JavaExample;

public class Tutorial {

//static method returning an integer
  static int arraySum(int[] args){
    /* creating an object from the class 
    to access variable from above */
    Tutorial myVar = new Tutorial();

    //for loop shorthand
    for(int i : args){
            myVar.myNumber += i;
        }
        return myVar.myNumber;
   }

//public method not returning a value
   public void printMe(String words){
    //print to the console
    System.out.println(words);
}
Enter fullscreen mode Exit fullscreen mode

Lastly, we will take a look at Java's main method. This is a built in method that is used to the project's code. All Java projects must use the main method at least once. Best practices dictates there should be one file responsible for running the Java code and the other classes will define the functionalities. In this example we will see the difference in how public vs. static methods are invoked.

package JavaExample;

public class Tutorial {
    public static void main(String[] args)
    {
        //access public method by creating an object
        Tutorial myObj = new Tutorial();

        //Public method invocation
        int addedNumber = arraySum(myObj.myArray);
        System.out.println(addedNumber);

        //Static method invocation
        myObj.printMe("hey! learning is fun!");
    }

}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Many say that starting the JavaScript for fresh beginners is a good idea as it is a more straightforward language with less 'magic' happening in the background, so as a new JavaScript developer, I was interested to see how easily or difficult understanding Java concepts would be. Although, I have not fully dug into all of its complexities, the learning curve of understanding these basic Java concepts was much flatter than starting off fresh with JavaScript.

I hope this article helps you in your coding journey!

Top comments (0)