wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Inquisition Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java Inquisition Question" Watch "Java Inquisition Question" New topic

Java Inquisition Question

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

int [] arr = {1, 2, 3, 4, 5};
int [] arr2 = new int[4];
arr2 = arr;

The answer is rints 5.

How can this be?arr2 is instantiated with a size of 4 elements..yet arr has 5 elements.How can arr2 reference arr when it is not large enough to do so?

===>SCJP 1.5(72%)<===
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

int [] arr = {1, 2, 3, 4, 5};
This statement would create a memory location with the above mention values.
And arr would refer to that a memory location(starting point).

int [] arr2 = new int[4];
Similarly arr2 would point to the other memory location.

arr2 = arr;

This statement cause arr2 to point to arr location which has got 4 location.

Remember array array are also OBJECTS.

It is very much similar to ...
A a1= new A();
A a2= new A();


Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ok...thats fine then I guess
I agree. Here's the link: http://aspose.com/file-tools
subject: Java Inquisition Question
Similar Threads
Arrays doubt
Array Confusion !
what is the output here..
Output Not understand!!!