Tuesday 16 October 2012

Difference between C++ and Java

I think almost every Computer Science Engineer wants to learn programming languages. Most basic high level languages which freshers try to learn are C++ and Java. There are a lot of chances of mixing up of syntax and implementation of a concept in these languages because each of them have their own features. Here is a humble effort from my side to carve out the basic differences in implementation of basic concepts and features of both the languages.
I will be describing few of the differences in my every post. Any suggestions and queries are also invited.
Here is the dose for the day.

  1. Use of % (modulo) operator
As we all know that % is a modulo operator and it is used for getting the remainder after division of its operands. For example, 234%2 is equal to 0 and 233%2 evaluates to 1.
Statement
% can be used only for integers in C++ but in Java it can also be used for floating point numbers.

  • C++
Following code attempts to make use of % for float operands

int  a=65,b=7;
float f=7.7;
cout<<(a%b)<<endl;
cout<<(f%b)<<endl;//error
This code gives error and last line does not compiles.

  • Java
The same code in Java method is as follows
int  a=55,b=7;
float f=54.5f;
System.out.println(a%b);
System.out.println(f%b);
Output is
6
5.5
Thanks all for your time. We will continue with 'Difference between C++ and Java' series in next post. Any queries and suggestions are invited in comments and through e-mails.

No comments:

Post a Comment

Java vs C++

THis is a basic blog that differentiates features of two of the widely used languages C++ and Java.