Why Java is platform-independent but not C++?

Key Personnel

Dilsher Singh

Sr. Manager Creatives

Pramod Sai Naik
     Megavath

Asst. Project Manager

Jobanjit Singh

Sr. Software Engineer

Vishal Pandey

AGM- Operations & General Management

Manjot Singh

Sr. Software Engineer

No items found.

Why Java is platform-independent but not C++?

Author: Jaskaran Singh, Software Engineer at SimbaQuartz
July 7, 2020 | SimbaQuartz

Both Java and C++ are efficient programming languages that use object-oriented programming(OOPs) paradigm. Java follows OOPs more than C++. Both support OOPs features like:

  • Classes

  • Objects

  • Polymorphism

  • Encapsulation

  • Abstraction

  • Abstraction

In this article, we are going to discuss the platform independence of Java and why C++ is not supporting platform independency.

We have heard that java is developed to eliminate the various drawbacks of C, C++. Although Java has mostly inherited its syntax from C++ and some major features as well. Java is preferred in almost all major organizations because it supports “code once, runs anywhere” made it platform-independent and easy to use and upgrade.

Before knowing why java is platform independent let’s have some brief about compiler phases:

Lexical Analyser:

Accepts high-level language code and tokenize the input to a valid set of tokens.

Syntax Analyser:

It checks the syntactical structure of the given input, i.e. whether the given input is in the correct syntax.

Semantic Analyser:

Interpret symbols, their types, and their relations with each other.

Intermediate Code Generation:

In the analysis-synthesis model of a compiler, the front end of a compiler translates a source program into an independent intermediate code.

Code Optimizer:

It transforms the code and tries to improve the code by making it consume fewer resources (i.e. CPU, Memory) and deliver high speed.

Target Code Generation:

Use the optimized intermediate code and generates target code or machine code on which machine can operate easily.

A Java program whenever compiled, the compiler only generates the intermediate code called bytecode and on runtime, it generates machine code. Hence Java program has lesser compile-time and has higher runtime. Let’s have some elaboration on this:

  • High-level language code (i.e.: java code) goes to java compiler.

  • Java compiler follows the lexical analyzer phase, syntax analyzer phase, semantic analyzer phase and then goes the intermediate code generation phase.

  • On Intermediate code generation, phase Java compiler creates intermediate code i.e. bytecode

  • Bytecode is the machine-independent code ready to run on any environment or platform. This is responsible for java portability.

  • We can run Java bytecode on any machine having JDK installed and running, JVM takes up the bytecode and runs it on the machine.

  • We pass bytecode and run the java program. JDK first converts bytecode to machine code than it runs that code on that machine.

On the contrary, the C++ compiler takes the code, follow all the compiler phases and directly generates the machine code ready to run on a machine. Hence:

  • C++ has higher compile-time and

  • Lesser Runtime

Where do we use Java or C++:

Java:

  • Where you are integrating your application software to multiple platforms use java.

  • Use java where you have a large codebase to handle.

  • Where you have a great extension plan for your application software.

C++:

  • Where you need to interact with memory very often.

  • You need to create the OS for your organization.