Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array" Watch "Array" New topic
Author

Array

catherine matthews
Ranch Hand

Joined: Jan 22, 2006
Posts: 78
Dear al,
i have confusions arrays as following please help....

int []m = new int[5];
m=null;// this compiles but caanot get any value...
// does this means that when i use null it clears the address pointing to that array m?

//m[1]=null;// why this cannot be given and i get a compiler error? why i can use null for object and not array? array is an object type right?
What is the value of array by default?
It follows the datatype given?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

"int" is a primitive not an Object. This array is an object that holds the references to your "int" primitives. The default value is 0. For arrays that hold references to Objects, the default value is null.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
you cann't do m[0] = null or m[1] = null because you have declared that this array contains int.so in this case you can assign only int values to m[0] or m[1].

why i can use null for object and not array? array is an object type right?

you are right arrays are objects in java but that object contains int values,so you can't assign null to them.

What is the value of array by default?

In your case,elements by default will be initialized by zero but if array contains objects then elements will be initialized by null
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

Raj Kumar Bindal I may have misunderstood what you ment by this,

you are right arrays are objects in java but that object contains int values,so you can't assign null to them.


but if you ment you cannot assign an array full of primitives to null you are wrong.

try:



This should work without a problem.

John
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
code which you have given will run properly but i mean if we have declared that an array will contain int elements,we can't assign null to the individual elements of that array.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
ah sorry, i misunderstood what you were saying.

Yep you are right a primitive cannot be set to null. You would have to use an array of Integer objects.

John
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Originally posted by catherine matthews:
Dear all,
i have confusions arrays as following please help....

int []m = new int[5];
m=null;// this compiles but caanot get any value...
// does this means that when i use null it clears the address pointing to that array m?

//m[1]=null;// why this cannot be given and i get a compiler error? why i can use null for object and not array? array is an object type right?
What is the value of array by default?
It follows the datatype given?


your "int []m = new int[5]" creates and array (which is an object). That array holds ints, which are not objects.

when you say "m = null", m no longer points to the object (the array). the object becomes eligible for GC (assuming there are no other references to it).

I'm not sure what you mean by "What is the value of an array by default?" if you mean the reference m, it refers to nothing until you assign it to something. You have done that with the "= new int[5]".

if you are referring to the members inside the array... that depends. if the things in the array are primitives (like int, char, long), they are set to 0.

if the array contains OBJECTS, or more precisely object references, they point to nothing until you assign them to something. This is a common error - to create an array of objects, but not actually put any objects into it, then try and reference them.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array
 
Similar Threads
Errors Trying To Compare Strings
How can I determine if a string array contains a particular string?
Doubt regarding Local array
emptying an array
Sun Cirtification