Introduction to Computers, Programs, and Java
Some Important Dates in the History of Computers
First programmable device (Turkish inventor Al-Jazari's water-powered Castle Clock)
First description of the "Analytical Engine" by Englishm mathematician Charles Babbage (a fully programmable mechanical computer, able to perform calculations automatically)
Ada Lovelace (daughter of poet Lord Byron) writes notes on how to program the analytical engine to calculate Bernoulli numbers [needed for uniform formula of the sum of the first n perfect m powers.])
Babbage builds a piece of his "Analytical Engine (limited finances and other reasons kept it from being built)
- 1930's and 1940's
- Digital computers at the dawn and during WWII (Konrad Zuse and the German Z1 and Z3 [1938-41] probably the first - used binary and tape; Colossus Mark I in Britain 1943 to break secret codes; ENIAC 1946 program-controlled by patch cables and switches, designed to compute artillery firing tables, but used for calculations for the hydrogen bomb.)
What is a Computer?
A computer is a machine that manipulates data according to a list of instructions, consisting of both hardware and software. Computers can take many different physical forms.
Modern computers generally have the following (hardware) components:
- CPU (Central Processing Unit),
- Memory (RAM),
- Storage Devices (hard disk, floppy disk, CD, DVD, thumb-drive, etc...)
- Output Devices (monitor, printer, etc...)
- Input Devices (keyboard, mouse, graphics tablet, scanner, joystick, the Mac Wheel, a capacitive touch screen, etc...)
- Communication Devices (network interface card [NIC], modem, bluetooth adapter, etc...)
These components talk to one another through a communication channel called a "bus" to execute instructions in the form of computer programs (software).
CPU (Central Processing Unit)
- The CPU retrieves instructions from memory (RAM) and executes them
- The CPU's speed is measured in GHz (gigahertz).
- 1 GHz = 1 billion cycles per second.
- Modern CPUs have a few "cores"
The computer's memory is responsible for storing data and program instructions for the CPU.
Programs and data need to be loaded into main memory (RAM) before execution by CPU
When data is stored in memory, everything is coded as a series of bits
A "bit" is a binary digit taking the value of 0 or 1.
Bits are used as they naturally correspond to the two "states" in electronic devices (on and off).
You can fairly easily convert between binary and decimal number systems - instead of expressing numbers in the "decimal way" (i.e., base 10) as sums of multiples (0-9) of powers of 10, we write them in "binary" as sums of multiples (0-1) of powers of 2. In both cases, the coefficients on the powers give the digits needed.
Memory is volatile storage. (i.e., it goes away when the power goes off)
RAM (random access memory): Memory that is considered "random access", as you can access any memory cell directly if you know the row and column that intersect at that cell.
SAM (serial access memory): Memory that can only be accessed sequentially (like a cassette tape). This type of memory works well for memory buffers, where the data is normally stored in the order in which it will be used.
DRAM (dynamic RAM): The most common form of computer memory today. In a DRAM memory cell a 0 or a 1 is represented by a paired transistor and capacitor. The capacitor holds the bit of information, while the transistor acts as a switch that lets the control circuitry on the memory chip read the capacitor or change its state. Think of a capacitor as a small bucket that stores electrons. To store a 1, the bucket is filled with electrons. To store a 0, it is emptied. Unfortunately, capacitors leak. Without the intervention of the memory controller, within a few milliseconds a capacitor's bucket-full of electrons becomes empty. As such, to store information for any length of time, the computer must (very frequently) recharge all of the capacitors holding a 1 before they discharge. To do this, the memory controller reads the memory and then writes it right back again. This refresh operation happens automatically thousands of times per second.)
Memory is measured in "bytes", each of which is a sequence of 8 "bits" (a convenient power of 2 with enough bits to encode the alphabet and other necessary characters)
Fortunately, high-level programmers (like us) need not be concerned with encoding and decoding data to and from series of bits - this is performed automatically.
You can envision memory in the following way:
| Memory Address
|| Memory Content
|| Decoded As
|| character "J"
|| character "a"
|| character "v"
|| character "a"
|| number "8"
- Remember, main memory (RAM) is volatile. Storage devices and secondary memory allow for more permanent storage of programs and data.
Input and Output Devices
- I/O is the means by which a computer receives information from the outside world and sends the result back.
Programming Languages (and where Java fits in...)
- A program's set of instructions are specified using a programming language.
- There are three types of programming languages:
- Machine language
- a set of primitive instructions built into every computer, in the form of binary code.
- pretty darn hard to read, as it consists of just 1's and 0's!
- Assembly language
- developed to make programming easier
ADDF3 R1, R2, R3
- The computer cannot understand assembly language. It must first be converted to machine code. This is done by an "assembler". The assembler takes an assembly "source file" containing instructions like the one shown above, and translates the same into a machine code file (1's and 0's), which can then be executed by the computer.
- High-level languages
- Developed to make programming even easier still - both to learn and use.
- High-level languages try to mimic our natural language, and are hence, far more readable.
area = 5 * 5 * 3.1415;
- Similar to how assembly source files must be "assembled" with an assembler to create the machine code file - source code written in a high level language must either be "interpreted" by an interpreter, or "compiled" with a compiler. The difference between these two is that an interpreter reads the source code one statement at a time, translates the statement into machine code (or virtual machine code) and then immediately executes it. A compiler, on the other hand, translates all of the source code into a machine language program called an "object program". This object program can then be linked with supporting libraries using a "linker" to create an executable file you can run on the computer.
- Supporting libraries keep programmers from having to reinvent the wheel every time they write a program. For example, lots of programs need to display a dialog box to the user. Rather than every programmer having to write his or her own instructions for how to display a dialog box to the user, a common set of these instructions sits inside a library that can be called upon by the programmer. In this way, the programmer can focus more on the details unique to his particular program.
- Some popular high-level languages
- COBOL (Common Business Oriented Language)
- FORTRAN (FORmula TRANslation)
- BASIC (Beginner All-Purpose Symbolic Instructional Code)
- Pascal (named for mathematician and philosopher, Blaise Pascal of "Pascal's Triangle" fame)
- Ada (named for Ada Lovelace, daughter of Lord Byron, the poet and the "first programmer" - having written a program for the machine Charles Babbage never built: the Analytical Engine, to calculate Bernoulli numbers.)
- C (whose developer designed B first)
- Visual Basic (Basic-like visual language developed by Microsoft)
- Delphi (Pascal-like visual language developed by Borland)
- C++ (an object-oriented language, based on C)
- Java (We will use this one!!!)
- C# (i.e., "C Sharp"; a Java-like language developed by Microsoft)
- Developed by James Gosling and the "Green Team a.k.a. FirstPerson" group from Sun Microsystems who originally were trying to take advantage of the coming trend of consumer device and computer convergence.
- Originally named "Oak" for a tree outside Gosling's office.
- After developing the language to create a demo of one of these convergent devices: an interactive, handheld homeentertainment device controller with an animated touch-screen - and failing to successfully pitch the idea to the cable companies, the team focused their attention on using the language to develop a similar content delivery system for the internet.
- The team created a Java-technology-based clone of Mosaic named "WebRunner" (named after the movie "Blade Runner"), which later became the "Hot-Java" Browser.
- Marc Andreessen of Netscape made an agreement to integrate Java into the Netscape browser and the rest is history...
- Why the name Java? There were ten names considered (see below), and there are some disputes and failed recollections as to who actually suggested it be named "Java".
- Silk, Lyric, Pepper, NetProse, Neon, Java, DNA, WebDancer, WebSpinner, WRL (WebRunner Languages)
Why Learn Java?
- Java can easily create applications for
- Internet services (Java Applets, Java Web Applications)
- Desktop computers (cross-platform)
- Hand-held devices
- Java is a well-established and a very popular language
- Don't believe it? Check out www.langpop.com
- Java is...
- This provides great flexibility, modularity, clarity, and reusability
- Java was designed to be object-oriented from the beginning
- Distributed computing involves several computers working together on a network
- Java was designed to make distributed computing easy
- Compilers can detect many problems that would first show up at execution time in other languages
- Java has a runtime exception-handling feature (This allows programs to exit more gracefully when there are unforeseen problems.)
- Architecture-Neutral and Portable
- You can write the code once, and run your program on any platform (Windows, Unix, MacOS, etc..) When you write a java program, you first compile the program into Java Bytecode, which the Java Virtual Machine can then understand and execute on any computer, regardless of the operating system.
- Java implements several security mechanisms to protect your system against harm caused by stray programs - especially with regard to applets
- Applets are little programs that reside in/on web pages and run automatically when the page is visited - just think what would happen if such security measures didn't exist... visiting a web page could launch a program to reformat your hard drive!.
- Multithreading allows for a program to be broken up into separate processes that can be run in parallel. This means programs run faster on computers with multiple processors that can then split the work up. It also means greater responsiveness to user input (for example: a long calculation won't interfere with the user hitting the cancel button).
What makes up the Java Language?
- The Java Language Specification -- which consists of the syntax and semantics of the Java language
- The Java API (Application Program Interface) -- which consists of predefined classes for developing Java programs
There are several different editions of the JDK (Java Development Kit), which programmers use to write Java programs:
- Java Standard Edition (J2SE) - used to develop client-side standalone applications or applets
- Java Enterprise Edition (J2EE) - used to develop server-side applications such as Java servlets and Java ServerPages
- Java Micro Edition (J2ME) - used to develop applications for mobile devices such as cell phones
- (Note: We will use J2SE in this course.)
There are several different versions of the JDK, as well, that have been released throughout the years:
- JDK 1.02 (released in 1995)
- JDK 1.1 (1996)
- JDK 1.2 (1998)
- JDK 1.3 (2000)
- JDK 1.4 (2002)
- JDK 1.5 (2004) a.k.a. JDK 5 or Java 5
- JDK 1.6 (2006) a.k.a. JDK 6 or Java 6
- JDK 1.7 (2011) a.k.a. JDK 7 or Java 7
- JDK 1.8 (released to developers in 2014) a.k.a. Java 8
Java Program Development
A Minimal Development Environment
If desired, one can create a java program using only a text editor, and compile and run it using only the command-line executable programs javac.exe and java.exe, respectively. javac.exe is the Java Compiler and produces class files containing Java byte code. java.exe is the Java Virtual Machine (JVM) and is able to execute the instructions written in Java byte code contained in the class files produced by javac.exe. Compiling and running things from the command-line is perhaps not the most efficient way of doing things, but it will work.
Integrated Development Environments (a.k.a. "IDE"s)
The process of writing programs is generally easier if one works within an IDE (Integrated Development Environment). This is a software application that provides comprehensive facilities for software development, such as:
- A source code editor
- A compiler and/or interpreter
- A debugger (which helps you walk through your code line by line as it executes, to track down runtime/logical errors.
Popular IDEs for Java:
- Eclipse (Open source by IBM ...and FREE!)
- NetBeans (Open source by Sun Microsystems ...and FREE!)
- Borland JBuilder 2007
We will use Eclipse in this course.
The Process of Writing a Program
However you plan on writing, compiling, and running your programs -- there is a certain order to the process, as suggested below:
- Create/Edit Source Code You can use a text editor, like "notepad" or "edit" in Windows, or "vi", "vim", or "gedit" in Unix, to type your Java source code. Several of these editors are designed to run from a command line interface (CLI) through a shell window. Each program file should have a "*.java" extension.
- Compile Source Code When the program is written, it is then compiled with the Java compiler, "javac <my sourcefile>", which is included in the JDK.
- Fix Syntax Errors If there were errors in your source files that keep the compiler from compiling your program (these type of errors are called "syntax errors"), re-edit the source files to eliminate them and then recompile. If there were no such errors, then a Java bytecode file will be produced (i.e., a file with a "*.class" extension).
- Run Bytecode The java bytecode file is a set of instructions that can be executed by the Java Virtual Machine (JVM). To run the JVM, one can type "java <my classfile>" from the command line. This runs the program.
- The *.class files are not meant to be directly read by humans. If you really want to know what is inside them, you can use "javap -c -s -verbose <classfile>" from the command line to translate a class file into bytecode you can actually read (which is printed to "stdout" by default). This is called "disassembling" the class file. In this class, you should never have to do this - just know that it can be done. Expert programmers disassemble class files to discover how they might tweak the associated programs into running more efficiently. Note: If you do execute the above command, leave off the .class extension just like you do when running "java".
- Fix Runtime and Logical Errors There might still be errors present in your program, despite the fact that it compiled.
- Sometimes, a program crashes in the middle of running. This is called a "runtime error".
- Sometimes the program runs, but doesn't do what you want it to do. This is called a "logical" error. If you encounter a runtime or logical error, just like before - go back to the source files, fix the error, recompile, and run it again. Repeat this process until all of the errors are gone.