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.
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;
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", you can now write the address on the paper.
so the reason "int 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
Originally posted by Sagar Rohankar: One more think I observe, When a programmer has a strong background of C and C++, the code :
seems to be perfectly legal and good to him, but in Javaarrays are handled as object so newhas 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
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...
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad: