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
🌐
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.
Discussions

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
Declare an array in java without size - Stack Overflow
Hello am trying to declare an array in java but i do not want the array to have a specific size because each time the size must be different. I used this declaration: int[] myarray5; but when... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to declare an array without initial size?
No, not really. An array by definition is a fixed size throughout its lifetime, so you can't change it as you go. At your level, the normal way to do this would be to declare a sufficiently large array (but check those limits). For example, how likely is it that a user of your program will enter more than 1000 characters? char array[1024] = {0}; //Declare and zero a 1k char array int i = 0; //Loop counter while (i < 1023 && ch = getchar() != '\n') //Check limits of array array[i++] = ch; //Assign char and increment i More on reddit.com
🌐 r/C_Programming
10
1
September 27, 2016
How to pass an array of unknown size to a class?
You want to make it an instance variable. More on reddit.com
🌐 r/javahelp
5
1
June 14, 2020
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string array- tutorial with code examples
Java String Array- Tutorial With Code Examples
April 1, 2025 - In this tutorial, we will discuss the string array in Java in detail along with the various operations and conversions that we can carry out on string arrays. A String Array can be declared in two ways i.e. with a size or without specifying the size.
🌐
Talkerscode
talkerscode.com › howto › how-to-declare-string-array-in-java-without-size.php
How To Declare String Array In Java Without Size
Being the starting point for running a Java application, the main method in Java is typically a first method you encounter while learning how to programme in Java. The main function is then launched by public static void main. On the command prompt, the method main accepts a string array. Public refers to accessibility from everywhere. Because the main method can only be called once and is fixed, static is primarily utilised for it. The string array is then declared without a size.
🌐
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.
🌐
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.
🌐
JanBask Training
janbasktraining.com › home › what is java string? how to declare string array in java with example
What is Java String? How to Declare String Array in Java with Example
June 1, 2018 - In another way to declare the String array we put the square bracket after the data type that is String, just like shown below: ... Above two declarations are considered similar by the Java compiler and have the same effect.
Find elsewhere
🌐
Upgrad
upgrad.com › home › blog › software development › string array in java: java string array with coding examples
String Array In Java: Java String Array With Coding Examples | upGrad blog
August 11, 2026 - In the first section, the string array is declared just like a general variable without specifying the size. Remember that before using this method, you have to instantiate the collection with “new”. Check out upGrad’s Advanced Certification in Blockchain · In the second section, the string array is instantiated and declared with ‘new’. Here the string array in Java is declared with five elements.
🌐
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.
🌐
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.
🌐
Alvin Alexander
alvinalexander.com › java › java-string-array-reference-java-5-for-loop-syntax
Java String array examples (with Java 5 for loop syntax) | alvinalexander.com
This array programming approach ... array in two steps, you can do everything in one step, like this: public class JavaStringArrayTests { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; }...
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - We can either assign an array to null or an empty array: ... But we need to know the size when we initialize it because the Java virtual machine must reserve the contiguous memory block for it.
🌐
Shahucollegelatur
shahucollegelatur.org.in › Department › Studymaterial › sci › compsci › Array and String-converted new.pdf pdf
Arrays and String
Java String array is basically an array of objects. There are two ways to declare string array – declaration without size and declare with size.
🌐
Quora
quora.com › How-can-we-create-array-without-specifying-a-size-in-java
How can we create array without specifying a size in java? - Quora
Answer (1 of 11): Yes. You can, technically, declare the type of a variable as an array (of specific type) like int[ ] a; But you can't use it before initializing it. This array ‘a’ doesn't exist anywhere in the memory. The compiler only knows that ‘a’ will be an array.
🌐
Edureka
edureka.co › blog › string-array-in-java
Java String Array | String Array in Java with Examples | Edureka
July 5, 2024 - For implementation ensure you get Java Installed. A string array is declared by the following methods: String[] stringArray1 //declaring without size String[] stringArray2 = new String[2]; //declaring with size
🌐
Scaler
scaler.com › home › topics › how to declare an array in java?
How to Declare an Array in Java? - Scaler Topics
April 20, 2024 - We can use this same technique for strings and booleans as well. In java we can initialize the array using new keyword as follows - Note: Once the size of array is specified it would not change. ... Declaration of array in java without specifying its size is not possible.
🌐
javaspring
javaspring.net › blog › declare-an-array-in-java-without-size
Java Array Declaration Without Fixed Size: How to Avoid Initialization Errors and Use Dynamic Arrays — javaspring.net
When you allocate memory for a traditional Java array, you must specify its size upfront, and once created, that size cannot be changed. This rigidity often leads to initialization errors—such as NullPointerException, ArrayIndexOutOfBoundsException, or compile-time syntax errors—when the number of elements is dynamic or unknown ahead of time. If you are wondering whether you can declare a Java array without a fixed size, the answer depends on the stage:
🌐
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.