File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays doubt" Watch "Arrays doubt" New topic
Author

Arrays doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
source : k&b exam

class Test
{
public static void main(String[] arg)
{
int arr={1,2,3,4,5};
int arr2=new int[4];
arr2=arr;
System.out.println(arr2[4]);
}
}

My doubt is the first array that is arr has different length then the second one so how can we pass "arr" to "arr2"


Thanks All
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
arr is not an array, it is a reference! and as a reference it is of type int[] so that it can refer to any int-array, size doesn't matter in this context. That's the reason why it is forbidden to specify a size while declaring an array.

is forbidden, as a reference can refer to an array of any size.

[ May 17, 2007: Message edited by: Sasha Ruehmkorf ]
[ May 17, 2007: Message edited by: Sasha Ruehmkorf ]
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,as per my knowledge,this is your answer.
Array indices start with 0 so the first element is refernced with index "zero".In the array arr there are elements {1,2,3,4,5},so here the length of array arr is 4,and in the second array arr2[4] the index itself is given 4.Thats why we can assign arr2=arr.so both have the same array length.
i hope you must hav got it.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi dhwani,
Length gives you the size of the array. It doesnt give based on the index.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks for correcting my wrong explanation.
sandipan chakrabarti
Greenhorn

Joined: Mar 25, 2007
Posts: 6
first of all you cannot declare an int variable to hold an array
Next the array type is an object and the thing here we are assigning is
just a reference. As we know reference are just pointers to the memory location, so it does not matter whether it is an array of length 4 or length 5
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
Hi all,
i think we are forgetting on one thing. look at the code once more:
class TestCheck
{
public static void main(String[] arg)
{
int arr={1,2,3,4,5};//line1
int arr2=new int[4];//line2
arr2=arr;
System.out.println(arr2[4]);
}
}
line 1: doesnt follow the valid declaration of an array.
it should be
int [] arr = {1,2,3,4,5};
OR
int arr [] = {1,2,3,4,5};
line 2: again doesnt follow the valid declaration of an array.


for both lines the compiler will complain

i hope this is what you you were looking for.
please let me know if i am wrong or didnt get your question
thanks


debasmita
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Sorry the original code is


class Test
{
public static void main(String[] arg)
{
int[] arr={1,2,3,4,5};
int[] arr2=new int[4];
arr2=arr;
System.out.println(arr2[4]);
}
}

My doubt is : How can we assign one array to other array when the lengths of the array are different?

Thanks All
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,i do not have exact & correct explanation of your question .But what i know is array gets resized when two arrays of different length are assighned to each other.

Please refer to the below shown link may be what i am trying to explain is this.In this article you read the topic 1):"More on Assignments".
http://www.guistuff.com/javascript/js_arrays_a1.html

I hope this information will help you to atleast some extent.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
nik, I've said it before: arr is just a reference! Have you read my post? What is still unclear?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

My doubt is : How can we assign one array to other array when the lengths of the array are different?


The size of array is matter of runtime, that is array of what size will be
created on the heap. While declaring reference variable you only
tell the compiler that this reference variable of this dimension ([] means 1
D, [][] means 2 D and so on)and you dont tell what size of array will be
created at run time. It is compiler error to tell the dimension while
declaring.

int[5] a; //error
int[][] b; //OK, ref variable b can refer to two D array
int [][][] c; //ref variable c can hold array object of 3 dim

