aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between array of references and single reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between array of references and single reference" Watch "Difference between array of references and single reference" New topic
Author

Difference between array of references and single reference

Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
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

Joined: Apr 21, 2005
Posts: 61
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....
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
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 ]

Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
thanks for your help...sorry Still i am Unclear...please give a easy explanation...
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
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

Joined: Apr 21, 2005
Posts: 61
Thanks for Ur time for me
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
No problem.

Good Luck!
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between array of references and single reference
 
Similar Threads
final variable
Class cast exception
array doubt
problem with initialization
Sun Cirtification