Getting Started with OOP and Java
members-aol-com-whitelegg
By Lon Hosford
 
This is your first week for Computer Programming I. By now you discovered that this course targets learning about Object Oriented Programming (OOP) using the Java programming language.
 
OOP Languages
Java is one of many languages that are considered OOP languages. C++ is also an OOP language. Visual Basic is another. Have you ever heard of Smalltalk? Object oriented programming was added to Perl in version 5. PHP has OOP.
 
The degree to which each of these languages implement the concepts of OOP varies. C++ implements more OOP than does Java. Smalltalk claims to be a "Pure Object Environment and Programming Language". Visual Basic has added a few new OOP features each version since version #4.
 
OOP Concepts
There are a number of concepts related to OOP. The text covers them briefly starting on page 3. The best way I have learned OOP concepts is to write simple code examples implementing these concepts. You can read about the OOP concepts of encapsulation, inheritance, interfaces, and polymorphism, but until you apply them, it is sleep medicine for sure.
 
You also should look at http://java.sun.com/docs/books/tutorial/java/concepts/index.html for a perspective on OOP from the authors of Java.
 
Java and OOP Concepts
Fortunately for you, Java is a very popular OOP language and it implements OOP in a programmer friendly way. You can learn all the OOP concepts with Java.
 
Java and the Sun
Java was invented by Sun Microsystems. Java is so popular that Sun maintains a separate web site for Java: http://java.sun.com. This week's discussion will have you explore the Java web site.
 
From Programming Toasters to Glory
Java is a relatively new language on the block. The original idea behind Java was to have the same program work on many machines. Literally they were thinking about smart machines like toasters and light switches as well as computers.
 
Little Applications
The idea was such that the Netscape people working in partnership with Sun saw an opportunity to have Java run inside their web browser. They called these programs applets for "little applications". The applets became popular because the offered the web programmer the ability overcome the limitations of web page HTML at that time.
 
When Java was introduced to the Netscape web browser, most of the implementations were limited but entertaining. A text scroller was popular (now doable in Javascript). I added a Java hang person game tied to my IT dictionary to one of my web sites. Now that is better done in Flash. These little Java applications created some large files sizes for typical modem bandwidth standards at the time, but we felt they added to our web sites a presentation level attractive to our visitors.
 
In the long run Java in a web page proved limiting due to code size, uniform browser implementation problems and newer alternatives that proved more efficient. Now Java is thrives for server side Web applications with small client side parts of those applications.
 
Yet the applet brought Java into the forefront as the programming language for the Internet when the Internet was a hot bed of growth for the IT world.
 
The Java Compiled and Interpreted
Most programming languages are interpreted or compiled. BASIC is interpreted. Javascript is interpreted. PHP is interpreted. C is compiled. C++ is compiled.
 
Java is both compiled and interpreted.
 
Interpreted languages
Interpreted languages take the source code and convert it to machine code as each line is processed. If the same line is processed more than once, this conversion may need to be repeated. An interpreter program runs to handle this task. A intepreter needs to exist for each type of platform.
A platform is the operating system and cpu (central processing unit) together in this discussion. Example Intel Pentium III and Windows 98. Sun UltraSPARC® IIi cpu and Unix.
For interpreted languages often there are differences in the source code between platforms. BASIC is the primary example. The BASIC interpreters were often written by the platform manufacturers and so the source code expected by these interpreters varied.
 
Compiled languages
A compiled language's source code is converted to machine code by a compiler program one time. The computer then uses that compiled machine language. A compiler program must be written for each platform and the source code needs compiling for each platform. In other words the platform on which you compile the code is the platform on which you run the code.
 
To deploy compiled languages on multiple platforms requires multiple copies of executable machine code.
 
Multiple copies of source code also can be required because of differences in the compilers on platforms.
 
Think about it for C++ and two target deployment platforms. Compiler for Windows on Intel Pentium cpu. Compiler for Sun UltraSPARC® IIi cpu using Unix. Source code for each because the compilers have differences. Separate copies of the executable. Even worse, there are many flavors of Unix as I simplified for the Unix platform.
 
Write Once for the Java Virtual Machine
Java takes the write once deploy many approach. There is one source code. The compiler converts the source code into what is called byte code used by all platforms. Byte code is a file format that an interpreter type of program would understand.
 
The interpreter is called a Java Virtual Machine (JVM). Each platform requires a JVM program developed. The important feature is that the JVM handles the platform differences using the same byte code files. As a result one source is all that is needed and only one compile is all that is needed.
 
If a platform does not have a JVM, then the vendor of the platform is saying Java does not run here. The marketing power of Java results serious platforms having a JVM written for it.
The early days of web browsers, the JVM was built into the web browser. Now the JVM needs to be downloaded from the Java web site and installed into the web browser. This is done with an HTML tag and is at the discretion of the web browser owner.
Programming In Java: You Already Started and Did Not Know It
Java is a programming language that exhibits all the elements of any programming language with the addition of OOP concepts and an extensive programming library called the Java API (application programming interface). This means that you have data types, variables, constants, selection statements, repetition statements, array type variables, functions or procedures both called methods in Java and the ability to accept input and display output. The latter input and output are actually part of the API.
 
Many of the Java language features are identical to other languages. Some have minor variations.
 
Programming In Java: What Do You Need?
Sun provides a developer kit called the Java Development Kit (JDK). It is free. There are levels of the kit and you only need the standard version which is on a CD that comes with the course text and is equally obtainable at the Java web site. Be sure you have the kit for your platform.
There are companies that provide Java development kits at a fee. These are more robust in how they help the programmer work. In 2001 many companies producing Java development kits folded following the dot com collapse and Microsoft no longer supports Java. IBM helped the fallout a bit by providing their Java development software tools free to purchasers of IBM servers.
You need a text editor as well. In using Windows, the Notepad program does the trick, but you may want an editor that is more tuned to a programmer like Textpad or Homesite particularly for the colored syntax highlighting. The companies making Java development kits have a professional programmer text editor included.
 
In addition to the course and its materials, you also need access to the Java API documentation. This is accessible from the Java web site with a web browser as well as downloadable from their web site. Do not hurry to download the documentation until you find you need to access it frequently. Most of what you need is in the course text.
 
Hello World From Java
I know when I learn a new language the first goal I have is to know how to do anything with it and that usually that means a "Hello World" program. In Java you will want to learn how to do a stand alone "Hello World" program and a Web browser "Hello World" program or applet. We will start with stand alone Java programs and the last week we turn our attention to web browser applets.
 
A Hello World program essentially provides you with the minimum template of a language. So that is a good place to start: knowing the minimum template for a Java program, how do I prepare it for running and how do I run it.