• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why cant we declare the size of Array in java?

 
prathamesh bandivadekar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are creating reference of array object

do like this

int[] arr= new int[2];
 
Henry Wong
author
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic