Sunday, December 8, 2013

Object in JAVA : Day 2

a) What is Object? How to declare an Object in a JAVA program? Explain the Syntax of Object Declaration.

b) Write Syntax to assign an Object Reference Variable.


  1. a))) What is Object ?
    >> Objects are key to understanding object-oriented technology. Real-world objects share two characteristics: They all have state and behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).
    *** How to declare an Object in a JAVA program ?
    >> Declarations can appear as part of object creation as you saw above or can appear alone like this
    Date today;
    Either way, a declaration takes the form of
    type name
    where type is either a simple data type such as int, float, or boolean, or a complex data type such as a class like the Date class. name is the name to be used for the variable. Declarations simply notify the compiler that you will be using name to refer to a variable whose type is type. Declarations do not instantiate objects. To instantiate a Date object, or any other object, use the newoperator.
    *** Explain the Syntax of Object Declaration.
    1.Every object used must be declared
    [class name] [object name];
    *[ class name] // name of the class where there is object //
    * [object name] // name of the object (have to valid identifier)
    Identifier: Any sequence of letters, digits, underscores, and dollar signs. But there should be following limitations:
    a) Must begin with a letter.
    b) Cannot contain any spaces or other “white space”
    c) Cannot be a Java “reserved” word.
    Reserved Word: An Identifier that is used for a specific purpose and cannot be used for any other purpose.

  2. b))) Write Syntax to assign an Object Reference Variable.
    >> Objects can be referenced by a variable too. The syntax of the object variable declaration is the same as it is for primitive variable declaration:
    Class-Name variable-name
    where Class-Name is the name of any class (remember, in Java class names are always capitalized) and variable-name is a valid Java variable name.
    Object variables hold the address of the object to which they refer. The address of an object is also called a pointer to the object.

  3. a) What is Object? How to declare an Object in a JAVA program? Explain the Syntax of Object Declaration.

    Ans :

    objects :

    objects are the basic run time entities in an object-oriented system.They may represent a person , a place ,a bank account , a table of data or any item that the program has to handle.They may also represent user-defined data such as vectors , time and lists . Programming problems are analysed in term of object and the nature between them . Program objects should be chosen such that they match closely with the real world objects.
    When a program is executed , the objects interact by sending messages to one another . For example , if "customer" and "account" are two objects ina program then the "customer" object may send a messege to the "account" object requesting for bank balance. Each object contain data and code to manipulate data. Objects can interact without having to know the details of each others data or code . It is sufficient to know the type of message accepted and the type of response returned by the object.

    How to declare an Object in a JAVA program :

    When we create a class , we are creating a new data type . We can use this type to declare objects of that type . Obtaining objects of a class is a two-step prosess . First , we must declare variable of the class type . This variable dose not define an object . Instead , it is simply a variable that can refer to an object . Second , we must acquire an actual , physical copy of the object and assign it to that variable . We can do this by using the new operator . The new operator dynamically allocats memory for an object and return a reference to it . This reference is , more and less , the address in memory of the object allcated by new . This reference is then stored in the variable . Thus , in java ,all class objects must be dynamically allocated.

    Explain the Syntax of Object Declaration :

    class classname
    data type mathodename(parameter-list)
    //body of methode;


    public class classname {

    public static void main(String[] args) {

    classname b; //Declare reference to object

    b = new classname(); //Allocate a an object



  4. b) Write Syntax to assign an Object Reference Variable.

    public class Box
    public double calvol(double width,double heigth ,double depth)
    return width*heigth*depth;


    public class Ob {
    public static void main(String[] args) {

    Box b1 = new Box();

    Box b2 = b1;

    double vol;

    vol = b1.calvol(11,12,14);

    System.out.println("The volum is : " + vol);

    vol = b2.calvol(19,15,14);

    System.out.println("The volum is : " + vol);



    Here , we might think that b2 is being assigned a reference to a copy of the object refered to by b1 . That is we might think that b1 & b2 refer to separate and distinct objects . But the concept is wrong . Instead , after this fragment executes b1 & b2 will both refer to the same object . The assignment of b1 & b2 did not allocate any memory or copy any part of the original object . It simply makes b2 refer to the same object as dose b1 . Thus , any changes made to the object through b2 will affect the object to which b1 referring , since they are the same object.

    Although b1 & b2 both refer to the same object , they are not linked in any other way . for example in the program there are two class variables b1 & b2 . They are passing different values as arguments and if we print them the output will be two different results . This proves both b1 & b2 have no relation to each other , although they are refer to the same object.