Both C++ and Java are great object-oriented programming languages that have broad utility in the computer world, however the two languages bare several differences.
Portability
- Java was initially developed to help developers build cross-platform software with the same code and hence it was designed to run on top of a Java virtual machine as opposed to C++ which is compiled directly into machine executed code.
- Java by the fact that runs on top of a Java virtual machine that has to interpret the language to the machine before it gets executed by the computer results in it being a tad slower than C++. In instances where large programs involving large computations are required such as scientific simulations, the difference in speed would easily be noticeable.
Threading
- Java has an inbuilt capability for multi-threading meaning it can take advantage of a multi-core computer by using different CPUs to process different threads. C++, on the other hand, does not have such a functionality readily inbuilt and has to take utilize external libraries to achieve it.
Java example:
class tex implements Runnable{
public void run (){
System.out.prinln("Thread is running");
}
public static void main (String args[]){
tex mobj=new tex();
Thread mthread=new Thread(mobj);
M
thread.start();
}
}
C++ example:
#include <thread>
#include<iostream>
#include<string>
using namespace std;
void task(string str){
cout <<" task is done: "<<str;
}
int main(){
Thread t(task,"completed");
t.join();
}
Relationship
- In Java, all functions must be inside a class for it to be considered valid since Java only supports object-oriented programming, but C++ supports free flowing functions where some functions may lie outside of a class since it supports both object-oriented programming and procedural programming.
- Java has a strict relationship with its source code which C++ does not enforce. Since all functions must lie inside a class in java it requires that all source code inside a particular class be placed inside a file with a name identical to the name of the class, all the source code for class Foo must be inside Foo.java
Input-Output
- C++ utilizes in and out the syntax to perform input out operations, for example, to get information from users especially in a console application c++ uses cin >>x. For example, to get a number from a user and store it into a variable x and cout << data to be displayed << endl; to get information out to the user taking care to reverse the signs.
- Java, on the other hand, uses a more complex mechanism which reads one byte at a time using System.in to get information from a user as a way of interaction and System.out.println(X); where X is a variable, to display information back to the user usually after some processing has been done.
Memory Management
- Java is also controlled having features such as memory management restricted and access left to the system while C++ gives programmers access to manage memory utility of his or program. Though the addition control at times is beneficial, it also intensifies the complexity of developing in c++ by having programmers be responsible for memory management.
- C++ supports pointers which are merely variables that store the actual address of another variable in memory. Pointers are denoted by an asterisk preceding the variable name, and like any other variable it must be declared an example of a pointer declaration is
Int *name of variable // pointer to an integer
Float *name of variable //pointer to float
In the actual sense, the data types stored by pointer variables are all similar, they are all long hexadecimal addresses to another variable’s location in memory obtained by preceding the ampersand (&). Java does not support pointers since it also offers little access to memory management.
Error Handling
- In Java, functions must deal with exceptions in cases where the function might declare that it may through a function. This is usually achieved by using try-catch statements where the function would be located inside the try block and any exception thrown dealt with by the catch block an example being
Try {
Code to be run
} catch (Exception name variable with information about the exception) {
Handle the exception for example by display an error message
}
- C++ though it also has the try-catch exception handling, it lacks a final block which always gets executed regardless of the presence of an exception or not in java the try catch finally block would look like:
Try {
//Code to be run
} catch (Exception name variable with information about the exception) {
//Handle the exception for example by display an error message
} finally {
//code here is always executed
}
LIBRARIES
- Java uses import statements to be able to use other external code references, with this statement java can also import multiple classes such as Import java.util . * something which C++ cannot perform. C++ uses #include statements to be able to get access to other external files. The c++ include is also different from the java import in that; the #Include statement increases code size.