Contributors

Sunday, July 8, 2012

Class & Object

Simply an object is a software bundle of variables and related methods. In real world objects have 2 characteristics; state and behavior. A human being can be taken as an object in the real world and that person have state (name, color, hungry) and behavior (talk, eat, write) Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods
Object can be refers to an actual instance of a class. Every object must belong to a class. Objects are created and destroyed within the execution of the program.

  • Objects provide the benefit of modularity and information hiding.

Simply a class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kindUsing object-oriented terminology, can say that a humanBeing object is an instance of the class of objects known as Human. humanBeing have some state  (name, color, hungry)  and behavior  (talk, eat, write)  in common. However, each  humanBeing state is independent of and can be different from other   humanBeing
Class can be refers to the actual written piece of source code in which the class is defined. The properties of the class does not change before, during or after the execution of the program.

  • Classes provide the benefit of reusability.  Software programmers use the same class, and thus the same code, over and over again to create many objects.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p;
/**
 *
 * @author DELMI
 */
public class Human {
    void talk(String s) {
        System.out.println("Human can talk " + s);
    }
    String skin(String s){
        String skinColor = s;
        return skinColor;
    }
}
class perform {
    public static void main(String arg[]) {
        //create reference variable of Human h1 & h2
        Human h1 = new Human();
        Human h2 = new Human();
        //initialize languages e & s
        String e="English";
        String s="Sinhala";
        //call the methods using reference variables
        h1.talk(e);
        h2.talk(s);
        //print the reference variales h1 & h2
        System.out.println(h1);
        System.out.println(h2);
        //verify that a new object can be referd by the existing refernce variable
        h1=new Human();
        System.out.println(h1);
        //verify that the class methods are not perform untile it is called
        String b="Black";
        String w="White";
        Object o= h1.skin(b);
        System.out.println("The color of skin is "+o);
    }
}
//:::::::::OUTPUT:::::::::
//Human can talk English
//Human can talk Sinhala
//p.Human@19821f
//p.Human@addbf1
//p.Human@42e816
//The color of skin is Black
 
Read More - SCJP Sun Certified Java Programmer Study Guide, Chapter 2 Object Orientation by  Kathy Sierra & http://www.iam.ubc.ca/guides/javatut99/java/

Saturday, July 7, 2012

Why Java is Platform Independent


All accept Java as platform independent language and Java is popular because of it. But how Java becomes platform independent.

  • When we write a code in Java, the source code is complied by the Java compiler (javac). Java compiler converts the source code into universal byte code or to a ".class" file that is standard for all platforms, machines or operating systems.

  • There can be any number of JVM but they all understand the one common language called universal byte code and translates them into the binary executable of a particular platform for which they are developed. 

  • Java is an interpreter. This interprets the ".class" file based on a particular platform and executes them.

  • Java Virtual Machine (JVM) comes into play. JVM for windows will be different from JVM for Solaris or  Linux. But all the JVM take the same byte code and executes them in that platform.


Java Source Code ==> Java Compiler ==> Universal Byte Code ==>Java or JVM ==> Execution

There is separate development section in Sun Microsystems to develop the JVM for a particular platform. For each upgrade of the JDK, the JVM for each platform is updated.

Read More-Chapter 2 of Inside the Java Virtual Machine, Platform Independence by Bill Venners