Now that you've learned a program in Java, we're going to finish this part of the course by putting what you've learned in context with other programming languages that you might have heard about. First, we'll take a look at some popular languages. We begin with a story about the Tower of Babel. Now this is a story about the origins of multiple languages that you may have heard about. After the flood, the whole Earth was of one language and once speech. And they built a city and a tower at Babel, believing that with a single language people will be able to do anything that they imagine. But Yahweh disagrees and confounds the language of all the earth. And so the question to think about is why? Why would he do that? And the answer is, the idea is that proliferation of cultural differences and multiple languages is one basis of civilization. We'll come back to this story in just a minute. Now, just as a starting example, if you want to get from one place to another, there are a lot of different ways to do it. You could use rollerskates or a bicycle, or maybe a car, or maybe you need an all-wheel drive vehicle. It may be a truck, or it may be an airliner or a stealth bomber. All of these solve the problem of getting from one place to another. In the same way, let's think about several different ways to solve a programming problem. You can use C or Java or Matlab or C++ or Python, Ruby, OCaml, there's many, many different ways to solve a programming problem. And, of course, the parallel here is to emphasize the idea that different languages are appropriate for different tasks. And we'll try to talk about that in the context of each of these languages. Now just to start, hopefully after this course you can write Java code. And just as a running example, we'll use the three sum program that we studied in the performance lecture. Where what we wanted to do was read a set of integer values from standard input and find all the triples that sum to zero. And this is code from our lecture on performance. We take an integer in from the command line. We make any array to hold that many integers. We read those integers off standard in. And then we go in a triple for loop, checking all the triples of those integers to see if they sum to zero. And we print out the ones that do. And that's a program that we study and we're just going to use that as an example. And so if we have a file called 8ints.txt, that has those 8 integers, 30, -30, -20, -10, and so forth. We compile our program with javac ThreeSum.java. And then we run it by typing java ThreeSum. And we give it the command line 8 and we take from standard input 8ints.txt. Then it prints out those for triples that sum to zero. So you can write this program. If we were to give you a similar task you could write a program to do it. And give it data as input, compile it and run it, and solve the problem. And so now I want to tell you that, well, knowing that you can also write C code. There's differences, and the differences are highlighted in red here and I'll talk about them briefly. But even not understanding quite what those things do, you can pattern match off this program. Much in the same way, when we first started, there were things in Hello.Java you didn't understand, but you still were able to write that program and run it. In same way you can do that with the C program. So that's the text of the program, it's got those differences highlighted in red. And for the same file, if you type cc ThreeSum.c. Your computer most likely has a C compiler, that's what cc stands for. It produces a machine language file called a.out, it always produces the same file a.out. You want to save it away in ThreeSum.machinelanguage or whatever you can do that. And so to run it, you type a.out, give it a command line argument and take its command line from its standard input from the file same as before, and it'll print out the same four values. There's lots of noticeable differences. The first thing is, in the first two is the library conventions. How do i specify where the standard input in other libraries are, and that's using the include statement. The library names are .H and they're inside brackets in C. The way that we create arrays, there's the second line of the program. n star a, gotta have that star, equals malik n star size of n. That says give me an array of n integers at execution time. Standard input, we say scan f in C. And then there has to be that all important ampersand before the thing that we read, and you can learn about the details of all of these things. And then also taking from the command line a 2 i, so function that converts from a string or ASCII to integer. The star and the ampersand have to do with pointer manipulation and we'll talk about that in part two of the course. But those are the highlighted differences. But even so, from this program you could write another program that reads in integers and process it in some other way. The body of the program is very much the same as it was in java. So you can write C, if you've always wanted to write a C program, type in this one and run it and them maybe write another C program. The big difference between C and Java, and there are plenty of differences, is that there's no data extraction, there's no objects in C. A C program is just a set of static methods. The way Java was before the last couple of lecutres where we talked about data extraction in objects. In 1989, Bjarne Stroustrup introduced C++, and the idea of that language is to add data extraction to C. It was called C with classes. And it embodies many of the ideas of object oriented programming that were coming into widespread use, and that was the purpose of the C++ language. Now you might remember this quote from Stroustrup, there's only two kinds of programming languages; those that people always gripe about and those that nobody uses. So you can also write C++ code. Well, one thing is that you can use C++ pretty much just like C. Now there's differences. So now we have a more friendly way to create an array, more like we're familiar with in Java and new int and in brackets, the. Body of the code is pretty much the same. The input and output are different and we'll talk about that in just a second. Your print likely have a C++ compiler on your computer, how you should call the program, .cxx for C++, cpp is the name of the compiler. So if you type cpp Threesum.cxx, again, it creates an a.out. You run that a.out with the right command line argument and the file, you get the same for output. They're very similar to C. The differences, again, the libraries are a little bit renamed differently. Standard input has this idiom where we use double or greater than signs. That's kind of like pushing it to, getting it from an input stream. And the output idioms, the same ways, double less than signs, getting it from the output streams. And it's not formatted, which many people found annoying. And nowadays, you have formatted printing in C++. But it's also got this pointer manipulation, that's what the star and the A has to do with. And we'll talk more about that in part two. So you can use C++ with objects. And this example is from programmer part two called the building a simple table with binary search trees. So we won't really do the details other than to show the kind of the idea of an object that contains fields, very much the same way that we write it in Java. This code is actually from a version of my algorithms book in 1990. And that code still works fine today. C++ is very widely used throughout our computational infrastructure. because it combines the simplicity of C with the power of object-oriented programming. Now the big challenge with C++ is the idea of pointer manipulation. And that algorithms book talks quite a bit about that. We're manipulating references to objects directly in our code. Also, the idea of generics which we talk about again a little bit more in part two, where we want to write programs that can work with any type of data. That's something that was bolted on to C++ later in the game, so that's a challenge there. It's also a challenge in Java, to be frank. But the big difference between C and C++ in Java, and again, there's many, but the big one is that you're responsible for allocating memory in those languages. Your programs can manipulate and do manipulate pointers. The system has a library of methods that will do memory allocation and reserve memory for you. But programs explicitly call methods that get memory, call and allocate it, and free it up for any objects that are created. And this creates a big possible source of bugs, called memory leaks. So if you have C code, you want to reuse an array name. So you create the array with a call to a memory allocation routine, and this says get me 5 doubles. And then when you're done with it, you free the array, and then maybe you create and you get another one with 10 doubles, for example. In Java, we have automatic garbage collection where the system keeps track of all references, manages the memory use, reclaims memory that's no longer accessible from your program, and the programmer is freed from dealing with it. So in Java, you just say give me an array of 5 doubles, and then sometime later, give me a new array of 10 doubles. And that first array of 5 doubles, the system goes and reclaims that memory at some point. Like many people, I personally, and many others drag, kicking, and screaming into this. Because it seemed to us the garbage collection was going to be very slow and inefficient. It's turned out that, actually, the cost of garbage collection is hardly noticeable nowadays. And it's much, much better for programmers, particularly, beginning programmers to not have to worry about where the memory is. And even an experienced C programmer might forget or make a mistake in the amount of memory freed, which can lead to crashes. And that's certainly one source of crashes in modern systems, it's memory leaks, where the programmer loses track of the amount of memory being used. And eventually, the system runs out of memory. So if you want to learn those languages, you're going to have to learn to deal with memory allocation. So just in general, if you have code that manipulates pointers, it's much more difficult for this system to check that the code's free of that sort of bug. Okay, what about Python? A lot of people ask us why don't we teach the course in Python. And well, let's take a look at Python. You can certainly write Python programs. Now one thing that you can do is use it like a calculator. So most likely on your system, if you type Python, you'll get the access to an interactive version of Python. And it gives you three greater than signs, and then you can type expressions at it. If somebody says that's what I have, I type 2 + 2 and it gives me 4, so I'm happy with that. Next thing I do is well, let's compute the golden ratio, and figure the square root function must be called sqrt. And always it says, well, I don't know what square root means. So I have to go and look up and find out how to import the math library. So not so difficult, type import math, and now I can type any math expression, and I get the golden ratio. So just with that much and there's online documentation type help. There's lots you can do within just a few minutes or a few hours, you can throw away your calculator. Please throw away your calculator. If you can write code and save code and get an extensible libraries and all the benefits of programming on any device that runs Python, and that's pretty much in a computing device that you might have. But you can also use Python like Java. We can write a threesum program. Here's the threesum program in Python. And I'll talk about all these differences in a minute. So for Python, you just say Python. Type in that program, say that program name, command line argument, the standard input redirection, you get the answer right away. So the noticeable difference are number one, you probably noticed there's no braces. One thing people dislike about languages like Java and C is the braces, the left and right braces that seem to be all over the place. Python indents mean braces. You can indent and you have to be consistent, and some Python programs insist that you indent a certain number. But, this program, each indent just means a different block of code. We actually have no two line blocks in this program, but if you had two lines the same indentation, it'd just be braces around those. Another quite noticeable difference is there's no type declarations. We never said that n was an integer or that a was an array of integers. Python has what's called run time type checking, and we'll talk about that later on. Works okay as long as when it comes time to assign a value to the thing, the one that you're assigning it happens to be an integer. And we'll talk about that later. Array creation's always different. So in Python, you say (0)*N makes an array of N 0s. If the I/O idioms are different, so stdio.readInt() is the read in. So that's an R standard I/O library. You could also use Python's Sys.stdin model, they have a built in print or we have a standard I/O. Even for simple, taking from standard input, standard output stream is always something that you have to cope with in a new programming language. One of the first things that you need to know how to do. But again, you can take one of our sample programs and get pretty far once you've seen how it works for one sample program. And the other thing about Python is the for loop works differently. So we say for IN range N, range of an integer is a list from zero to that integer. And it's a way of specifying all the possible values that might take on. There's pluses and minuses to that approach that we won't get into right now, but that's how you can decode this program and how you can write Python code. So with this right away again, you run this program if you've always wanted to run a Python program. Then modify it to do something else with integers and you're off and going with Python programming. It does bring up an important concept that we've talked a little bit about, but in the context of Python it's a good time to really make this distinction. So with Java, we've been working with a compiler, which takes your whole program and translates it to machine code. Actually, in Java's case it's a virtual low level language that later is translated to specific code for specific machines. So you have your source code, which is the text that you type. And you type javac in this program called the compiler converts it into a machine code and C and C++ do the same kind of thing. An interpreter is a different kind of program. What that one does is simulate the operation of a machine running your code. So in fact, that's what the Java virtual machine does. When you say Java, it takes the simple machine code in a pretense that it's your computer running it. C actually makes machine code and just runs it in C++. But for Python, what Python does, when you say python is it runs this program called an interpreter that takes your program one instruction at a time and executes it, keeps track of the values, and variables, and changes according to the program said. Andi it does it one instruction at a time. It's a bit slower to do that because it's a lot of work looking at the meaning of variables and so forth. But on the other hand, it's just a different computational experience. It's kind of closer to when you had the interactive Python, you'd type one instruction at a time. It's the same process that's going on when you put your instructions in a file and run them. Now, one big difference between Python and these other languages, again, there's many, is that Python doesn't type check a compile time. So that's why there's no need to declare types of variables. The system only checks for type errors at run time, when it's time to assign a double value to a variable. And then maybe add another double value to it. It's only at the time that those operations are performed on those types of data that the system checks to make sure that it can do the operation that's specified. Now it does make it easier to write small programs. You have an idea of what the type of your variables are and the idea is you shouldn't have to continually remind the system that I'm working with an integer. I know I'm working with an integer. On the other hand, for large programs with complicated objects, with types of behavior that we're building, this is maybe questionable at best. And here's a typical scenario that I've heard more than once in the real world. That's a combination of this idea of no compile-time type checking and the idea that Python has interpreted. So, scientists or a program will use Python to solve or write a small program because it's so easy. You just type it in, there's no braces. But the small programs have ways of turning into bigger and bigger programs. And maybe at the time that it actually seems to be debugged and we're ready to run it on big data, the scientists or the programmer makes a mistake, maybe near the end of the program. Maybe the name of the file where the results finally get written is supposed to a string concatenated with something and it's not quite the right type. So what might happen is that first of all, the program runs for a long time because you can debug it for a small problem. But when you give it a big problem you're going to notice that Python's a lot slower than Java. So rather than your thing finishing in a half an hour, it might take half a day or more. And then it crashes because it gets to the type error and just crashes. And I've heard of this happening in more than one situation in the real world. So what I say is that using Python for large problems is playing with fire. And I'll come back to that. But it is reasonable to, number one, throw out your calculator and use Python. And number two, fine to run prototype in Python. But if you're going to take this program and use it for some scientific investigation or some product that you want to sell, you really want the benefit of the kind of automatic checking that a system like Java does. And also, you might want the efficiency that you get from compiled code as opposed to interpreted code. Well, and then there's Matlab. Lots of scientists and engineers use Matlab. And you can write Matlab code. Again, just here is another language just like Java. So now where for loops, we say one colon in to mean from one through N and then we start at 1, not at 0. Of course, the input and output are different. We don't have braces but we have to type end at the end of blocks. So all these things are easy to deal with. And now that you've seen this program, you can write MATLAB programs to do tasks just like with the other ones. Always wanted to write a MATLAB program, go ahead and write code like this. A more typical example, and what it was built for is to use MATLAB for matrix processing. So if I type this code A = [1 3 5; 2 4 7], I'm specifying a matrix with two rows and three columns. And then if I type B = [-5 8; 3 9; 4 0], that's a matrix with three rows and two columns. And then I can just type C = A*B, and that means matrix multiplication in MATLAB. And then if I just ask for the value of C, I'd get the multiplied matrix. And of course, it can do values and many other things. So using MATLAB for matrix processing, that's definitely what it was built for. It also powerful tools for making graphs, and plotting, and doing other things. But the big difference is between MATLAB and the other languages we've talked about, again, there's a lot. The first one is MATLAB is not free. So I noticed I didn't say you can just go to your computer and type MATLAB. You have to buy MATLAB or your institution has to buy it. And if you write code for MATLAB and you want your family to see your code or whatever, they're going to have to buy it. That's why we don't use it in the beginning course. And the other thing is that most MATLAB programmers really just use one data type, the matrix. And they kind of understand that that's what they're doing. For example, in MATLAB, if you say i = 0, well, what that means is redefine the value of the complex number i to be a 1x1 matrix whose entry is 0, probably not where you intended. Again, no type checking, and all kinds of concurrent problems. Now it's important to note that MATLAB happens to be actually written in Java. It's intended to process matrices, and more general tasks, that's what Java is intended for. And actually, Java compilers are written in C. Nowadays, C compilers are written in C, and really, that's not really the right way to build a programming language. At least test it by writing a compiler in that language. And there's good matrix libraries, actually, a lot of times, the same matrix libraries are available for all these different languages. So you can do matrix processing in any one of these languages. It might not be quite as simple as a times b as it is in MATLAB, but you can even make it that way, in say, C++ or Python. So certainly, reasonably use MATLAB as the matrix calculator. But if you want to do anything else, probably you want to use Java, C, or C++, actually nowadays, you can get matrix calculator on your phone. So now you know how to program in lots of different programming languages, C, C++, Python, MATLAB. There are lots of differences, might take some study, but not nearly as much as you might have anticipated before you learned how to program well in one language. And most computer scientists will say that, learning the first language is what's important. And you're going to learn many languages if you're involved with computers.