Friday 26 October 2012

Difference between C++ and Java

This is the 5th post in "Difference between C++ and Java" series.
In this post we will discuss about the array initialization and declaration in two languages.
  1. Array Declaration
Statement
We need to define the size of array in C++ but not in case of Java at the time of array declaration.
When we declare an array in C++, we need to set the size of array in the same statement only.In C++, there is a concept of pointers, therefore we can say that array in C++ is collection of similar data-items(same data-type) stored at contiguous memory locations. But in case of Java, there is no access to pointers or memory addresses, therefore from a programmer's perspective Java arrays are just a collection of similar data-items. Moreover in case of Java, arrays are considered just like objects of any class. Hence, we can initialize them at some later time. Java arrays are just treated as special kind of objects(for simplification,actually they are reference variables explained in later posts), hence we can assign the size at some later stage as shown in the example below.
  • Java
int arr[];
//some other code
arr=new int[2];//size is assigned
//some other code
arr[0]=12;arr[1]=45;//manipulating array values

Similar code in C++ will give a compilation error. In C++, we have only two options, either give the arrays size or intialize the array using curly braces as shown in the example below.

  • C++
int a[4];//assign the array size
//some other code
int b[]={1,2,3};//initialize the array in same line
//some other code
a[3]=12;//manipulate the first array
      Moreover, in Java if we write array size along with object name inside square brackets as we do in C++, then we get a compile time error.
 2.  Default Values in Array 
Statement
Default values in C++ array elements is a garbage value, but in Java all numerical data-elements are initialized to 0 and Objects(reference variables) are initialized to a null value. 
C++ compiler does not take any overhead to initialize the values of array elements by itself. Therefore, initially, a garbage value(unpredictable value) is assigned to each array elements.
Java takes the things differently. In Java, initial value is 0 for integers, 0.0 for double, 0.0f for float, false for boolean and null for objects of the classes.
Following code snippets clarify the same.
  • Java
 int a[]=new int[2];
 for(int i=0;i<a.length;i++)
    System.out.println(a[i]);
 Output for above snippet

0
0
  • C++
int a[4];//assign the array size
for(int i=0;i<4;i++)
{
    cout<<a[i]<<endl;
}
Output for above snippet
4247764
4237328
4261536
163
3. Array Length/Array Size
Statement
In Java , array variable keeps the track of current array size or array length(length attribute) but in C++  we need to consider the change in array size by our variables.   While traversing the array for input or output of the array, often we need to find out the number of loop runs. For finding the number of loop runs we need array size. Programmer needs to keep the track of this value and update this value when we re-initialize array in C++. 
       But in case of Java, as I have already told that array is considered just like an object. In Java 'length' attribute of array variable automatically stores and update the value of array size/ array length. This length variable is always of the int type irrespective of the type of array. Programmer cannot change its value as it is marked final by the JVM. Hence, a lot of programming effort is saved in programs with intense use of arrays. When the array is re-initialized, then the value of the length attribute is automatically changed by the JVM.
4. Jagged Arrays
Statement
We cannot have jagged arrays in C++ but we can have them in Java, hence only the required memory is used. 
 In C++, we have two dimensional arrays. The problem is that we can only have rectangular arrays.
Consider the situation, there is a company with 3 departments and we need to store salaries of the members of these departments. Suppose there are 3,8,2 members respectively in these groups. We can think of storing these values in a two dimensional array with size 8X3. Therefore, we require 8X3=24 integer elements. But we require only 3+8+2=13 elements. Hence we are wasting 24-13=11 elements of memory. C++ does not allow each of the element of a two-D array separately. Java provides Jagged array in such a case. We can have following code snippet for the same example.
  • Java
int salary[][]=new int[3][];
salary[0]=new int[3]; // first department
//store salary of dept 1
 salary[1]=new int[8];// second department
 //store salary of dept 2
 salary[2]=new int[2];// third department
 //store salary of dept 3
We can re-initialize the individual elements, independent of each other.
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.

3 comments:

  1. bhaiya, the post is too much helpful for me. please continue your post in frequent intervals...

    ReplyDelete

Java vs C++

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