【发布时间】:2011-06-02 04:04:52
【问题描述】:
我想知道为什么 indexOf 方法的参数是 int ,而描述是 char。
公共 int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified **character**
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#indexOf%28int%29
Also, both of these compiles fine:
char c = 'p';
str.indexOf(2147483647);
str.indexOf(c);
a]基本上,我感到困惑的是 java 中的 int 是 32bit ,而 unicode 字符是 16 bits 。
b]为什么不使用字符本身而不是使用 int 。这是任何性能优化吗? chars 比 int 更难表示吗?怎么样?
我认为这应该是一个简单的推理,这让我更加了解它!
谢谢!
【问题讨论】:
标签: java data-structures primitive-types