Comparing 2 weirdly different languages to get a better understanding. Some may like this approach and some won't. But no other ways to find out. And this is not like that one place that you find everything that you need to know about these languages. this will more like be an article on where it is and where might these be heading to.
I'm a bit hesitant on recommending this to absolute beginners as some of the words can be confusing. But still I'll try to use as mush as simple words to explain these concepts.
As the Programming industry has been constantly improving with no slow downs, we have ended up with a load of technologies and languages to learn. And when you look at it from a beginner perspective it can be so confusing. So, I'll to give a little bit of overall on what has changed on these two languages and how they work. Because java is one of the oldest and a most solid language and JavaScript on the other hand is mentioned as some kind of a modern language and so I think it'll be interesting to compare them and see what has changed.
Before we go deep down let me give a little overview on..
Where we use these languages.
Java is a general-purpose programming language that is widely used in a variety of contexts, including desktop app development, web development, mobile development, and enterprise applications.
Java is known for its portability, scalability, and performance, and it is commonly used to build large-scale applications that need to run on a variety of platforms.
And JavaScript is also a general-purpose programming language, but it is primarily used for web development. JavaScript is used to add interactivity and dynamic behavior to web pages, and it is supported by all modern web browsers. JavaScript is also used to build server-side applications using runtime environments like Node.js, and it is increasingly being used for mobile development using frameworks like React Native.
So, generally we can use both languages to build Desktop apps, Mobile apps and Server side apps with their own pros and cons.
And that is also a main reason for choosing this topic as both of them are competitive with their abilities.
And now go a bit deep and look at a core attribute of these languages.
How these languages talks with the processor.
Even though they mostly serve similar purposes the way that they talk to the processor (central processing unit or CPU) can be quite different.
In Java, the code is compiled into bytecode, which is a low-level machine-readable format that can be executed by the Java Virtual Machine (JVM). The JVM is a runtime environment that is responsible for executing the bytecode and managing the memory and resources of the program. When the JVM runs the bytecode, it translates the instructions into machine code that can be understood by the CPU.
In JavaScript, the code is typically interpreted by a JavaScript engine, which is a software program that executes JavaScript code. The JavaScript engine reads the code and translates it into machine code that can be understood by the CPU. Unlike Java, JavaScript is not compiled into a standalone executable file (You can find a ton of tutorials on how to compile java code on YouTube), but is instead executed directly by the JavaScript engine as it is encountered in a web page or other context.
JavaScript engine may differ from the browser you use.
Data types
In both Java and JavaScript, variables are used to store values in memory. However, the way that these values are stored and accessed in memory can be quite different between the two languages.
In Java, variables are stored in a specific location in memory, and the location is determined at runtime by the Java Virtual Machine (JVM). The JVM also manages the lifetime of variables, allocating and deallocating memory as needed. Java has a number of different data types, including primitive types (such as int, double, and char) and reference types (such as objects and arrays). Primitive types are stored directly in memory, while reference types are stored as pointers to the memory location where the object is stored.
In JavaScript, variables are also stored in memory, but the way that they are stored and accessed is quite different from Java. JavaScript is a dynamically-typed language, which means that the type of a variable is determined at runtime and can change during the lifetime of the variable. JavaScript variables are not bound to a specific location in memory and are instead stored in a variable object associated with the current execution context. JavaScript has a number of different data types, including primitive types (such as number, string, and boolean) and reference types (such as objects and arrays). Primitive types are stored directly in memory, while reference types are stored as references to the objects they represent.
Also something we often hear is that Java is a statically typed language and JavaScript is a dynamically typed language. And recently TypeScript was introduced and it works like JavaScript with types and more. And java also recently introduced the var
keyword in Java 10 which acts like a dynamic datatype. Still it is not a dynamic data type it just allows you to declare variables without specifying an explicit type, and the type is inferred from the initializer expression. So, here also we can see these languages are having some similar moves.
Understanding these can be an advantage when working with either language.
Object Orientation
Object oriented programming is not specific to any languages. It is a concept used in programming and can be achieved with their own ways with different languages.
Java
Java can be one of the first languages that comes to our mind when we hear OOP. And below I'll add the key areas of how these two languages handle this technique.
- Java is a class-based OOP language, which means that objects are created based on a class definition.
Example for class-based OOP -
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void greet() {
System.out.println("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
}
}
To create a new Person object in Java, you would use the new operator like this:
Person p = new Person("John", 30);
p.greet(); // Output: "Hello, my name is John and I am 30 years old."
Java has four access modifiers (public, protected, private, and default) that determine the visibility and accessibility of class members (fields, methods, etc.).
In Java, it is possible to define multiple methods with the same name but different parameter lists, a technique known as method overloading.
Java has a concept of static members, which are associated with a class rather than a specific instance of the class.
Java has a feature called interfaces, which allow a class to specify a set of methods that it must implement.
JavaScript
- JavaScript is a prototype-based OOP language, which means that objects can inherit properties and methods from other objects (called prototypes). (Also note that JavaScript introduced Classes in ES6 which can be used as a syntax to create objects and defining their behaviors).
Example for prototype-based OOP
let person = {
name: 'John',
age: 30,
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
To create a new object that inherits from the person object in JavaScript, you would use the Object.create method like this:
let student = Object.create(person);
student.study = function() {
console.log(this.name + ' is studying.');
};
student.greet(); // Output: "Hello, my name is John"
student.study(); // Output: "John is studying."
class-based OOP languages use classes to define objects, while prototype-based OOP languages use prototypes to create new objects that inherit from existing objects. Both approaches have their own benefits and trade-offs, and the best approach to use depends on the specific needs of the application.
JavaScript does not have access modifiers, and all object properties and methods are public by default.
JavaScript does not support method overloading, but it does allow functions to be defined with optional parameters.
JavaScript does not have a direct equivalent to static members, but it is possible to achieve a similar effect using the static keyword in a class definition.
JavaScript does not have interfaces, but still we can use a regular object with no special behavior or enforcement if we want.
These are just a few of the differences between Java and JavaScript. There are many other differences between the two languages, and it is important to understand the specific features and capabilities of each language when working with them.
I didn't go in-depth about all the features. But I hope someone would find this helpful. And your corrections or suggestions are always welcome as they helps me to improve.
And If you loved it, please don't hesitate to leave a reaction Your support will be very much appreciated.
Top comments (0)