Comparison of C++ and Java in Implementing Introductory Programming Algorithms

Computer programming is the heart of computer science and C++ and Java are the most popular and widely used languages. The performance of C++ and Java programming languages has been benchmarked using robotics, numerical, genetic and evolutionary algorithms, but not using introductory programming algorithms. This article examined C++ and Java by analysing the implementation of novice algorithms of introductory programming courses by evaluating the difficulty, effort, time and delivered bugs of programs with Halstead software metrics. The study identified that Java is more difficult than C++ in the implementation of selected algorithms. Similarly, the efforts required to translate the novice algorithms in Java are higher than C++. The time involved in translating selected algorithms in C++ is lower than Java. Similarly, the number of delivered bugs in C++ is lower than Java. The study suggests that C++ is more suitable than Java for the implementation of introductory programming algorithms and reasonably more suitable for introductory programming courses. The study emboldens the programming linguists to do further analyses in contemplating other programming languages with Halstead software metrics and other kinds of algorithms.


Introduction
T echnology pervades nearly all areas of society [1]. In the new global environment, the information technology is becoming widespread and seems inevitable. Information technology has successfully integrated in different areas including healthcare [2], education [3], defence [4], marketing [5], and sports [6]. The computer is an essential element of information technology and software is a main part of any digital computer. Software is developed through a formal system called programming languages. Programming is the heart of computer science [7] and a carrier awarding discipline [8]. The job opportunities of programmers are estimated to grow 8% from 2012 to 2022 [9]. Similarly, according to Dice [10], the opportunities for software developers will increase by 2026. The rapid increase in job opportunities of programmers naturally amplifies the challenges in the design of programming languages. Notable achievements have been made and a plethora of programming languages have been developed. Thousands of programming languages have been developed, but these languages do not endure forever. Indeed, many powerful languages become obsolete some time. Only few programming languages such as C++ and Java are surviving and are popular due to their dynamic features and rapid improvement.
C++ is a general-purpose and high-level language developed by Bjarne Stroustrup. It was designed to provide flexibility and efficiency of C language with program organization facilities of Simula [11]. C++ is a compiler-based language, so it translates the source program into a machine native instruction through a compiler [12], and uses a linker to attach the precompiled libraries with generated program of a compiler. Categorically, C++ is an imperative language and treats the program as a collection of statements that change the state of a program. Apart from imperative nature, C++ is a procedural language and thereby supports the notion of procedures and subroutines. It also supports structured programming and is statically typed with smart support of object-oriented programming and generic programming. C++ has an extensive support to manipulate memory at low level. Pre-processors are also available in C++ [13] as a textsubstitution mechanism.
Java is a successful and powerful programming language developed by James Gosling [14]. Java is strongly and statically typed [15]. Its syntax is inspired from C++ and it is a compiled and interpreted language. Platform independence and security are its unique features. Java is extensively used in big data applications [16] and provides a strong solution to develop robust and efficient object-oriented software [17]. Pre-processors and linkers are not available in Java, and packages are loaded or imported dynamically at runtime. Java does not support operator overloading. C++ and Java are not only used in commercial applications, but also in the pedagogy of introductory programming. In the introductory course on programming (CS1), the conventional algorithms of programming are offered to learners with suitable programming languages. It has been widely observed that learning to program is challenging and hard for beginners [18][19], and normally manifested in the form of high dropout and failure [20].
The intrinsic complexity of introductory programming courses naturally challenge the researchers and consequently a choice of programming language for introductory programming courses has been an area of study for decades [21]. Still there is no consensus on the choice of programming language for an introductory programming course. Many instructors agree to use C++ or Java in CS1; whereas, several opposed their induction in introductory courses on programming, for instance, Agarwal and Agarwal [22] has identified several issues of teaching C++ in CS1, and similarly Nasrawt and Lam has mentioned the drawbacks of using Java in introductory programming [23].
The selection of appropriate language for introductory programming courses is usually made on an empirical basis by experimenting on the groups of learners. Invention of programming languages initiated the best and ongoing research on the pros and cons of programming languages through the comparative evaluation. As a matter of fact, the comparative evaluation of programming languages has gained some attention from early on. Most studies devoted to analyse the runtime performance, integrity, compiled size, reliability, memory utilization [24] and vulnerabilities, yet very less focus has been concentrated on evaluation of the implementation of introductory programming algorithms in topical programming languages. The prospective evaluation is essentially important both from the technical and pedagogical aspects.
The central objective of present study is to com-pare C++ and Java by analysing the implementation of introductory programming algorithms. The novelty of this work lies in two affirmations: i) the use of Halstead complexity metrics in the comparison of programming languages, and ii) the endorsement of suitable language for introductory programming courses through quantitative analysis rather than the empirical examination. To the best of our knowledge, no study of such pattern has yet been conducted. The proposed study would provide a quantitative knowledge about the suitability of C++ and Java in introductory programming courses along with numerous metrics. The organization of this article is as follows. Section 2 discusses the related work on comparison of C++ and Java. Section 3 describes the research method and results. Section 4 presents the discussion followed by a conclusion.

