Introduction to Java: But we already know a language...
Welcome to Object Oriented Programming. This course is on Object Oriented Programming, or OOP, which is a style of programming. When we started programming, we learned how to solve problems procedurally, in which we wrote code and functions to accomplish a set of steps, in order. Later in the semester, we started building objects, which could do complicated things even based on simple code (remember your space invaders? or Linked Lists?). This was simple OOP.
Some programming languages support this style of programming more than others. C++ supports OOP well (it was designed to), as were other major languages, such as Java, C#, and Python.
In this course, we will be using Java. Why Java? Knowing multiple languages will help you better understand programming. Java is in many respects easier than C++, and it comes with many built-in features that we can explore. You should expect to learn or at least become familiary with 8-10 languages while you are an undergrad. Once you have learned the first 2-3, you will find it very easy to pick up new ones.
Why do we need more than one language? Different languages are good for solving different problems. Each has its strengths and weaknesses. The cost model of software development has driven the direction of language development for the last few decades:
- Early computers were expensive, but programmers' time was cheap. In 1964, a Burroughs 205 mainframe cost $5,000,000 (worth $36,500,000 in 2012 dollars). This was a popular computer for universities. The median wage in 1964 was $6,000 (worth $43,000 in 2012 dollars).
- Modern computers are cheaper than programmers's time. You can buy a high-end desktop for under $1,000. The median wage for a software engineer is currently $88,000.
High-level languages are fast to develop because they hide the messy and time-consuming bits of programming, like tracking registers and allocating memory. The trade-off is that these languages take more clock-cycles to run each command. Thanks to Moore's Law, we get faster machines each year, which makes high-level programming feasible. The graph below shows an artist's interpretation of the relative distribution of various languages between time to develop and time to run. (NOTE - this is just to give you a rough idea of the relative speeds, there is no data to back up the exact position of any language on the graph):

Video games are often written in multiple languages. The graphics code in a first-person shooter might have to must render a million polygons onscreen, at 30 frames per second. That code needs to be extremely efficient. It is probably written in C and/or Assembler. The high-level code that controls a character's actions are often written in a high-level scripting language such as Lua. In general - code that runs many times per second needs to be efficient; code than runs once can take as many clock cycles as it needs.
There is one other reason why we see multiple programming languages: Personal Preference. The computer industry is full of highly-opinionated people who insist that their favorite language/OS/editor/browser/etc. is better than all the others. We call these arguments "religious wars". If you show the above graph to any software professional, it will almost certainly spawn an argument as to where each language should appear on the chart.
Java History
Java is a programing language developed by Jim Gosling and Bill Joy at Sun Microsystems in the 1990s. Sun wanted to be the leader in "intelligent" consumer devices. They wanted every bit of electronics you have to be controlled by downloadable programs that would do things like model your behavior and do things for you (e.g. to have your refrigerator tell you that you are out beer.) They began to design a programming language that could be used for these goals. It had to:
- Be robust. Your coffee maker should not crash.
- Be portable. Code that runs on one microwave needs to be able to run on a microwave from another manufacturer, easily and without compilation.
- Be safe. It should be difficult to develop malicious code to take over your appliances like some B horror movie.
The smart appliance market did not work out as a business model for Sun. There were too many technical challenges and faulty assumptions about the market.
Then along came the world wide web. These same requirements were needed for the Internet. Java finally found its market.
How is it robust?
- Java gets rid of pointers. Pointer errors are the source of most programming bugs in C/C++. (No more Seg Faults!)
- Java uses garbage collection. Unused memory is automatically freed, so you never have memory leaks. Programmers do not have to allocate and deallocate memory themselves. (No more delete!)
- Java streamlines programming. It's easy to mess up in C++ because it carries all sorts of baggage from C. (e.g. .c_str()) Java's simplicity and readability make development and maintenance cheaper.
- Java has similar syntax to C++. This lets existing programers learn it easily.
How is it portable? And safe?
Unlike C++, Java runs on a virtual machine called the JVM (Java Virtual Machine).
Remember, C++ is compiled and linked, which results in an executable file only appropriate for that computer's architecture. Java is compiled into bytecode, which is identical no matter which machine it is compiled on. This bytecode is then interpreted by the Java Virtual Machine and run. So, anybody with a JVM (like the one your computer is always telling you to update) can run the bytecode.
But what is a virtual machine? A virtual machine is software capable of running code that appears to that code to be a specific physical computing environment. The diagrams below illustrate how this works:

The diagram above shows two different operating systems represented as puzzle pieces. A program will only run if its lower edge fits perfectly into the OS piece. The shape of the edge represents the libraries that must be linked at compile-time. For example, the "stdio" library that handles basic print and keyboard functions is very different between Windows and Linux.

The diagram above show the same program (Firefox) compiled for the two different Operating Systems. This program is written in C++. The program seems the same to the user (each one serves web pages) but are very different under the hood. The version compiled on Windows will not run on linux, and vice-versa.

The diagram above shows how the Java Virtual Machine fits between the application and the Operating System. OpenOffice is written in Java. You can download one copy of this program that will run on either Windows or Linux. The Java Virtual Machine is a program written in C++. It must be compiled specifically for either Windows or Linux, the same as Firefox. Once it is running, however, it can run any Java program, regardless of the underlying OS. That makes Java code more "portable" than C/C++.
Life is full of tradeoffs... this portability makes Java code generally slower than C/C++.