Arrays must have a fixed length.

If your goal is to have a dynamically expansible list, consider a List instead. Everytime you add an item by add() method, it will grow dynamically whenever needed.

List<Character> chars = new ArrayList<>();
// ...

See also:

  • Java Tutorials > Trail: Collections > The List Interface
Answer from BalusC on Stack Overflow
🌐
Edureka
edureka.co › blog › character-array-in-java
Char Array In Java | Introduction To Character Arrays In Java | Edureka
July 5, 2024 - Let us start this article on Char Array In Java, by understanding how to declare arrays in Java. though start with Java installation.
Discussions

How to represent empty char in Java Character class - Stack Overflow
I want to represent an empty character in Java as "" in String... Like that char ch = an empty character; Actually I want to replace a character without leaving space. I think it might be suff... More on stackoverflow.com
🌐 stackoverflow.com
June 15, 2014
Is it possible to declare a blank array with multiple spaces?
Whenever you declare an array in java (and in a few other languages too) if the array is of a primitive type (char, double, int... etc), then each index of the array will be initialized with whatever value is produced with a zeroing of the bits of that space in memory1 and if its an array of objects they are initialized as null. 1Pretty much this just means that the value is set to zero. In the case of a character, it's the null character '\0'. With primitives, it's not really possible to have a 'null' index in an array. You would have to use some sort of special number to symbolize empty space like `Integer.MAX_VALUE` or `Integer.MIN_VALUE` which represents the largest and smallest possible integer respectively. The other option is to use an object-based approach with ArrayLists or some other data structure. More on reddit.com
🌐 r/javahelp
5
9
July 13, 2020
What is the initial values of char arrays?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
9
7
October 15, 2021
java - An empty char is not possible but an empty string is possible - Stack Overflow
There are NULL characters and white characters but those are not empty characters so char ch = '\0'; is possible but not char ch = '';. Now, we can create a empty string in Java, and string is nothing but an array of characters, so it makes me wonder how an empty string would be stored? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Quora
quora.com › How-can-I-set-a-character-array-to-null-in-Java-Or-empty-it
How to set a character array to null in Java? Or empty it - Quora
Answer (1 of 6): [code]class EmptyCharArray{ public static void main(string[] args) { char namearray[6] = {'a', 'n', 'm', 'o', 'l'}; for(int i=0;i
🌐
Delft Stack
delftstack.com › home › howto › java › empty char in java
How to Represent Empty Char in Java | Delft Stack
February 2, 2024 - While in Java, it is possible to have an empty char[] array, the concept of an empty char itself is not applicable. This is because the term char inherently denotes a character, and an empty char lacks meaningful representation.
🌐
Linux Hint
linuxhint.com › represent-empty-char-in-java
How to Represent Empty char in Java
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Javatpoint
javatpoint.com › character-array-in-java
Character Array in Java - Javatpoint
Character Array in Java - Character Array in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Huda Tutorials
hudatutorials.com › java › basics › java-arrays › java-char-array
Java char Array - char array in java - Huda Tutorials
July 16, 2021 - If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector. ... /* Java char Array Example Save with file name CharArray.java */ public class CharArray { public static void main(String args[]) { // JAVA CHAR ARRAY DECLARATION char c[]; // MEMORY ALLOCATION FOR JAVA CHAR ARRAY c = new char[4]; // ASSIGNING ELEMENTS TO JAVA CHAR ARRAY c[0] = 'a'; c[1] = 'c'; c[2] = 'D'; c[3] = 'B'; // JAVA CHAR ARRAY OUTPUT System.out.println("Java char Array Example"); for(int i=0;i<c.length;i++) { System.out.println("Element at Index : "+ i + " " + c[i]); } } }
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_fill_char.htm
Java Arrays fill(char[], char) Method
The Java Arrays fill(char[] a, int fromIndex, int toIndex, char val) method assigns the specified char value to each element of the specified range of the specified array of chars.The range to be filled extends from index fromIndex, inclusive, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › character-array-in-java
Character Array in Java - GeeksforGeeks
January 22, 2026 - Example: This program demonstrates how to access and modify elements in a character array in Java.
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › empty-char-in-java-95407
empty char in java
September 22, 2003 - Hi everyone, I want to initialize an empty char in java, but I just found out that isn't as simple as I thought. I tried Code: char ch = ''; But that
🌐
Reddit
reddit.com › r/javahelp › is it possible to declare a blank array with multiple spaces?
r/javahelp on Reddit: Is it possible to declare a blank array with multiple spaces?
July 13, 2020 -

I'm trying to write a method that involves taking an int and using that number to create an array of that size. However, the contents of the array are not known at initialization. Is there a way to declare an array with (for example) ten blank spaces?

I tried

int[] MyArray = new int[Num];
MyArray[Num] = Num; 

but that only creates an array with Num at the first index.

Thanks for any help, I really appreciate it :)

🌐
Javatpoint
javatpoint.com › how-to-declare-an-empty-array-in-java
How to Declare an Empty Array in Java - Javatpoint
How to Declare an Empty Array in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 275722 › check-if-char-array-is-empty
java - Check if char array is empty | DaniWeb
indeed it doesn't. There is no such concept as an "empty array" of primitives. Every element of an array is always initialised to something. In case of object types, that's null. In case of primitive types, that's 0 (zero).
🌐
Coderanch
coderanch.com › t › 392740 › java › Set-char-empty
Set a char to empty (Beginning Java forum at Coderanch)
October 9, 2016 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... char is a primitive type that contains a character value. Asking why it cannot be set to "empty" is like asking why an int can't be set to "empty".
🌐
Reddit
reddit.com › r/javahelp › what is the initial values of char arrays?
r/javahelp on Reddit: What is the initial values of char arrays?
October 15, 2021 -

So, I'm learning about arrays using int name = new int[5] where it creates a fixed size array (in this case, it has 5 items of arrays).

As for int, it will fill it with 0 in each item as the initial values. For boolean, false. For double, 0.0. For String, null.

But then, I tried create the char array. But, when I printed it out, it shows weird values. This is what it prints out on my command line. What the hell is that?

Top answer
1 of 2
3
See this for default values of datatypes. The default value of char is the NULL character ('\u0000'), because char is numeric on the inside and will have a default value of 0, which maps in the ASCII table to that character. NULL will not be properly displayed.
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
Initialization of character arrays
We cannot provide a description for this page right now
Top answer
1 of 3
5

A Java String is not an array of characters (nor is it a single char, which is an integral primitive type). It is an Object type, and includes a length.

JLS-10.9. An Array of Characters is Not a String says (in part)

In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).

A String object is immutable, that is, its contents never change, while an array of char has mutable elements.

I mean in the end each character in the string will be stored as a BYTE, now how a empty string will be stored?

No, each character is 2 bytes. The Java Tutorials: Primitive Data Types says

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

In the case of an empty String there aren't any characters; and an empty array has length 0.

2 of 3
4

String is certainly backed by a char[] (a field known as value), but that does not under any circumstance imply that a String is exactly equivalent to a char[]. They're two different objects.

Now, with that out of the way, let's reason about what we're expecting with a String of length zero. This is how we determine length:

/**
 * Returns the length of this string.
 * The length is equal to the number of <a href="Character.html#unicode">Unicode
 * code units</a> in the string.
 *
 * @return  the length of the sequence of characters represented by this
 *          object.
 */
public int length() {
    return value.length;
}

The big thing here to note is that if we're creating an empty string, the length of our backing array has to be zero. So, the way that an empty String is created is by providing an empty value array at instantiation.

🌐
Dot Net Perls
dotnetperls.com › char-array-java
Java - char Array - Dot Net Perls
January 25, 2024 - In Java, Strings are immutable: they cannot be changed in place or added to. With char arrays, we manipulate character buffers. Char arrays are faster—we can change text data without allocations. With the String constructor, we can convert back into a string. To begin, char arrays support the array initializer syntax like other arrays. Here we create a new array with 3 elements.
🌐
TutorialsPoint
tutorialspoint.com › in-how-many-ways-we-can-convert-a-string-to-a-character-array-using-java
In how many ways we can convert a String to a character array using Java?
October 10, 2019 - Create an empty character array with the length of the String. The charAt() method of the String class returns the character at a particular position. Using this method copy each character of the String to the array. ... import java.util.Arrays; import java.util.Scanner; public class ...