String's Maximum Length in Java - Calling Length() Method

String's Maximum length in Java - calling length() method

Considering the String class' length method returns an int, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)

In terms of lengths and indexing of arrays, (such as char[], which is probably the way the internal data representation is implemented for Strings), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:

The variables contained in an array
have no names; instead they are
referenced by array access expressions
that use nonnegative integer index
values. These variables are called the
components of the array. If an array
has n components, we say n is the
length of the array; the components of
the array are referenced using integer
indices from 0 to n - 1, inclusive.

Furthermore, the indexing must be by int values, as mentioned in Section 10.4:

Arrays must be indexed by int values;

Therefore, it appears that the limit is indeed 2^31 - 1, as that is the maximum value for a nonnegative int value.

However, there probably are going to be other limitations, such as the maximum allocatable size for an array.

What is the maximum size of a `String` object in Java?

Strings contain an array of chars, so I think you can have at most 2^31 - 1 characters in a Java String. This is the value of Integer.MAX_VALUE.

Any String larger than that, and you can't even index all of the elements.

How many characters can a Java String have?

You should be able to get a String of length

  1. Integer.MAX_VALUE always 2,147,483,647 (231 - 1)

    (Defined by the Java specification, the maximum size of an array, which the String class uses for internal storage)

    OR

  2. Half your maximum heap size (since each character is two bytes) whichever is smaller.

Collecting a List of all strings with maximum length

I would do it like this:

List<String> values = Arrays.asList("abc", "ab", "bc", "bcd", "a");
// I group by length and put it into a TreeMap then get the max value
values.stream().collect(groupingBy(String::length, TreeMap::new, toList()))
.lastEntry()
.getValue()
.forEach(System.out::println);

Output:

abc
bcd

Is there any limit for string size in a Java program?

A common question you could have searched for but I going to answer it again anyway.

Is there any limit for the number of characters that I can assign?

Its Integer.MAX_VALUE or 2^31-1 or about 2 billion. You are more likely to have memory problems before getting to this size. e.g. You need 4 GB for the String and 4 GB to create it.

I am assigning the user input to this string xx. 70% of the times people give only one word. some times they give a big sentence so want to know is that ok?

I suspect all the works of J K Rowling would fit into one string.

or is there any better java practices?

I suggest you keep things as simple as possible. Assigning a String reference is about simple as it gets.



Related Topics



Leave a reply



Submit