0% found this document useful (0 votes)
4 views

C++ and Java

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C++ and Java

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ vs Java – Difference between 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.

Read more at: History of Java.


C++ vs Java
DESCRIPTION C++ JAVA

Java supports both compiler and interpreter. Java Compiler


C++ only supports compiler. C++
converts the java code (source code) to the bytecode. Java
Compiler & program is compiled and run by the
interpreter JVM executes this bytecode at runtime and produces
Interpreter compiler, which converts the source
output. This compilation and execution can be done on different
code into machine code.
machines.

Platform- Java is platform-independent. As the bytecode can be run on any


C++ is platform dependent.
independent machine by JVM.
DESCRIPTION C++ JAVA

C++ was designed as an upgraded


Java was initially developed for Digital devices, later it was widely
Designed For version of C, with advanced features
accepted as an internet programming language.
such Object oriented features.

C++ is mainly used for developing


Used for Java is mainly used for application programming.
system programming.

Goto statement C++ supports the goto statement. Java does not support the use of goto statements.

Multiple Java does not support multiple inheritance. The same


C++ supports multiple inheritance.
inheritance functionality can be achieved by using interfaces in java.
DESCRIPTION C++ JAVA

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++ does not have its own concept of


Multithreading Java supports multithreading.
multithreading.

Operator C++ supports the concept of operator


Java doesn’t support operator overloading.
overloading overloading.
DESCRIPTION C++ JAVA

Structure C++ supports structures. Java does not support structures.

Unions C++ supports unions. Java does not support unions.

In C++, you cannot handle runtime


Runtime error In Java, you can catch and handle runtime errors.
errors.

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;

cout<<"Enter second integer number: ";


cin>>num2;
cout<<"Sum of entered numbers is: "<<(num1+num2);
return 0;
}
Output:

Enter first integer number: 10


Enter second integer number: 20
Sum of entered numbers is: 30

2) C++ program to add two numbers using function overloading


In this example, we will see how to add two numbers using function overloading. Function overloading is a
feature that allows us to have more than one function having same name but different number, type of sequence
of arguments. Here we are defining three functions for the same purpose addition, based on the data type of
arguments different function is called.

/* Function overloading example. Where we can have


* more than one functions with same name but different
* number, type or sequence of arguments
*/
#include <iostream>
using namespace std;

int sum(int, int);


float sum(float, float);
float sum(int, float);

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;

cout<<"Enter two float numbers: ";


cin>>num3>>num4;
//This will call the second function
cout<<"Result: " <<sum(num3, num4)<< endl;

cout<<"Enter one int and one float number: ";


cin>>x>>y;
//This will call the third function
cout<<"Result: " <<sum(x, y)<< endl;
return 0;
}
int sum(int a, int b){
return a+b;
}
float sum(float a, float b){
return a+b;
}
/* Remember that sum of int and float is float
* so the return type of this function is float
*/
float sum(int a, float b){
return a+b;
}
Output:

Enter two integer numbers: 21 88


Result: 109
Enter two float numbers: 10.2 30.7
Result: 40.9
Enter one int and one float number: 20 16.4
Result: 36.4

3) Addition using Class and Function


#include <iostream>
using namespace std;
class Add{
public:
/* Two variables that we are going to
* add. If you want to add float or double
* variables instead, just change the data
* type. for example: float num1, num2;
*/
int num1, num2;

/* This function ask the user for two numbers.


* The numbers that user enter are stored into
* num1 and num2 variables so that we can add
* them later.
*/
void ask(){
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}

/* This function adds the numbers that are passed


* to it through arguments. I have used parameter names
* as n1 and n2 but you can choose any parameter name.
* This function returns the result.
*/
int sum(int n1, int n2){
return n1+n2;
}

//This function displays the addition result


void show(){
cout<<sum(num1, num2);
}
};
int main(){
//Creating object of class Add
Add obj;

//asking for input


obj.ask();

//Displaying the output


obj.show();
return 0;
}
Output:
Enter first number: 21
Enter second number: 19
40

You might also like