Wednesday, December 15, 2010

Lesson4 - Some tips

  • Java is case sensitive. Syntax words need to be declared or written in the required cases. For eg. String has to be written in INIT CAPs. 'if else' statements the words have to be in lower case.
  • '+' is the string connector operator.
  • Every application has to have a main method.
  • PATH - is the system parameter where the JDK exists along with the other JAVA libraries which contain apis or methods that can be used in your programming. System.out.println is one such method.
  • CLASS PATH - is the system variable where the .class files are stored. For any method to invoke a method of some other class, both the classes need to be stored in the same class path.
  • public static void main - public : method has to be defined as public if you want other applications to call the method.
  • void - void signifies that the method doesn't return any data.
  • static - static signifies that the method can be invoked without object. Generally methods are called as object.method, but if declared as static then you can just invoke the method without the object.

Tuesday, December 14, 2010

Lesson3

BASIC CONCEPTS Of OOP (Object Oriented Programming)

Objects - You can say that Objects are representations of physical elements. For eg. Bicycle. A complete status or Objects will have properties and states. This is similar to objects in pl/sql; in pl/sql we define the objects in terms of the attributes or columns.

Bicycle states (current speed, current pedal cadence, and current gear).

Methods - Method is the process by which the states are changed. This is analogous to a procedure in pl/sql.

Classes - You can think of class as a collection of methods and object properties.
For eg. In the below Bicycle class pedal cadence, speed and gear are the properties/states.
changeCadence, changeGear, etc are methods to bring about changes in the
states or properties.
class Bicycle {         int cadence = 0;        int speed = 0;        int gear = 1;         void changeCadence(int newValue) {             cadence = newValue;        }         void changeGear(int newValue) {             gear = newValue;        }         void speedUp(int increment) {             speed = speed + increment;           }         void applyBrakes(int decrement) {             speed = speed - decrement;        }         void printStates() {             System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);        } }

Inheritance - In reality there are many kinds of bikes like mountain bikes, dirt bikes, etc ... Each of the above can be classified as a class in its own due to some unique features while some features would be same in all the bikes. Say the properties of the class Bicycle would apply to both MountainBikes and DirtBikes hence you would like to extend the properties and methods of class Bicycle to Classed MountainBikes and DirtBikes - this lending of a properties and methods of a class to another is called inheritance.

Syntax ...
class MountainBike extends Bicycle {       // new fields and methods defining a mountain bike would go here 
}

Interfaces - collection of methods without bodies. In analogy with pl/sql you can say its a package specification.
interface Bicycle {         void changeCadence(int newValue);   // wheel revolutions per minute         void changeGear(int newValue);         void speedUp(int increment);         void applyBrakes(int decrement); } 
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {     // remainder of this class implemented as before  } 
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Packages - A package is a namespace that organizes a set of related classes and interfaces.


  • Real-world objects contain state and behavior.

  • A software object's state is stored in fields.

  • A software object's behavior is exposed through methods.

  • Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.

  • A blueprint for a software object is called a class.

  • Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.

  • A collection of methods with no implementation is called an interface.

  • A namespace that organizes classes and interfaces by functionality is called a package.

  • The term API stands for Application Programming Interface.
  • Saturday, December 11, 2010

    Lesson2

    Code Snippet provided earlier -
    /**
    * The HelloWorldApp class implements an application that
    * simply prints "Hello World!" to standard output.
    */
    class HelloWorldApp {
    public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
    }
    }
    -------------------------------------------------------------------------

    Comments - text to be ignored by compiler
    have to written between /*

    'class' is like a package name in PL/SQL and is always a must. Begin and End of a class are denoted by curly brazes.
    class followed by application name is the general convention.

    Every application must contain a main method - this is similar to a procedure in pl/sql.

    public static void main(String[] args) {
    System.out.println("Hello World!");
    }
    Note the declaration - enclosed within the round bracket are the parameters passed to the method.
    String[] args -- is a declaration for an array of input strings; 'args' is just a name to the input array and not a key word.

    System.out.println("Hello World!") - syntax to print messages on the screen. println method exists in the System class and hence called using the mentioned syntax.

    That concludes Lesson2

    Lesson

    Suddenly Started learning Java . have been thinking to do this since long. I have decided to pen all my learnings and what better place than my own blog. Here goes the first day learnings.


    I am not using any IDE (some Development Environ.) hence have to go through the hassles of downloading JDK and setting the compiler path also called as CLASS PATH.

    JDK is the Java Compiler. You need to install the JDK in your machine.
    The source code files are to be saved with an extension .java. After compilation a new file (which is the executable) gets created and has the same file name as the source code file but has a new extension called .class. You can take this .class file to any machine and run your program, the machine need not contain your source code.


    After installing JDK you need to set the Class Path. Locate the JDK Bin folder and note/copy the path.
    Go to Control Panel > System > Advanced Tab > Environment variable; in system variables you will find a variable called path - edit this; to the existing text add a ';' and paste the class path.

    Now that the class path is set, you are ready for compiling your source code. Go to Command Prompt (Run > CMD) and traverse to the folder where you have saved your .java file. From here type javac and then hit enter. This should create your executable.

    Now to run the program. From command prompt type java then hit enter.

    Lesson1 Over.

    BTW here is the snippet of HelloWorldApp -
    /**
    * The HelloWorldApp class implements an application that
    * simply prints "Hello World!" to standard output.
    */
    class HelloWorldApp {
    public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
    }
    }