Results 1 to 6 of 6
  1. #1
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default whay does this compile?

    Java Code:
    import java.util.Scanner;
    
    public class practest5
     {
    	public static void main(String[] args)
    	{
    		int[] array1 = {5,10,15,20};
    		int[] array2;
    		int num;
    
    		array2 = new int[array1.length / 2];
    		array2[2] = array1[0];
    
    		System.out.println(array2[0]);
    
    
    	}
     }
    this will compile but throws error when run because its trying to print an element out of bounds of the array.

    question is why do it compile then?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    array2 = new int[array1.length / 2];
    array2[2] = array1[0];

    Java does rather little at compile time to check for errors beyond those of type. The compiler makes no attempt to compare 2 with the expression "array1.length/2". afaik "arr[-1]=0;" will also compile.

  4. #4
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    ok thanks for that

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    A bit of background for why:

    To check whether the index is out of bounds, you have to evaluate the expression. The idea of compiling is that you create bytecode, or assembly, or something else that can do it FOR you. To check for IndexOutOfBoundsExceptions and other things like that, you have to run the code... which a compiler should not do.

    Type-checking needs a bit of evaluation too, but as the compiler is already having to figure out what the method/constructor/expression is doing to evaluate it, it doesn't take much to figure out what type it will return... Running the code, however, as mentioned above takes a lot longer (relatively speaking)
    Last edited by Singing Boyo; 10-23-2010 at 07:01 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Singing Boyo View Post
    To check whether the index is out of bounds, you have to evaluate the expression. The idea of compiling is that you create bytecode, or assembly, or something else that can do it FOR you. To check for IndexOutOfBoundsExceptions and other things like that, you have to run the code... which a compiler should not do.
    Even if the compiler could do it it can't do it; suppose the expression contains a method that interacts with the user:

    Java Code:
    int[] array= new array[getIntegerFromUser()];
    The user would be very surprised if she compiled such program ...

    kind regards,

    Jos

Similar Threads

  1. Can't Compile
    By sidk47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-15-2010, 04:43 PM
  2. Help with compile
    By mr_anderson in forum NetBeans
    Replies: 7
    Last Post: 06-10-2010, 04:03 AM
  3. Replies: 8
    Last Post: 02-26-2009, 03:21 PM
  4. doesn't compile?!
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 06-14-2008, 05:42 PM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •