Java程序中的字符串大小是否有限制? [英] Is there any limit for string size in a Java program?

查看:273
本文介绍了Java程序中的字符串大小是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

字符串在Java中的最大长度 - 调用length()方法

我有一个字符串定义为

字符串xx

是否有任何限制我可以分配的字符数?

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

2)我将用户输入分配给此字符串xx。
70%的人只给一个字。有时他们会给出一个很大的句子,所以想知道那可以吗?或者有没有更好的java做法?

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?

推荐答案

你可以搜索一个常见的问题,但无论如何我会再次回答它。

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?

其Integer.MAX_VALUE或2 ^ 31-1或约20亿。在达到此大小之前,您更有可能遇到内存问题。例如字符串需要4 GB,创建它需要4 GB。

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.


我将用户输入分配给此字符串xx。 70%的人只给出一个字。有时他们会给出一个很大的句子,所以想知道那可以吗?

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?

我怀疑JK罗琳的所有作品都适合一个字符串。

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

还是有更好的java做法吗?

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.

这篇关于Java程序中的字符串大小是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