Posted in Uncategorized

Java Post #1 Java Diving o (≧ ▽ ≦) o

┬┴┬┴┤・ω・)ノ

for all those Engineers who have at least half a brain and no interest in a 3 inch thick textbook…

Selamat siang!
(Greetings in Indonesian…)
Let’s Learn Java!
.
.
.
What can one possibly know with regards to Java?
Java is part of Indonesia,(hence the greetings…) a nation with more people than France, England, and Germany put together, located northwest of Australia.  And their history isn’t filled with nearly as much stupid squablings as those of the Europeans!  Java is the most flamboyant island of Indonesia (there are 3 larger islands, but they don’t have as much going on), and the capital Djakarta is the most exuberant city on Java.  All the major cities are linked with a well developed rail and highway network, so you can get around just fine.  Java is one of the world’s most densely populated areas, and has 35 active volcanos to boot.

Of course, if the next popular programming language is called “England”, I’ll work to educate you about that island as well, even though England is relatively boring, having far fewer people, square miles, and active volcanoes than Java.

Does Java make you think of coffee?  Coffee is indeed a major export, along with tea, tobacco, sugar, coconuts, and cinchona.  But the main food crop is rice, along with corn, soybeans, peanuts, sweet potatoes, and cassava. Did I just hear roar from my stomach?

◥█̆̈◤࿉∥ (⋆‿⋆)
Do you equate Java with a cup of coffee?  Time to wake up and smell the real Java!

.
.
.

(⌐■_■)


Okay so the Java’ian Language
Java is a fairly simple programming language (though it has extensive standard libraries unlike Python).
A programmer writes .java source files, which get compiled by javac into .class files containing Java ByteCodes instead of CPU-specific machine code. The .class files are run by a Java virtual machine (interpreter) (e.g. someone’s browser) at run time.(I understand,Utter Gibberish…)

( ̄^ ̄)ゞ

Java programs are just sets of classes.Classes are like a template with properties and functions that gives us something to work with…I’ll get to that soon…
A Java Applet is a program that is referenced by a web page, and run by a Java interpreter in your browser when you browse that page. Any time a class is needed, the browser will load the .class file (probably over the internet) just once (then it is cached), after which point it can make instances of the class as often as it likes.
An applet is not the main program — it just responds to various UI (user interface) events, like the page being loaded by the browser, or a mouse click. Various routines are called when various events happen, so there are typically many “points of entry” into an applet.
JavaScript is a completely separate language, originally named LiveScript, but renamed to JavaScript by its creator, Netscape, somewhat early on.
You can include JavaScript source code in an HTML document.  I will not discuss JavaScript here. JavaServer Pages (.jsp) are a way of embedding Java code into HTML-ish pages.  I will not discuss JavaServer Pages here either, although probably I should and maybe I will…
I think that’s all there is to mention with respect to the word ‘JAVA’…Now let me start what I was actually planning to post…
.
.
.

Object-Oriented Jargon (OOJ)
Remember, the purpose of jargon is to separate the “in” people from the “out” people. Jargon allows you to speak in a special way so that only “in” people will understand you.
It is important to denounce anyone unfamiliar with the jargon as ignorant and not worth listening to. Proper use of jargon terms will allow people to identify you as a knowledgeable expert, especially to the extent that they denounce you incomprehensible…

(-‸ლ)

It is also important to claim that the jargon enables you to think properly, and to assume that people unfamiliar with the jargon couldn’t possibly understand the concepts that the jargon enables you to talk about.
So let me give you a heads up on the frequently used Jargon’s..Before you end up deeming this incomprehensible…*sigh

  • In an object-oriented language, functions (subroutines, procedures) are called methods. Functions are a set of executable code that is defined under a name..

  • The act of calling (executing) a function is called invoking the method.

  • A class is a collection of methods, and a collection of variables.

  • Like a struct in C, or a record in Pascal, the variables exist as a group for every instance of the class…(Its okay if you don’t get it…) Each occurrence of the struct/record (each region of memory which contains a set of the variables) is called an object. Wait, didn’t I just mention it’s called an instance?  Yes, an object is an instance of its class.
To know the class of the object … is to know what struct/record/(set of variables) it has, and what methods it can perform…
What, how can a set of variables perform anything? Ah, Now that is the crux of “object-oriented phenomena“. All code is thought of as being executed by some object or another.
Here is some simple Java code:
class Point { // We are defining a class called “Point”
    int x, y, z; // It has three integer instance variables
 // (every Point instance will have its own values for these variables)
    void translate(int dx, int dy, int dz) {  // here is a 3-argument function “translate”
      x += dx;      // this function adds (dx,dy,dz) to (x,y,z)
      y += dy;
      this.z += dz; // “this.z” is exactly the same as “z” specific to the variable in method containig class
    }
}
Every method in a class has a secret unlisted argument, called this, which is the group of variables declared in the class. For example, the above function translate somehow has to know which point it is translating. The caller must tell it which point to translate. Any call to translate must be with regard to a specific instance of the Point class.
        Point p1;    // this says the variable p1 can hold a Point
                         // but it doesn’t hold anything yet (it is null)
        p1 = new Point(); // this assigns a newly created point to p1
        p1.x = 3;
        p1.y = p1.x + 5; // we can work with instance variables like this
        p1.z = -4;
        p1.translate(2,2,2); // we can invoke the translate method on p1
