Two Laptop Bag*
The moose likes Beginning Java and the fly likes arrays - different size 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 "arrays - different size" Watch "arrays - different size" New topic
Author

arrays - different size

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Why does this code compile and even run ? Arrays are of different size!



Regards,<br />Arnab
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

The following line of code...



merely assign the finalvalue reference to the same array object referenced by the initial variable. It does not do an array copy. After the operation both variables will reference the same array object.

Since both are int array varables, the assignment is legal.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The key is that the variables "initial" and "finalvalue" are both of the same type: int array. They can reference any int array assigned (or re-assigned) to them.

The size of an array has to do with the object created -- not the variable it's assigned to.

Here, an int array is instantiated to length 3, and another int array instantiated to length 2. Initially, references to these objects are assigned to the variables "initial" and "final" value respectively. But there's no reason these can't be switched.

However, in your code, I think you'll find that both of these variables end up referencing the same object. Can you see why?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145


If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the refrence you're assigning it to.



So I do not understand the above statement ? The code seems to work differently.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,

If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the refrence you're assigning it to.


I think here the same dimension means with respect to 1-D, 2-D and so on. It does not refer to the size of the array as in your case the arrays are of size 2 and 3 respectively BUT the important thing to remember is that both have the same dimension.

I really hope that what I have said is right. If not, please correct me.


With Best Regards,
Shyam Prasad Murarka
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi Shyam,

Now that you mention it... it does look that you are correct!

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays - different size
 
Similar Threads
Java Rule Round-Up
Putting Binary Search Tree into an array
A simple question
Javaprepare Question
inhertiance question