Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Textbook: "the size of an array must be known at compile time"

I was just skimming through one of my old textbooks and found this passage defining arrays in Java:

A one-dimensional array is a structured composite data type made up of a finite, fixedsize collection of ordered homogeneous elements to which there is direct access. Finite indicates that there is a last element. Fixed size means that the size of the array must be known at compile time, but it doesn’t mean that all of the slots in the array must contain meaningful values.

I have a basic understanding of arrays and am comfortable using them in every day tasks, but I am very confused by the statement that the size of arrays must be known at compile time.

A very simple Java program demonstrates that an array can be instantiated with a variable size at runtime:

import java.util.Scanner;

public class test
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number: ");
      int size = scan.nextInt();
      int[] array = new int[size];
      System.out.println("You just create an array of size " + array.length);
   }
}

This compiles, executes, and reaches the end without error.

What gives?

like image 586
CptSupermrkt Avatar asked Nov 26 '13 03:11

CptSupermrkt


People also ask

Does array size need to be known at compile time?

For dynamically-sized arrays you'd typically use a `Vec<T>` in Rust (or a `Box<[T]>`). These imply the array is stored as a separate heap allocation. The size of all stack-allocated items needs to be known at compile-time.

Do you have to declare the size of an array in Java?

Answer: No. It is not possible to declare an array without specifying the size. If at all you want to do that, then you can use ArrayList which is dynamic in nature. Q #2) Is Array size fixed in Java?

Which is the correct way of knowing array size in Java?

To find the length of the array, we have used array name (arr) followed by the dot operator and length attribute, respectively. It determines the size of the array. Note that length determines the maximum number of elements that the array can contain or the capacity of the array.

Can we change the size of an array at compile time?

Size of an array If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.


1 Answers

It is a very poorly worded paragraph, but if you interpret it loosely, it's correct.

In your example, the size of the array is known at compile time. The size is size.

You're interpreting "known at compile time" with "static" or "constant," which is understandable. Of course as we know though, the JVM allocates memory dynamically based on the value of size.

The author is probably trying to distinguish between an array and something like an ArrayList, where the dimensions don't have to be specified upon initialization.

like image 71
Vidya Avatar answered Sep 21 '22 05:09

Vidya