Thanks,
[ May 18, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
The code above gives compiler error.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
I tried the above given code in Eclipse,it does not give any compiler error
the code is correct and we get the output.
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
hi chandra,
i think i have understood teh concept but not sure.could you please go through my explaination once and correct me if i am wrong in my understanding.

here is my explaination:
compiler just checks for a valid assignment and in this it passes the valid assignment.Hence it compiles fine.
During runtime,it doesnt check the size of arr2(in this example), instead creates the size equal to arr and hence runs fine too.
please let me know if i have got it wrong in anyway.
thanks
[ May 18, 2007: Message edited by: debasmita pattnayak ]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

OK. I'll try to exlpain it from the very beginning.

Take for instance the following statement:
int[] arr2=new int[4];

Now, let's examine it in detail:
int[] arr2 <---- declares a variable arr2 which can refer to an array of int (whatever length, always!)
new int[4] <---- creates an array of int object of length = 4
So:
int[] arr2 = new int[4]; <---- creates an array of int (of length = 4) and assigns its *reference* to variable arr2 (which is capable of containing an array of int reference).

The important thing here is to understand that after that statement, arr2 contains a *reference* to an actual array of int. But it is not an array of int; the array is somewhere in the heap (but you shouldn't care where it actually is).

So the following code:
int[] arr={1,2,3,4,5};
int[] arr2=new int[4];
arr2=arr;

really means:
- An array-of-int-reference variable is declared (in stack) and the reference of a vector of int (in this case {1,2,3,4,5}) is assigned to it (to the arr variable).
- Another array-of-int-reference variable is declared (now is arr2) and the reference of a vector of int (created calling new int[4]) is assigned to it (to the arr2 variable).
You have, until now, two array objects whose references are assigned to two variables. Each variable holds the reference to each one of those objects (in the example arrays of int).
- In the last of the three statements, you take one of the references (the one that is hold by the arr variable; this is a reference to the object created by {1,2,3,4}) and assign it to the other variable (arr2).
Now, you still have two objects (or maybe not, because as there are no more references to one of the objects, i.e. the one created by new int[4], that object is now elegible for garbage collection) and two variables referring to the very same object (in this case the array of int {1,2,3,4}).

I hope I clears it up for you.
Please, take in mind that a variable declared as int[], can hold the reference to any array of int of whatever length. Two or more variables can hold the reference to the same array of int object. And what is more, such a variable may hold no reference at all: that's the case when you say "int[] arr3 = null;".
[ May 18, 2007: Message edited by: Sergio Tridente ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks serigo got it
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
thanks serigo!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Thats a good question posted by nik and a great explanation by Sergio Tridente.

Thanks a lot for that Sergio!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Hi Guys,
Just to add up to what serigo said, here is a piece of code and output.



The output is as follows:



Both the arr and arr2 after assignment points to the same object thereby we get the same HashCode! (only the array reference "arr2" changes to point to a new object as that of "arr").

This states that the array *references* (arr & arr2) can be made to point *an int array* (as here they are declared) irrespective of their sizes.

HtH.
Sandeep Sachan
Greenhorn

Joined: Sep 17, 2005
Posts: 9
class arr3
{
public static void main(String[] args)
{
//---> objects will be created inside heap memory of type "int"
//indirectly i am stating the size of array arry and the value of per index!
int[] arr={88,55,223,6664,4445};
//blank array of type int but will have a single
//dimemtion and size to hold 5 elements
// 0 -----> 4 indexs
int[] arr2=new int[4];

//passing the values to each element
arr2=arr;
for(int a :arr2)
{
System.out.println(a);
}
}
}
ramesh vardhan
Greenhorn

Joined: Jun 21, 2005
Posts: 6
/* Code bit */
int[] arr1={1,2,3};
int[] arr2=new int[4];
arr2=arr1;
System.out.println(arr2[3]);
---
The above code throws a java.lang.ArrayIndexOutOfBoundsException.

Code insight:
1.Two array objects were declared and initialized.
so two references and two objects are created.
2.Then the reference of arr2 is made to point to the object of arr1 by assigning its reference.
3.Hence after assignment two references are pointing to one object,making
the empty array eligible for Garbage Collection.
4.arr2 has only 3 elements and max index is 2,hence the exception.
Its simple assignment of objects to references.No value copy is made or element copy is done.
Hope this clears your doubt.


Regards,<br />RAMESH VARDHAN.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Thats a nice explanation Ramesh !

His doubt was


My doubt is : How can we assign one array to other array when the lengths of the array are different?


Thats why the exception was not explained
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays doubt