Sunday, September 12, 2010

Java simplified




Java programs are either Applets or Applications. The first one runs under an
HTML environment in a browser window, the later runs standalone like we
are used to thinking about how programs run. Most of the commands and
features available to one are available to the other. Obviously Applets, since
they are running on someone's computer anywhere in the world must have
some restrictions (e.g.. cannot modify files on someone's computer)

Most of the standard commands and control statements in Java are quite
similar to those of other languages. Assignment statements, if statements,
for-next statements, Do - While statements, etc. are easy to understand and
pretty similar in most languages. However , using these statements to
accomplish some logical end is the first stumbling block that beginning
programmers need to overcome.

Understanding the various data types both primitive and non primitive is
of major importance to Java programmers. Primitive types are pretty obvious
and used in most languages on an everyday basis. Such types are :
int long short double float char Boolean etc.

Non primitive types are referred to as reference variables because they point
to memory locations that contain the values. Strings etc.

In addition to the above another important learning step is how to organize
our program so that it will work. Concepts such as top-down, or styles
that use a modular approach are quite popular. Modular means to divide
the logic into sections of code and then call each section as it is needed.
Think of modules are being small programs. These small programs can
also be called function or even METHODS. Remember that when you use
them , their purpose is to perform some task. Understanding the concept of
Methods is critical to appreciating the power of Java. In addition to Methods
performing tasks (just like a program, they can receive values (to play with)
and return results (if needed).

So in short programs are groups of Methods and variables interacting. In Java
we will refer to this grouping as CLASSES. So instead of writing programs
we are really writing CLASSES. And each of our Classes contains
METHODS (or member function) to perform specific tasks.

Now the real power explained. People have written many CLASSES already.
These CLASSES perform a wide range of tasks for you , so you in essence
do not have to reinvent the wheel every time you need something. These
CLASSES are bundled together into PACKAGES , which you import into
your program (at the top). Common packages are java.awt or java.lang or
java.applet etc. New CLASSES are being developed all the time. Bundled
into packages and made available to the programmer. (java.swing is an
example from our textbook). So in short you are not writing programs, you
are writing classes that can use existing classes to help them achieve their goals

When your class needs something that another class has to offer you can
normally do one of two things after the import of the package containing
the class:
1.. directly refer to a member function in that class to perform the task
String name = JOptionPane.showInputDialog("enter your name " )
2.. create an OBJECT of that class and then refer to the object along
with the member function so the task can be performed
Button red = new Button();
.... red.setBounds (1,2,44,23);

In summary the full power of java and object oriented programming is
realized when our class creates objects of existing classes which were
created from other classes. Classes can inherit the capabilities and power
of the classes from which they were derived and so on and so on.
So not only are we not reinventing the wheel , we are pulling a wheel of
the shelf that is already fine tuned and using that to go forward to design
yet even better classes to solve problems.

When designing our own classes there are way to allow access to certain
methods and variables and not allow access to others. In other words we
allow enough access to realize its power but not enough to mess it up.
I will spend more time on these and other concepts later.

No comments:

Post a Comment