So, I'm thinking about that, is the JTextArea length infinite? Or how many chars can be used max?
No, JTextArea
is not infinite
.
We can imply the maximum length based on the fact that JTextArea
only returns a String
, which has a length
which returns a int
. This implies that the maximum length of a JTextArea
is bound to Integer.MAX_VALUE
, but, because of array overheads, is slightly smaller. But in practice, you'll probably find that it's much smaller, due to the need for arrays to be laid in memory in a continuous manner, so it will depend on how much memory the JVM has available and how fragmented it is.
We can further investigate this and have a look at PlainDocument
, which is the default Document
used by JTextArea
, which uses a char[]
as it's internal data structure, just like String
.
This further concretes the reasoning that the limit of a JTextArea
is limited to less then Integer.MAX_VALUE
You can have a look at Do Java arrays have a maximum size?, Why I can't create an array with large size? and Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8? for discussions on why an array can't be declared as Integer.MAX_VALUE
Now, before someone suggests that you could write a linked list implementation of a Document
, don't forget that both Document
and JTextArea
rely on String
, which is a key limiting factor