• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Difference between array of references and single reference

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls follow my code:

My first Program with single reference


And my Second Program with Array of References:

I understood, why that errors occured.

My main question is what is exact difference between array of references and single reference?
Why that first program giving error at compile time and second one at runtime.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry my Speed Post... I got the answer.. is it exact to my problem

In the Java programming language arrays are objects (�4.3.1), are dynamically created, and may be assigned to variables of type Object (�4.3.2). All methods of class Object may be invoked on an array.


Comments open....
 
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly...

The issue is this.

Program #1 declares the variable, but doesn't associate an instance with it. The compiler edits are designed to catch this. This variable has local method scope.

Program #2 declares the variable and instantiates the object array to a size of 5. While the array variable refers to a valid instance of the array. The contents of the array which should refer to instances of the objects of the type declared, were never created/initialized. An array is simply a collection of references the references may or may not refer to a valid instance on the object of the collection type. As a result, if it's possible that you will get unitialized instances in your array/collection it's usually a good idea to check that the item is not null before you make any method calls on it.
[ May 18, 2005: Message edited by: Byron Estes ]
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help...sorry Still i am Unclear...please give a easy explanation...
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare a non-primative variable (i.e. object), it's capable of pointing to an instance of the object type declared.

Until you instantiate an instance of the object using the "new" keyword and assign it to the variable, the variable is null.

In the case of an array, you declare it and you instantiate it. So far so good. The variable points to an instance of the array type you declared.

The DIFFERENCE here is that an ARRAY IS A COLLECTION OF VARIABLES, you simply access them via an index (i.e. objects[1]). When the array is instantiated it doesn't fill all the variables/buckets it contains. It simply provides you with variables capable of receiving/holding pointers to the instances.

Whenever you are using an array of objects you usually go through a 3 step process where you declare it, instantiate it and you fill it with instances of the type it was declared to hold.
[ May 18, 2005: Message edited by: Byron Estes ]
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for Ur time for me
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem.

Good Luck!
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The meaningful difference in the two situations is that the values in an array (whether they are primitives, as in "int [] x", or objects, as in "String [] x") are assigned a default value, just the same as instance variables. Method-local (automatic) variables are not.

In the case of the array, the array variable arr is a method-local variable - and therefore must be initialized before it is accessed, or a compiler error will be found.

But once the array is instantiated, the individual elements within the array need not be initialized - they get their default value, which is null for object types, 0 for numeric values, false for booleans, etc.

I broke out one of the lines in your example to show the distinction:



Another thing that might help is to think of reference variables as having three states:

1. declared, but not initialized - gives the "might not have been initialized" compiler error when you try to access it

2. declared and initialized, but null - gives "NullPointerException" runtime exception when you try to access it (apply the dot operator on it).

3. declared and referencing an object (not null) - everyone's happy

Instance variables and array elements get to skip state # 1 - they are automatically initialized to null. So you can't ever get a "might not have been initialized" compiler error.

Local variables (like your "A s" in your first program) can be in all three states.

Note, state 2 doesn't apply when you're talking about a primitive, which can't be null. It's either uninitialized (if it's local and you have not assigned a value), or set.

Hope this helps.

-- Jon
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic