Java, Python and Javascript, a Comparison
Total Page:16
File Type:pdf, Size:1020Kb
Independent project, 15 credits, for the degree of Bachelor of Science with a major in Computer Science Spring 2019 Faculty of Natural Sciences Java, Python and Javascript, a comparison Rasmus Horntvedt and Tobias Åkesson Author Rasmus Horntvedt, Tobias Åkesson Title Java, Python and Javascript, a comparison Supervisor Kamilla Klonowska Examiner Daniel Einarson Abstract With the amount of programming languages currently available there is a high risk of confusion and doubtfulness in aspiring programmers of which to choose. It may be motivating for a beginner to choose “the perfect language” when starting, to avoid learning multiple languages. This thesis compares three popular languages on three separate aspects, their syntax, usefulness in different areas, and performance in terms of speed. Syntax wise the results varied with some aspects being very similar across all three languages to completely different in others. In terms of usefulness in specific areas the languages flexibility allowed them to develop applications in most fields, while being dominant in different areas. The speed comparison resulted in python being the slowest across all tests, with Java and Javascript (running inside Nodejs) competing for first place. Keywords Python, Java, Javascript, Comparison, Benchmark, Performance, Syntax, Programming language. 1 Content 1. Introduction 4 1.1 Purpose 4 1.2 Research questions 5 1.3 Limitations 5 1.4 Structure RQ1 6 1.5 Structure RQ2 6 1.6 Background 7 2. Method 8 2.1 Ethical acknowledgements 8 2.2 Research question 1 8 2.3 Research question 2 8 2.4 Research question 3 9 2.4.1 Testing system and setup 10 3. Literature review 11 3.1 Python, Java and Javascript - Overview 11 3.1.1 Python 11 3.1.2 Java 12 3.1.3 Javascript 12 3.2 Related work 13 4. Result 15 4.1 The differences between the languages (RQ1) 15 4.1.1 Variables 15 4.1.2 Comments 16 4.1.3 Separators 16 4.1.4 Indentations or curly brackets 16 4.1.5 Selection statements 17 4.1.6 Loops 17 4.1.7 Data structures 18 4.1.7.1 Array 18 4.1.7.2 Array Lists 19 4.1.7.3 List 19 4.1.7.4 Sets 19 4.1.7.5 Tuples 20 4.1.7.6 Stacks and queues 20 4.1.7.7 Dictionaries 21 4.1.8 Classes 21 4.1.9 Functions and Methods 22 4.1.10 Error handling 22 4.1.11 File management 23 4.1.12 Operators 24 4.2 What can the languages be used for? (RQ2) 24 4.2.1 Python usage areas 24 4.2.1.1 Python machine learning 25 2 4.2.1.2 Python automation 25 4.2.1.3 Python web development 26 4.2.2 Java usage areas 26 4.2.2.1 Java Android development 27 4.2.2.2 Java full stack development 27 4.2.3 Javascript usage areas 28 4.2.3.1 Javascript full stack development 28 4.2.3.2 Javascript mobile development 29 4.2.3.3 Javascript Desktop application development 30 4.2.4 Comparison on different usage areas between the languages 31 4.3 Which language is solving tasks the fastest? (RQ3) 34 4.3.1 Quicksort 34 4.3.2 Fizzbuzz game 35 4.3.3 Reading from file 36 5. Discussion 37 5.1 Discussion of research question 1 37 5.2 Discussion of research question 2 38 5.3 Discussion of research question 3 39 6. Future work 40 7. Conclusion 40 8. References 41 9. Appendix 54 9.1 A Syntax 54 9.2 B Benchmarking algorithms 61 3 1. Introduction As a new programmer in the ever growing computer world it is difficult to pinpoint exactly what one needs out of a programming language. With a long catalog of languages it is difficult to make a decision of what programming language is the best, since there is no definitive answer to what “the best” is. The best could be in terms of third party support, ease of understanding, speed or functions etc. Therefore it is impossible to tell what the best is. However, it is possible to explain why one language would be preferred over the other in certain situations. It is still important to note that generalization and misinformation can give the impression of big differences in terms of any of the previously mentioned factors and can therefore skew the decision of what programming language to use. There are several popular programming languages, but the three chosen for this report is as follows: Java, Python and Javascript. Java is one of the most prominent and successful languages in the world [1]. Python has been around since the late 1980’s [2] but has in the last few years been steadily rising in popularity and is now competing with some of the most popular programming languages such as C and C++ [1]. Javascript, although the least popular language out of the three chosen [1], is still relevant thanks to the support it gets from different frameworks that it can implement, and developers who continue to add features to it like for example with Nodejs [3]. Tiobe was used as a way to measure the popularity of the languages. It should be noted that Tiobe uses search index and so it is not a clear correlation between popularity and the statistic being presented on the site [1]. The three languages are fundamentally different but allows us in a nuanced way to compare different aspects of the different languages with each other, and how much weight one should put on a specific aspect of a language. 1.1 Purpose The purpose of this report is to analyze and compare these popular programming languages in three different ways. It is not meant to declare that one language is superior to the rest, but rather analyze desirable traits and strengths in all of them. The reasons for choosing this topic was to understand how different programming languages work and if it is easy to convert knowledge between languages. Considering we have mainly focused on Java during our studies, it was in our interest to develop an understanding of different languages and make a comparison between them. The thesis is primarily targeted towards novice coders, and programmers that has established some knowledge within at least one programming language and is interested in how other programming languages work. It requires some knowledge to understand every component of programming language since they are not explicitly explained in the thesis. It can also be hard to understand the 4 terminology used if the reader is a complete beginner. The report is clearly divided into three separate parts, each focusing on different aspects of the programming languages. The first part of the report centers around the differences between the chosen languages in terms of how easy they are to understand and use for development. This part also involves mentioning similarities. The second part of the report is about finding each language best use case and when it is preferred to be used. This can be interesting to know since some of the chosen languages are considered multi-purpose languages. The last part of the report is about comparing the speed and run time of each language and how they handle different computing tasks. 1.2 Research questions Research question 1: What is the differences between Java, Python and Javascript in terms of syntax and ease of understanding? Research question 2: In what situations is it preferable to use one of the languages over the others? Research question 3: Which of the three languages are solving tasks the fastest? 1.3 Limitations In order to not exceed the time limit, the authors needed to set some limitations. It would be unreasonable to try and compare all the languages and it was therefore necessary to narrow it down to only comparing three languages. It is important to note that there are languages which are more popular than Java, Javascript and Python, such as C and C++ [1]. The reason as to why these languages were not chosen was because they involved lower level concepts, which might overcomplicate the beginning for new programmers. There was also not sufficient time to compare additional languages. Another limitation that was needed to be done, was when deciding what to analyze when answering the first research question. Trying to break down the complete syntax of all components in three languages would not be plausible in the time that was given and so the authors focus on the more prominent parts of the languages. The authors acknowledges that libraries is a big part of a programming language and can be used to extend the features of said language. The time restraint and shear amount of libraries available would make it impossible to compare and add valuable knowledge to the field of computer science. When answering research question 2 the authors needed to set some limitations on where the programming language exceeds. The authors picked the areas in which the programming languages are popular to use and then tried to explain why they may be popular in that specific field. Another limitation that was needed to be done was to decide which tasks should be tested for research question 3 since it is not reasonable to test a large amount of tasks. Instead we decided to focus on some tasks that does very different things. 5 1.4 Structure RQ1 The first research question needed extra limitations since there are many components to talk about in each programming language.