Related Work
There have been many studies on comparative analysis of C++ and Java. Phipps [25] conducted a study to analyse C++ and Java in terms of defect rates and productivity. Personal Software Process, which is self-improvement process [26], is used to collect data from development projects. The study identified no difference between C++ and Java while analysing the defects against development time.
In last many decades, the evolutionary algorithms have shown to be a useful solution for optimization problems [27]. Merelo-Guervo et. al. [28] analysed evolutionary algorithms over OneMax function, bitflip mutation and crossover, and evaluated the performance of interpreted and compiled languages including C, C++, Java and Python. The study reported Java as fastest, but pointed that interpreter-based languages should not be much slower than compiled languages.
Robots' applications are the complex class of software. Compiler-based languages like C and C++ are normally considered more suitable to implement robotics software. Gherardi et. al. [29] analysed the runtime performance of Java and C++ in the implementation of robotics algorithms. The study reported that on Windows platform, Java is around 1.09 to 1.51 times slower than C++ and around 1.21 to 1.91 times slower on Linux platform.
elPrep is a framework for processing Binary Alignment in sequencing pipelines. Memory management is a major challenge in elPrep. Costanza et. al. [30] implemented nontrivial subset of elPrep in C++, Java and Go, and analysed their memory utilisation and execution performance. The descriptive results reported that C++ and Java are not reasonable to implement elPrep.
Generic programming is a pivotal element that supports the development of reusable components in software construction [31]. Saiedian and Hill [32] analysed the support of generic programming in C++ and Java. Qualitative as well as some quantitative attributes including performance, memory usage at runtime and size are analysed in the study. The evaluation described that support of generic programming in C++ is higher and better than the one provided by Java Development Kit. Likewise, in another study [33], it is opined that C++ has an extra expressive strength of generic programming; whereas, generic in Java is a syntactic sugar and merely promotes casting from coding level to the bytecode level.
Multiple inheritance is an essential feature of object-oriented programming in which a class inherits the features from single or multiple parents. Albalooshi and Mahmood [34] studied the implementation of multiple inheritance in C++, Java and Python and analysed the complexity and reusability with Chidamber and Kemerer metrics. The study suggested that C++ and Python support better reusability with multiple inheritance; whereas, Java has poor structure of objects in the implementation of multiple inheritance.
Fourment and Gillings [35] benchmarked the performance of programming languages through the analysis of implementing the Bioinformatics algorithms in different languages including C++ and Java. The study analysed the execution speed and memory utilisation by evaluating the algorithms of parsing BLAST file results, Neighbor-Joining tree construction algorithm and Sellers algorithms. The study concluded that C++ is faster and uses less memory than Java in the implementation of Bioinformatics algorithms.
TIOBE Company analysed the popularity of programming languages by maintaining a TIOBE programming community index and update the index once a month [36]. The index is based on the courses, third-party vendors and software engineers worldwide. Popular search engines including Google, Amazon, YouTube, Bing, Wikipedia and Yahoo are used to calculate the programming index.
A comprehensive popularity index of programming languages over different years provided by TIOBE is shown in Figure 1. According to TIOBE index, the popularity of Java remains higher than C++, yet a variation in popularity of both languages is observed in different span of time. PYPL (Popularity of Programming Language) is another forum for ranking topical programming languages. It is based on Google trends and like TIOBE it also provides a comprehensive pop- In present study, a pragmatic research methodology is defined and followed during the accomplishment of work (Figure 3).

