query in java

Discussion in 'Java' started by Amrita, Jul 20, 2004.

  1. Amrita

    Amrita New Member

    Joined:
    Jul 19, 2004
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    /*hi ,
    this is a query regarding Arrays in java. its an elementary one but i can't find it in any book.
    1.when returning an array frm a function, we just give the name...but how can we pass a locally created array like that? i think i am getting confused with C. please help me out.
    2. tell me why i am getting the runtime error in the foll. program. this is wht i am getting:
    C:\jdk1.2\bin>java Array
    Exception in thread "main" java.lang.NullPointerException:
    at Array.getarray(Array.java:8)
    at Array.main(Compiled Code)*/


    class Array
    {
    String s;
    Array[] getarray()
    {

    Array t[]=new Array[4];
    t[0].s="Amrita";
    t[1].s="Mitra";
    t[2].s="is the";
    t[3].s="Best";

    return(t);
    }


    public static void main(String args[])
    {
    int i=0;
    Array a=new Array();
    Array temp[];
    temp=a.getarray();
    for( i=0;i<temp.length;i++)
    System.out.println(temp.s);
    }
    }
     
  2. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    http://www.go4expert.com
    Hi Amrita ...

    1. In java apart from a few primitive types like int, float ... everything else are objects. Now you cannot directly access an object; you need to create an reference to it. While accepting and returning objects in methods we only actually accept or return object references, and references are "passed by value" between methods. That is, a copy of the reference to the same object is created and is passed. Thus you actually access the same object. ( In this respect it is the same as pass by reference of C++). If you want otherwise you have to explicitly create a separate copy of the object youself and then pass a reference to it.

    The concept is similar to C, where there is nothing like pass by reference. What many people call "pass by reference" in C ( which is a serious mistake) is actually pointers passed by value.

    Try to visualise objects and their references as separate entities. Then you wont have any problems to grasp the concept.


    2. There is a subtle difference between array or primitive types and array of objects.
    Array t[]=new Array[4]; .. After this line of code is executed, an array called 't' exists and has enough room to hold 5 'Array' objects. However, the array doesn't contain any 'Array' objects yet. It is empty. The program must explicitly create 'Array' objects and put them in the array. You are assuming that the previous line of code creates the array and creates 5 empty Array objects in it.

    When you are trying to access the t[0] object ( t[0].s = "Amrita" ) which is just null you are getting the run time exception.

    You can rewrite your Array class as :

    class Array
    {
    String s;

    Array() {}

    Array(String str) { s = str; }

    Array[] getarray()
    {
    Array t[]=new Array[4];
    t[0] = new Array("Amrita");
    t[1] = new Array("Mitra");
    t[2] = new Array("is");
    t[3] = new Array("Best");
    return(t);​
    }

    // the main remains the same ...​
    }


    or if you don't want to add a ctor you can initialize each object before you use it , like ..

    Array t[]=new Array[4];
    t[0] = new Array();
    t[0].s = "Amrita";
    t[1] = new Array();
    t[1].s = "Mitra";

    // .... and so on ..


    You can have a quick look at this link if you are still not sure what I mean :

    http://java.sun.com/docs/books/tutorial/java/data/arraysOfObjects.html

    Hope that helped. Do drop in a post if you have any further issues.

    Cheers,
    Amit Ray. :)

    Optimism is an occupational hazard of programming: feedback is the treament. (Kent Beck)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice