prev | TOC | next

Introduction to Programming in Java

 

Welcome to the world of Java programming...

These lessons will introduce you to the main concepts needed to start programming in Java.  Because the full Java language is quite extensive, we are not going to teach you every facet of the language.  However, if you do want to learn more after working through these lessons, go to Sun's free on-line Java Tutorial for more detailed information.  In addition, you can join the Jurtle Users Yahoo group to ask questions, exchange code, and take part in the Jurtle community.

A brief overview of programming and programming languages:

Programming involves writing a set of instructions (called the program) to tell the computer how to accomplish some task.  The task could be something simple like reading a list of names from a file, alphabetizing them and writing them back out to the file.  However, it could be something much more complex, like displaying a complete graphical user interface (GUI) for your favorite game and implementing all the game's logic.

A program's instructions are written in a specific programming language.  Early programming languages required us to think like computers and write in a form very close to how the computers worked internally.  Over the years there has been steady development in computer languages to allow the computer to understand a language which is more natural for us.

A large number of programming languages have been developed over the years.  Like human languages, they have several families with many branches going off from them.  Each family may serve a different purpose, and there is no one best language.  Different languages are good for different things.  

Java is a relatively new object-oriented programming language that has achieved widespread popularity.  It is a general-purpose language that can be used for many types of programming tasks.

The evolutionary path to Java is roughly:

Machine language All computer's processors have a basic instruction set.  This is the set of operations the hardware directly knows how to perform (e.g., add, subtract, move data).  In digital computers, each instruction has a binary number assigned to it (e.g., 01101010 might mean "add the next two numbers"). 

Machine language was used on the first digital computers in the 1940s.  Here you are interacting with the computer in its own terms -- 1s and 0s.    When programming in machine language, programs were entered one instruction at a time by flipping a row of switches on the front of the computer.  An up switch would mean "1" and down would be "0."    The process was very tedious and the programs were intimately tied to the computer's specific instruction set.

Assembler
As opposed to working directly with machine language, assembly language assigns mnemonics (or short words) to the binary instructions.  The programmer would write a program using these mnemonics along with the data they were operating on.  The programmer still needed to think in the the computer's terms while programming, but it made life a lot simpler.  A separate program called an assembler would translate the mnemonics into the machine language that the computer ultimately needed.

Fortran
Fortran was one of the early "High Level Languages" and dates back to 1954.  The vocabulary of a high level language more closely matches English and includes keywords such as for, while, do, return, extend, repeat, go to, procedure, function, etc.  A special program called a compiler  translated the high level language into the computer's machine language. 

Fortran has been substantially enhanced over the years and is still in limited use today.  This language was a workhorse in scientific and general-purpose programming in the 1970s and 1980s.

Pascal
Pascal was developed by Niklaus Wirth in 1970.  This language was an offshoot of Algol and was instrumental in the development and popularization of structured programming (see below).

C
C was developed at Bell Labs in the early 1970s as a programming language for writing operating systems and programming utilities.  The Unix operating system was originally written in C.

C++
C++ is an object-oriented enhancement to C developed at Bell Labs in the early 1980s.  It is widely used today for application and systems programming.

Java
Java is an object-oriented language developed by Sun Microsystems in the early 1990s and released in 1995.  Java combined many of the best ideas from Pascal, C and various other object-oriented languages.

The principal design goals were to have a relatively simple and “safe” language.  At the time, C++ was (and probably still is) the dominant object-oriented language, and Java's creators deemed it too complex and error-prone for non-expert programmers.

Java was designed as a cross-platform language.  The same compiled code can (theoretically) run on any computer.  Java accomplishes this feat by running your program within a Java Virtual Machine (JVM).  The JVM is another program that simulates a computer that has Java bytecode as its instruction set.  The Java compiler translates your Java program into the bytecodes needed by the virtual machine.  So when you run a Java program it actually runs on a "computer" being simulated by your computer.  Today, Java Virtual Machines are available for most modern computers.

Java's initial popularity was because of its concept of applets. These are small downloadable programs that run within a web browser.  It was the cross-platform nature of Java that made this feasible.  While applets are still in use today,  Java's success is now more in general-purpose programming and server-side programming (i.e., programs running on a web server that generate dynamic web pages).  Java is one of the fastest-growing languages around today.

Java's strengths:

  • Good object-oriented model
  • Cross-platform language
  • Hard to make mistakes that cause a program to crash
  • Large standard library of code you can use in your program
  • Many non-standard libraries available for free
  • Popular (easy to find other programmers to talk to)


Java's weaknesses:

  • The virtual machine needs to be running alongside your application. This increases the amount of memory your program needs.
  • Because the program runs on a virtual machine, it may be slightly slower than a natively compiled program.
  • Because the standard code libraries are designed to be cross-platform, you cannot easily take full advantage of all features unique to a given platform.


Compilers

As mentioned above, with high level languages such as Java, computers do not directly execute the program in the language you write it in.  It first has to be translated by a program called a compiler into that computer's machine language.  This process is referred to as "compiling your program."  The compiler can catch many programming mistakes during this process and present you with a list of the errors it found.  Lesson 14: Compilation Errors talks further about these error messages.

Programming Methodologies:

There have been many approaches to programming over the years.  Two that are relevant to Java and that you'll probably hear of are:

  • Structured Programming

    In the early years of programming, a program would often be one monolithic piece of code with the flow of execution jumping all over the place.  Structured programming tried to remove this "spaghetti logic" and enforce a rule where one routine (chunk of code) was responsible for one piece on computation.  A program was written by successively invoking a number of these "subroutines."

  • Object-Oriented Programming

    In this style of programming you create objects that are a combination of code and data.  An object holds its data and knows how to perform various computations on it.  For example, a school database program might contain a "Student" type of object.  A given student object might hold information about the student's name, age, home address, grade level, test results, etc.  The student object might also contain code that could calculate the end-of-year grade, current GPA, and years left until graduation.

    Smalltalk was one of the first object-oriented languages.  While it is still in use today, Java and C++ are the more currently favored object-oriented languages.