C++ and Java
C++ and Java
In this post, you will learn the difference between C++ and Java. There are many similarities and differences
between these programming languages.
Before we see the difference between them, lets look have a look the basic details about both of these
programming languages.
C++ language
C++ is the first programming language that introduced the concept of object oriented programming, it was C++
that introduced the whole idea about objects and classes. C++ initially known as “C with classes” as it was
developed as an advanced version of C, with added object oriented features as an improvement.
C++ language was developed by Bjarne Stroustrup at AT & T Bell Laboratories. The first commercial C++
compiler known as Cfront, was released in 1985.
Java language
Java programming language was developed by James Gosling at Sun Microsystems in early 1990. Java was
initially developed for digital devices such television, remote, set-top boxes.
Java project initially named “Greentalk” by James Gosling, it got renamed to “Oak” later. in 1995 Sun
Microsystems renamed it again from Oak to java as “Oak” was a registered trademark for another organization.
Goto statement C++ supports the goto statement. Java does not support the use of goto statements.
Java does not have any concept of pointer, nor does it support
Pointers C++ supports the use of pointers.
similar functionality.
In C++, you can use both call by value Java only supports call by value and does not support call by
References
and call by reference. reference.
C++ is not portable as it is platform Java is a portable language as java is a platform independent
Portable
dependent language. language.
C++ Program
Let’s have a look at a very simple C++ program that prints a message “Hello World!” on the screen.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Output:
Hello World!
Java Program
Let’s write the same program in Java programming language.
class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
Output:
Hello World!
1) Simple C++ program to add two numbers
In this program we are asking user to input two integer numbers and then we are adding them and displaying the
result on screen.
#include <iostream>
using namespace std;
int main(){
//Declaring two integer variables
int num1, num2;
/* cout displays the string provided in the
* double quotes as it is on the screen
*/
cout<<"Enter first integer number: ";
/* cin is used to capture the user input
* and assign it to the variable.
*/
cin>>num1;
int main(){
int num1, num2, x;
float num3, num4, y;
cout<<"Enter two integer numbers: ";
cin>>num1>>num2;
//This will call the first function
cout<<"Result: "<<sum(num1, num2)<< endl;