The last line indicates that when translate is called, the implicit argument this will be equal to p1. We say that the point p1 performs the translate method.
If the performer of a method is not specified, it is assumed that this should perform the method, so:
              translate(0,3,0);
is the same as:
              this.translate(0,3,0);
Of course, this only makes sense (is compilable) if *this* is a Point.
Class Hierarchy
Classes are arranged in a hierarchy (a tree, to be specific). When you make a new class, you have to say what it is a subclass of (what its super class is). Object is the class at the top. Every other class is a subclass of Object, or a subclass of a subclass of Object, or so on.
A subclass automatically inherits all the variables and methods of its super class. It may also define new variables and methods. If it wishes, it can override (redefine) some of the inherited methods.
Here we define a subclass of Point.  We say that we are sub-classing Point:

class ColoredPoint extends Point {
    int colorNumber;
    void changeColor() {
        colorNumber++;
    }
}

Then we could have code like:
        ColoredPoint cp;
        cp = new ColoredPoint();
        cp.x = 4;
        cp.y = 5;
        cp.z = 6;
        cp.colorNumber = 3;
        cp.translate(3,0,0);
        cp.changeColor();
Kind Of

We see in the above example that a ColoredPoint is a kind of Point. And in fact, any subclass of Point (or subclass of subclass of Point, etc.) is a kind of Point.Just a statement that might help you to sort out things in future..
An Advanced Topic:  Interfaces
Sooner or later (probably later), we will inevitably want to say that a class is a kind of two different things.

(◕ᴥ◕)


For example, a Chair might be a kind of Furniture, and it might also be a kind of ComfortablePlace.
How can we do this?
One way this could be is 

  • If Furniture is a subclass of ComfortablePlace, 
  • Then Chair could be a subclass of Furniture, 
  • And it would then be both a kind of Furniture and a kind of ComfortablePlace.
But this is conceptually wrong, since we would certainly want to allow other kinds of Furniture which are not a kind of ComfortablePlace, for example a Shelf.
We do not want to force ComfortablePlace and Furniture to have any particular relation to each other just because Chair happens to be a kind of both.
Some object oriented languages, like C++, allow multiple inheritance, meaning that a class can have more than one super class.
Other languages, like Objective C and, in particular, Java, do not allow multiple inheritance, so we will not discuss it any further!

ヾ( ̄◇ ̄)ノ〃

Java’s solution to the problem is the Interface.

  • An interface is like a class, except that it has no variables.
  • It just has a list of methods, and the methods don’t even have any code!
  • An interface doesn’t have any instances.
So what good is it?
A class can be declared to implement an interface:
class Chair extends Furniture implements ComfortablePlace {
    boolean hasArmrests;
    boolean canSwivel;
    int legs;
    boolean occupied;
    boolean occupy() { // return whether occupation attempt succeeds
        if (occupied)
            return false;
        occupied = true;
        return true;
    }
    boolean leave() { // return whether attempt to leave succeeds
        occupied = false;
        return true;
    }
}

Here is what the definition of the ComfortablePlace interface might look like:
interface ComfortablePlace {
    boolean occupy(); // return whether attempt succeeds
    boolean leave(); // return whether attempt succeeds
}
The compiler, upon seeing that class Chair is declared to implement the ComfortablePlace interface, will check that this is indeed so.
Now, just as we declared the variable cp above to be of type ColoredPoint, we can declare a variable to be of type ComfortablePlace.
Then the variable is allowed to hold any object that is of a class implementing ComfortablePlace, and we can invoke any method from the ComfortablePlace interface:
ComfortablePlace cp;
cp = new Chair();
cp.occupy();
In this way, a Chair is both a kind of Furniture and a kind of ComfortablePlace.
( ^▽^)ψ…

Code
Java code is pretty much nothing but classes. There are no global variables or precompiler.
Code is written in .java source files (typically one file per class, with the same name as the class), and compiled into .class class files.
For example, here is a “hello world” program, written as a file HelloWorld.java:
These are like #include statements in C, specifying what libraries (or, more generally, what other classes) will be used.
They must appear before any class definitions.
    import java.applet.Applet;
    import java.awt.Graphics;
This says we will define a class called HelloWorld, as a subclass of the already-defined class Applet
    public class HelloWorld extends Applet {
Instance variables would go here (or even class variables, indicated with “static”), but we don’t happen to have any.
Here we define a method called paint, taking an argument g of type Graphics (that’s why we included java.awt.Graphics above)
       public void paint(Graphics g) {
The body of the method is just to call g’s drawString method (defined in java.awt.Graphics)  with some simple arguments
          g.drawString(“Hello world!”, 100, 30);
(0,0) is the upper left corner.  That starts drawing the string at (100,30).
       }
    }
To use this applet in a web page, we would use the following html:
If this here html is rendered then your browser can’t run applets, perhaps because an option to enable java is not selected.
This tells the browser that the file “HelloWorld.class” contains a class HelloWorld which must be a subclass of Applet so that it can respond to all the messages that the browser will send it.  (Applet is in turn a subclass of Panel, then Container, then Component, which can draw and get events.)
***We have overridden only the paint method.***
If you write a Java standalone program (instead of an Applet), then you will use main(), like in C.
And for applets, you do not need a main().
Instead, your Applet subclass (specified in the html) will be sent messages such as start(), stop(), paint(), etc.
These might be discussed later…If I remember…

(シ. .)シ

Meanwhile… here is a list of keywords to strop your already degrading brain…
Keywords

φ(*⌒▽⌒)ノ

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s