You don't need to know the array size when you declare it

String[] myArray;

but you do need to know the size when you initialize it (because Java Virtual Machine needs to reserve a continuous chunk of memory for an array upfront):

myArray = new String[256];

If you don't know what the size will need to be in the moment you initialize it, you need a List<String>, or you'll be forced to make your own implementation of it (which is almost certainly worse option).

Answer from Jiri Tousek on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 1665834 › how-can-i-initialize-a-string-array-with-length-0-in-java
How can I initialize a String array with length 0 in Java? - Stack Overflow
The Java Docs for the method String[] java.io.File.list(FilenameFilter filter) includes this in the returns description: The array will be empty if the directory is empty or if no names were acce...
Discussions

How to declare string array[] of unknown size (JAVA) - Stack Overflow
Alternatively you can use an empty ... a new String[] each time the size changes. ... Save this answer. ... Show activity on this post. You don't need to know the array size when you declare it ... but you do need to know the size when you initialize it (because Java Virtual Machine ... More on stackoverflow.com
🌐 stackoverflow.com
August 30, 2016
java - How do you declare a string array of unknown size? - Stack Overflow
Possible Duplicate: Declaring an array of unknown size · I'm working in Java and I am trying to input a sentence into a string array. I am tokenizing it and determining the word count. However, I need to add each word into a string array in order to determine if there are duplicates or not. I am not sure how to initialize my array if I don't know the word count until later in the program. //Declares variables Scanner scan = new Scanner (System.in); int withoutdup ... More on stackoverflow.com
🌐 stackoverflow.com
java - How do you declare a string array of unknown size? - Stack Overflow
Possible Duplicate: Declaring an array of unknown size · I'm working in Java and I am trying to input a sentence into a string array. I am tokenizing it and determining the word count. However, I need to add each word into a string array in order to determine if there are duplicates or not. I am not sure how to initialize my array if I don't know the word count until later in the program. Copy //Declares variables Scanner scan = new Scanner (System.in); int withoutdup ... More on stackoverflow.com
🌐 stackoverflow.com
October 18, 2012
Java: how to initialize String[]? - Stack Overflow
You can do that in the declaration, or do it without the String[] later on, so long as it's before you try use them. ... Save this answer. ... Show activity on this post. You can use below code to initialize size and set empty value to array of Strings More on stackoverflow.com
🌐 stackoverflow.com
🌐
Java2Blog
java2blog.com › home › core java › string › how to declare a string array in java
How to declare a String array in java - Java2Blog
April 15, 2021 - We can declare and initialize array in java using new operator. We don’t have to provide size in this case. You can also use variable name before []. We can also declare and initialize array in java without using new operator. We can also declare a String array and can initialize it later.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string array- tutorial with code examples
Java String Array- Tutorial With Code Examples
April 1, 2025 - Note that before using this array, you will have to instantiate it with new. In the second declaration, a String Array is declared and instantiated using new. Here, a String array ‘myarray’ with five elements is declared.
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - Notably, the length of an array is always fixed and cannot be extended after initialization. Alternatively, we can specify the length of an array using a variable: ... Here, we declare a variable used as the length of the array. Importantly, length can only be of type int. Any other type apart from int throws an incompatible type error. When we declare an array, knowing the size is unnecessary.
Find elsewhere
🌐
Techie Delight
techiedelight.com › home › java › initialize an array of string in java
Initialize an array of String in Java | Techie Delight
July 7, 2026 - 1. We can declare and initialize an array of string in Java by using a new operator with an array initializer. For example, the following code snippet creates an array of string of size 5: 2. Following is an alternate syntax for declaring an ...
🌐
Delft Stack
delftstack.com › home › howto › java › how to initialize a string array in java
How to Initialize a String Array in Java | Delft Stack
March 13, 2025 - Yes, you can initialize a String array without specifying its size by using an array literal, where you directly assign values to the array.
🌐
Coderanch
coderanch.com › t › 714900 › java › Array-empty-braces-size-String
Array with empty braces and without size : String[] lion = new String[] {}; (Beginning Java forum at Coderanch)
August 20, 2019 - There is a shorter way to initialise that array; if the declaration and initialisation are written on the same line, the type may be omitted:-Remember that an array can have any length not larger than Integer.MAX_VALUE, but it is usually not possible to instantiate the largest sizes because of memory limits.
🌐
Board Infinity
boardinfinity.com › blog › string-array-in-java
String Array in Java | Board Infinity
August 20, 2025 - It is known as a String Array in Java when a String array is created. A String array must first be declared and initialized before it can be used. There are several options available for doing this. String arrays can be declared in programs either with or without size.
🌐
Edureka
edureka.co › blog › string-array-in-java
Java String Array | String Array in Java with Examples | Edureka
July 5, 2024 - A string array is declared by the following methods: String[] stringArray1 //declaring without size String[] stringArray2 = new String[2]; //declaring with size · The string array can also be declared as String strArray[], but the previously ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-arrays-in-java
String Arrays in Java - GeeksforGeeks
October 2, 2025 - String[] str0; // declaration without size String[] str1 = new String[4]; //declaration with size ... We can use both of these ways to declare String array in Java. In this method, we are declaring the values at the same line.
🌐
Centron
centron.de › home › tutorials › java string array - tutorial
Java String Array - Tutorial – centron
March 1, 2024 - Java String array is basically an array of objects. There are two ways to declare a string array - declaration without size and declare with size. There are two ways to initialize a string array - at the time of declaration, populating values after declaration.
🌐
HappyCoders
happycoders.eu › home › java › how to declare and initialize arrays in java
How to Declare and Initialize Arrays in Java
September 24, 2026 - You can find the complete code in the GitHub repository java-arrays, in the package eu.happycoders.initialize. Each main section has its own class there with a main() method that produces the output shown. We start with the most common operations, the array declaration and array initialization. Declaring a variable is the time we make the variable and its type known to the compiler. The following two lines show the declaration of an int array and a String array: ... We do not yet specify the size of the array in the declaration.
🌐
How to do in Java
howtodoinjava.com › home › java array › declare and initialize an array in java (with examples)
Declare and Initialize an Array in Java (With Examples)
October 10, 2026 - Readers who come from Python or ... about Java arrays. Yes, the declaration int[] data; has no size. We give the size only when we create the array with new int[n], and with an initializer such as {1, 2, 3}, the compiler counts it for us.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-array
Java String Array: Declaration, Initialization & Examples | DigitalOcean
August 3, 2022 - Master Java String arrays with declaration, initialization, and manipulation examples. Learn iteration, sorting, searching, and conversion techniques.
🌐
Hero Vired
herovired.com › learning-hub › blogs › string-array-in-java
String Array in Java: How to Declare, Example, Initializing
October 7, 2024 - This allows us to create an array without knowing the exact size beforehand. ... Finally, the split() method can parse a string into an array. This method will split a string into multiple substrings depending on the character or pattern used as the separator. ... "`Java String myString = “one,two,three”; String[] myArray = myString.split(","); // Will return an array with the strings "one", "two", and "three" ```