Design & Methods
The study virtually aims to compare C++ and Java by evaluating the implementation of introductory programming algorithms with software metrics. During the study, two hundred algorithms are selected from different books and online sources. The threshold concepts covered by the selected algorithms are shown in Table 1. For selected algorithms, the source code of C++ and Java are developed with high-level code generator [7]. The current study selected those algorithms which are covered in CS1. The introductory programming courses conventionally used imperative and object-oriented programming language and therefore C++ and Java are selected for the present study. Python is a popular language and widely used in medical imaging [39], Solar Photovoltaic Systems [40] and text analysis [41]. Categorically, Python is a multiparadigm language and its pedagogical significance in introductory programming courses is not very significant. Alzahrani et. al. [42] compared Python with C++ and identified that learning of Python required Conventional problems 35 4 Searching algorithms 8 5 Sorting algorithms 20 6 String processing 9 7 Stack & Queue 7 8 Linked list 11 9 Matrix processing 15 10 Tree 10 11 Heap 4  12 Graphs 13 13 Greedy algorithms 5 14 Dynamic programming 4 15 Numerical computation 23  [43] demonstrated that single Python is inadequate as an introductory programming language. The present study analysed the implementation of those algorithms which are offered in introductory programming. Since Python is not suitable for introductory programming, it is not considered for the current study.
The collected programs, formalized as a programming corpus, are analysed with Halstead complexity metrics (HCM) which is a useful way to analyse the complexity of code [44]. HCM was specially developed to quantify program complexity from source code by using the detail of operators and operands as an elementary information. The elementary information of operators and operands are based on following four variables. η 1 = number of distinct operators. η 2 = number of distinct operands. N 1 = total number of occurrences of operators. N 2 = total number of occurrences of operands. The Halstead complexity metrics use the elementary information of operands and operators and relate them with simple mathematical equations to calculate the program difficulty, efforts, times and delivered bugs. The difficulty measure is associated to the difficulty of the program to write or understand. The formula for calculating difficulty is given as follows.
The effort is defined as a measure of the mental activity required to reduce a preconceived algorithm to a program. It can be viewed as an elementary mental discrimination required to generate a program. The formula for calculating effort is given as follows.
The time (in seconds) required to translate the algorithm into implementation in the specified program language is calculated as follows.
The delivered bugs represent an estimate for the number of errors in the implementation of algorithm. The formula for calculating delivered bugs is as follows.
During the study, the programming corpus is analysed with Halstead Metric tool which is a software metrics analyser for several programming languages and freely available on SourceForge 1 . The programs are virtually analysed in two stages. In the first stage, the basic attributes are identified which are shown in Table 2 Table 5. The Mann-Whitney U tests conducted on calculated measures show that in terms of volume, efforts, time and bugs, the differences between C++ and Java are statistically significant, however, only in the case of difficulty, the difference between C++ and Java is not statistically significant, yet the difficulty level of C++ is lower than Java.

