Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I have a string defined as

String xx

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

2) 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? or is there any better java practices?

like image 754
The Learner Avatar asked Aug 25 '12 07:08

The Learner


1 Answers

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.

like image 191
Peter Lawrey Avatar answered Oct 21 '22 12:10

Peter Lawrey