It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array 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 "array doubt" Watch "array doubt" New topic
Author

array doubt

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186


at line 1, we assign an array with 3 elements to a empty array s declared with ZERO size, there is no compilation error, why is that?


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
An array is actually an object, and the arrayname is the reference.

s=arr; //-----------line 1

In this line, you are assigning the value of one reference to another, just as you would do for an object. So, the array size does not matter.

In effect, the array that 's' had earlier becomes unreachable and now, 's' and 'arr' point to the same location.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Because array bounds aren't checked at compile-time. Like null pointers dereferences, this will compile fine but cause a runtime exception.

Why doesn't the compiler perform this check at compile-time? Probably because, from a computer science perspective, I suspect that it's not possible in general to determine statically whether an array out-of-bounds error will occur in a program, given that Java array sizes don't have to be compile-time constants.


SCJP 5.0
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

at line 1, we assign an array with 3 elements to a empty array s declared with ZERO size, there is no compilation error, why is that?

In this case, its legal at compile time and runtime to assign one array to another, because simply you assign to the reference variable of the array Not the array its self.

But there is limitation, as the array you are assigning must be of the same dimension as the reference you are assigning to. For example:

At line 1, assigning 1D array to 1D array, hence valid statement.
At line 2, assigning 2D array to 1D array, Compile- time error.
[ November 04, 2007: Message edited by: Ahmed Yehia ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Ah, I misread your question in my original response. Vidhya and Ahmed's responses above are correct. Essentially, the two key things to understand here are that:
  • the type of an array reference does not restrict it to a specific array length; and
  • the assignment statement in Line 1 does not copy elements from one array to another, but merely changes the array reference s to point to the array object arr.

  • [ November 04, 2007: Message edited by: Kelvin Lim ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: array doubt
     
    Similar Threads
    this code is giving file not found exception
    Arrays
    Array Problem
    returning an array
    here is a question and i have a very similar code but i need to convert it