aspose file tools*
The moose likes Beginning Java and the fly likes Why cant we declare the size of Array in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why cant we declare the size of Array in java?" Watch "Why cant we declare the size of Array in java?" New topic
Author

Why cant we declare the size of Array in java?

prathamesh bandivadekar
Ranch Hand

Joined: May 22, 2008
Posts: 43
Hi All,

Can anyone tell me that why dont we write the size of array in Java?

I would like to know JVM level answer.

Plaese help me out with this.

Thanks and Regards,
Prathamesh.


Prathamesh M.B.<br />Java....only.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

Aaaaahhhh.... What are you talking about?

How do you expect Java to provide you with an array object, if you don't declare the number of elements that you want? Or are we talking about something else?

Henry
[ August 28, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi,

Memory is not allocated at the time of declaration.
eg: int[10] arr; -- not allowed

its only when new operator is encountered memory is allocated.
Therefore int[] arr= new int[0]; is valid.
prathamesh bandivadekar
Ranch Hand

Joined: May 22, 2008
Posts: 43
No But !!!

It is given in the book, that whenever we declare array in java we cant give size of that array?

it sayz like this :

int[2] array; // is wrong

int[] array; // is correct

This is my question.
Adinath Shirsath
Ranch Hand

Joined: Dec 04, 2007
Posts: 35
you are creating reference of array object

do like this

int[] arr= new int[2];
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

It is given in the book, that whenever we declare array in java we cant give size of that array?

it sayz like this :


This is because you are *not* declaring an array. You are declaring a reference to an array. The size of an array is with the array object, and a reference can refer to any size of array. So there is no need to tell the compiler what size it will be used for.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11241
    
  16

I think a lot of people have a disconnect on what exactly java does with variables and objects (i know i did for a long time).

Java actually creates TWO things most of the time - the object, and a reference to the object. so when you say

int[] arr = new int[2];

you are creating a reference, called "arr", and an integer object somewhere that does not have a name that lives in memory somewhere. The '=' character kind of wires them together.

I think of it like the left hand side is an address card in a rolodex, or a piece of paper. The right hand side is a house. I can write any address on the paper, or i can leave it blank.

saying:
int[] arr;

is just grabbing a blank piece of paper you want to carry around, until you find a house who's address you want to remember. the paper doesn't know, care, or even need to be aware of how many bedrooms the house has. Once you create the 'house' with five bedrooms via the "new int[5]", you can now write the address on the paper.

so the reason "int[2] array;" is wrong is because the reference doesn't know about the size of the array... it just needs to know it will point to an array that holds 'int's.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

One more think I observe, When a programmer has a strong background of C and C++,
the code :

int arr[5];

seems to be perfectly legal and good to him, but in Java arrays are handled as object so new has to be ther to make it sense ..

Everything else is explained by Experts , Thanks to all !
[ August 28, 2008: Message edited by: Sagar Rohankar ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sagar Rohankar:
One more think I observe, When a programmer has a strong background of C and C++,
the code :

int arr[5];

seems to be perfectly legal and good to him, but in Java arrays are handled as object so new has to be ther to make it sense ..


Mhh, I think the *true* difference is that in Java all objects are created dynamically on the heap, whereas in C++, the above would denote an array that is stored on the stack (assuming that it declares a local variable). With other words, in C++ you actually can declare a variable that directly holds an object, instead of "just" holding a reference to an object on the heap.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

When teaching Java, I often find that it is easier to understand why we have to do some things by translating the code to sentences.

If I write:


that would translate into "Create an integer array reference with the size of two and call it integerArray and assign a new integer array with the capacity to hold two integers to it". And that sounds stupid...

If I write:


that would translate into "Create an integer array reference and call it integerArray and assign a new integer array with the capacity to hold two integers to it". That sounds a lot better.

It becomes even more clear if I write:



and translate it to "Create an integer array reference and call it integerArray. Assign a new integer array with the capacity to hold two integers to the reference called integerArray. Assign a new integer array with the capacity to hold 42 integer to the reference called integerArray."

But in the end, I find that using List, ArrayList and other classes in the Collection-area is easier than messing with atomic arrays...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cant we declare the size of Array in java?