CS Electrical And Electronics
@cselectricalandelectronics
All PostsCPPDifference BetweenJavaProgramming

Difference Between Java And Cpp Programming Languages, Applications

Hello guys, welcome back to my blog. In this article, I will discuss the difference between Java and Cpp programming languages, applications, simple code on java language, simple code on Cpp language.

If you have any doubts related to electrical, electronics, and computer science, then ask question. You can also catch me @ Instagram – Chetan Shidling. 

Also, read:

Difference Between Java And Cpp Programming Languages

There are so many differences and similarities between the JAVA and C++ language 

Let’s have  look on differences between JAVA and C++:

Design Goal

  • C++ had been designed for applications and systems programming. This is  a continuation of the C language
  • Whereas Java had been created as an interpreter to print systems but afterwards extended as a support network computing.

Purpose

  • Mainly C++ is used for system programming.  
  • Whereas Java can be used for application programming. It is widely used in web-based, windows-based, enterprise, and mobile applications.

Platform-independent

  • C++ depends on the platform, i.e you cannot run C++ programs on different platforms.
  • Whereas Java is platform-independent, and it’s WORA, you can run java programs irrespective of platforms.

Multiple inheritance

  • C++ has multiple inheritance. 
  • Java doesn’t support multiple inheritance through class but this can be done by using interfaces in java.

Pointers

  • C++ has external pointers i.e You can write a pointer program in C++.
  • Java has internal pointers. But , you can’t write the programs using pointers in java. It means java restricts the pointers in java.

Operator Overloading

  • C++ supports operator overloading.
  • Whereas Java doesn’t support operator overloading.

Goto

  • C++ supports the goto statement.
  • Java doesn’t support the goto statement.

Compiler and Interpreter

  • C++ is a compiler based language, which is used to convert source code to machine code so, C++ is platform dependent.
  • Java uses both compiler as well as interpreter. In Java source code will be converted to bytecode during compilation. The interpreter executes this bytecode during runtime and it produces output. JAVA is platform-independent because it’s interpreted.

Structure and Union

  • In C++ structures and union concepts are there.
  • In Java we don’t have structures and unions.

Call by Value and Call by reference

  • In C++, we have call by value and also call by reference.
  • Java supports only call by value. There is no call by reference in Java, because there are no pointers.

Thread Support

  • C++ doesn’t  support threads. It relies on third-party libraries for the support of thread.
  • Java has built-in thread support.

Hardware

  • C++ is very much nearer to hardware.
  • Java is not interactive with hardware.

Object-oriented

  • C++ is an object-oriented language. But in the C language, a single root hierarchy is not possible.
  • Java is also an object-oriented language. Everything in Java is an object except fundamental types. It is a single root hierarchy since everything gets derived from java.lang.Object.

Documentation comment

  • In C++ we don’t have documentation comments.
  • In Java we have documentation comment (/** … */) to create documentation for java source code.

Note

  • Java will not  support default arguments like C++.
  • Java will not support header files like C++.  We can use import keywords to include different classes and methods.

C++ Program Example

File:first.cpp 
 
 #include <iostream>   
 using namespace std;   
 int main()   
 {   
   cout << "Hi!! C++ Programming";   
   return 0;   
 }   

 Output:  
 Hi!! C++ Programming  

Java Program Example

File:First.java

 classFirst{   
   public static void main(String args[]){   
    System.out.println("Hello Java");   
   }   
 }   

 Output:  
 Hello Java  

Hello World Example

Now let’s see how to write a simple program of Java. We are going to write a simple hello Java program easily after installing the JDK into our system.

If you want to create a simple Java program, first you need to create a class which contains the main method. Let’s see the requirements first.

To execute the Java program, the following software must be properly installed in your system.

  • First Install the JDK if your system doesn’t have it. Download from here.
  • Then set the path of the jdk or bin directory. 
  • Then write the Java program
  • Now compile and run your Java program

Hello World Example

Now let’s create the hello world program:

 class HelloWorld{   
   public static void main(String args[]){   
    System.out.println("Hello Java");   
   }   
 }   
 Output- Hello Java  

Note- You have to save the above file as HelloWorld.java

To compile- javac HelloWorld.java

To run- java HelloWorld

Parameters used in Hello world

Let’s have a look at the meaning of public, class,void, static, main, String[], System.out.println().

  • class- This is a keyword in Java which can be used to declare a class in Java.
  • public– This is a keyword in Java which can be called an access modifier that represents visibility of the data members, methods. public means It means it is visible to all.
  • static- This  is a keyword. If we declare any method or data member  as static, then it is known as the static method. The main advantage of this static method is, there is no need to create one more object to invoke the static method. The main() method will be executed by the JVM, so it doesn’t require creating an object to invoke the main() method. So, it saves memory too.
  • void– This is the return type of the method which  doesn’t return any value.
  • main-This indicates the starting point of the program.
  • String[] args or String args[]- It is used for command line arguments (we discuss in upcoming sessions).
  • System.out.println()– This is just like printf statement in C which is used to print statements. Here, System is a class, out is the object which comes under PrintStream class, and println() is a method of the PrintStream class. We will discuss the internal working of System.out.println(),  statement in the upcoming section.

In how many ways can we write a Java program?

There are lot many ways to write a Java program. The modifications which can be done in a Java program are listed below:

01. First one is by changing the sequence/order of the modifiers

Let’s have a look at the simple code of the main method.

 static public void main(String args[])   

02. The subscript/square braces[], can be used after type, before the variable or after the variable in Java.

Let’s see the various codes to write the main method.

 public static void main(String[] args)   
 public static void main(String args[])   
 public static void main(String []args)   

By doing these changes the compiler doesn’t throw an error.

03. You can also provide var-args support to the main() method just by passing 3 dots(ellipses), as shown below.

 public static void main(String... args)   

04. Semicolon is optional in Java at the end of class.

Let’s see the simple code.

 class A{   
 static public void main(String... args){   
 System.out.println("hello java");   
 }   
 };   

Valid Java main() method signature

 public static void main(String[] args)   
 public static void main(String args[])   
 public static void main(String []args)   
 public static void main(String... args)   
 static public void main(String[] args)   
 public static final void main(String[] args)   
 final public static void main(String[] args)   
 final strictfp public static void main(String[] args)   

Invalid Java main() method signature

 public void main(String[] args)   
 static void main(String[] args)   
 public void static main(String[] args)   
 abstract public static void main(String[] args)   

I hope this article ” Difference Between Java And Cpp Programming Languages ” may help you all a lot. Thank you for reading.

Also, read:

Author Profile

Shilpa Annigeri
Ships annigeri

Department of Electrical and Electronics engineering, B.L.D.E.A's V.P.Dr.P.G.Halalatti college of engineering and technology Vijayapur.
Share Now

Shilpa Annigeri

Ships annigeri Department of Electrical and Electronics engineering, B.L.D.E.A's V.P.Dr.P.G.Halalatti college of engineering and technology Vijayapur.