Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Inquisition Question

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int [] arr = {1, 2, 3, 4, 5};
int [] arr2 = new int[4];
arr2 = arr;
System.out.println(arr2[4]);

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?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

a2=a1.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...thats fine then I guess
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic