java vs javascript | Difference between Java and JavaScript


Java is an object-oriented programming language initially developed by sun microsystems, and later acquired by Oracle. It is a platform-independent language. i.e. write once and run anywhere. Core Java provides the basic programming features and concepts, whereas advanced Java provides APIs and libraries to develop a varied range of advanced applications. Some of the advanced Java editions are:

1. J2ME (Java 2 micro edition) for mobile application development.
2. J2EE (Java 2 enterprise edition) for web application development.

A Java code needs to be compiled i.e. converted into a byte code before execution. JDK and JRE setup must be completed for writing and executing a Java program. JDK is a Java development kit used to develop a Java application or program, whereas JRE is a Java run time environment needed to execute or run the Java program.



Java is a programming language used to develop a wide range of applications whereas JavaScript is a scripting language that executes on a client i.e. browser and no server interaction is involved. Javascript helps in creating dynamic web pages, every line of javascript code is first interpreted and executed line by line by the browser engine, whereas in Java, the entire code is first compiled into a class file (byte code) and then the compiled code gets executed.

java source file and byte code

Javascript interacts with DOM elements (for e.g. fetching and updating the inner html elements) and gives us a dynamic page with zero or no server interaction. The core usage of javascript is to interact with inner HTML code, perform operations on these inner HTML elements, and render a dynamic and responsive webpage based on the user's action.



Steps to get started with Java


1. Download JDK

2. Go to the advanced system settings and set the environment variables - user variables and system variables like - classpath, system properties, etc.

setting env variables

3. Create a Java file and put the below code in the file:

java source code

Remember, the file name should be the same as the class name given in the source code.

4. To compile the Java code, use the below command in the command prompt:

javac helloworld.java

5. To run the Java code, use the below command in the command prompt:

java helloworld

compiling and executing java code


Javascript on the other hand does not require any installation. You can write the javascript code in your html file (web page source) OR you can create a separate javascript file and include that in your HTML page (As shown below).

external js code

This JS code will be triggered on a user action on the web page. i.e. when you launch your web page and perform some user action that triggers the javascript code, the javascript code gets executed by the browser engine. This JS code then interacts with page elements, manipulates the page elements, and ultimately renders a dynamic response over the web page.



Summarised the difference between Java and javascript

Java JavaScript
It is an object-oriented programming language It is also an object-oriented scripting language conforming to all the ECMAScript specifications
Primarily used for programming and applications development pertaining to web, server-side, mobile, socket, I/O, applet, servlet, etc. It is used for creating dynamic web pages.
The java code needs to be first compiled before execution JavaScript code need not have to be compiled. It gets interpreted and executed line by line by the browser engine.
Prior installation and setup of JDK, JRE, and JVM are required for creating and executing the Java program. Only a compatible browser is required that can execute the javascript code.
Java servlets can be used for server-side programming It can be used for server-side programming if combined with node programming that we usually call Node JS.
Spring, Struts, SpringBoot EJB are some of the frameworks developed and based on Java for web development React, Angular and Vue are the frameworks developed and based on JavaScript
Java supports backend/server-side programming JavaScript supports frontend programming by accessing and manipulating the DOM elements and thus provides a dynamic web page without server interaction.
A java source code file has an extension of .java, whereas a compiled java file has an extension of .class JavaScript file need not have to be compiled therefore a javascript file has an extension of .js
Java is a multi-threaded programming language. JavaScript is single threaded language. i.e. just one line of code may be run at once.