Discussion
The study reported in this article comparatively evaluate C++ and Java by using Halstead complexity metrics. The implementation of 200 algorithms in C++ and Java is analysed. In the programming corpus of C++, 14446 operators are identified, whereas 17103 operators are recognized in the corpus of Java for same algorithms. The difference defined that Java required 18.93% more operators than C++. Similarly, in Java 9.53% unique operators are more involved than in C++. The average program length of C++ corpus is 107.81, whereas 121.22 in Java. This described that difference between both languages in terms of program length is 11.71% and evidently C++ is much finer than Java in terms of program length. The same behaviour is observed in estimated program length, in which C++ is found better than Java with the difference of 12.21%. The mean value of volume is 579.03 and 665.17 for C++ and Java respectively, which identify the difference of 18.85%. The Mann-Whitney U test applied to the volume described that the difference between C++ and Java is statistically significant. This signifies that C++ is more suitable than Java in terms of size of the implementation of the algorithms. The mean score for the difficulty of implementing selected algorithms in C++ is 17.79, whereas 17.85 in Java. The Mann-Whitney U test conducted on calculated difficulty described that difference between C++ and Java is not statistically significant, yet the implementation of algorithms in Java is more complex than C++.
The average score of efforts required to translate the algorithms into C++ is 12099. 16 16 in Java. This shows that the difference between both languages in terms of efforts is 11.69%. The Mann-Whitney U test conducted on the calculated efforts shows that the difference between C++ and Java is statistically significant. Therefore, as compare to Java, less elementary mental discrimination is required in C++ to generate the programs for conventional algorithms. The mean score of time involved to implement the selected algorithms in C++ is 672.18, whereas 755.68 in Java. Therefore, the time difference between both C++ and Java is 11.70%. The Mann-Whitney U test applied on the calculated time showed that the difference between C++ and Java is statistically significant. Therefore, C++ is found to be more efficient than Java in terms of time required to implement the algorithms.
The average score of the delivered bugs in corpus of C++ is 0.16, whereas 0.18 in Java. Therefore, the difference is 11.76%. The Mann-Whitney U test conducted on delivered bugs described that difference between C++ and Java is statistically significant. Hence, C++ is relatively better than Java on the subject of delivered bugs.
All the tests lead to a conclusion that C++ is better than Java in implementing the introductory programming algorithms. The results obtained in the study are further compared with the findings of other studies and are shown in Table 6. The detail included in Table 6 clearly delineate that the existing studies on C++ and Java do zilch in comparing and examining the difficulty, time and efforts required in the implementation of any sort of algorithms. The purview and justification for the accomplishment of this work lies in the formation of new knowledge and a novel pattern of study that has quantitatively analysed C++ and Java in different dimension by using Halstead complexity for determining the difficulty, time, efforts and bugs involved in the realization of introductory programming algorithms.

Conclusion
Programming is a core element of computer science and comparison of programming languages is an active area of programming languages. The popular languages like C++ and Java are being compared in assorted dimensions; however, no notable study has compared these languages in implementing the fundamental algorithms being offered in introductory programming courses. The selection of an appropriate programming language for introductory programming is conventionally decided on empirical corroborations. This study used Halstead complexity metrics and quantitatively analysed the C++ and Java by analysing the implementation of 200 conventional computer algorithms of introductory programming courses. The results are statistically analysed which showed that for the selected algorithms, the performance of C++ is much higher than that of Java in that it entails less difficulty to write and understand the programs. Similarly, C++ involves less mental discrimination in the generation of programs. Likewise, C++ required less time to translate and debug the programs and thereby is more suitable for introductory programming courses. This study presents a new tool for comparing and selecting felicitous language for introductory programming courses.
As future research, new algorithms will be added for more investigation and other techniques like MOOD metrics and Chidamber and Kemerer metrics for more detailed analysis.

S. No Study
Methods Results

1
Phipps [19] Defect rates in C++ and Java are compared C++ and Java have no difference in defect rates 2 Merelo-Guervo s et al. [22] Analyzed the implementation of evolutionary algorithms in compiled and interpreted programming languages Java is top-ranked 3 Gherardi et. al. [23] Performance of C++ and Java is analysed through the implementation of robotics algorithms C++ is faster than Java 4 Costanza et. al. [24] C++, Java and Go are analysed in the implementation of elPrep multithreaded framework.
C++ and Java are less reasonable in term of memory utilization 5 Saiedian and Hill [26] Support of generic programming in C++ is compared with Java by analyzing the size, performance and memory usage C++ is better than Java in the support of generic programming 6 Albalooshi and Mahmood [28] Multiple inheritance in C++, Java and Python is analyzed with Chidamber and Kemerer metrics C++ and Python provide better multiple inheritance over Java 7 Fourment and Gillings [29] Performance of C++ and Java is evaluated through the implementation of bioinformatics algorithms C++ is faster and use less memory than Java 8 TIOBE Index [30] Programming languages, including C++ and Java rank on popularity scale Java is top-ranked, whereas C++ is on fourth rank 9 PYPL [31] Programming languages including C++ and Java are ranked on popularity scale Java is ranked at second, whereas C++ is on the sixth rank 10 IEEE Spectrum [32] Rank the programming languages by using different metrics and data sources The second position is assigned to Java and fourth to C++ 11 Present Study C++ and Java are comparatively evaluated by analyzing the volume, difficult, effort, time and bugs. C++ is better than Java in implementing computer algorithms