为什么我可以做char c =' a'; c.tolowercase(),而必须执行字符。Tolowercase(c);
只能完成
char c = 'A';
Character.toLowerCase(c);
。
char c = 'A';
c.toLowerCase();
为什么
Why is it only done like
char c = 'A';
Character.toLowerCase(c);
and not..
char c = 'A';
c.toLowerCase();
I find this very confusing and don't know where I can find more information about why this happens or why it's necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tl;dr
char
类型是一个原语,而原语没有方法。此外,
char
已过时。在处理单个字符时,请使用代码点整数。与对象
你说:
你需要了解 Java 中双重 类型系统 之间的区别:
你说:
请参阅 Oracle 免费提供的 Java 教程。请参阅原始数据类型和什么是对象?。
并搜索 Stack Overflow “java原语”。
只有对象才有方法
char
类型是原始类型。所以它没有方法。只有对象才有方法。Character
类提供了一个静态
方法< code>toLowerCase 它将char
原始值作为其参数。你问:
基元不是面向对象的。原语最初包含在 Java 中,是为了使 C 代码的移植更容易,这是一种被考虑的能力在那个时代非常重要。
char
已过时您应该知道,
char
类型自 Java 2 起就已过时,并且自 Java 5 起就已过时。作为 16 位值,char
code> 在物理上无法表示大多数字符——本质上是损坏的。代码点
请使用代码点整数。
拉丁文大写字母 A 的代码点是十进制 65,十六进制。
获取字符的代码点。
获取包含代码点字符的
String
。获取由其代码点表示的字符的小写版本。
转储到控制台。
请参阅在 IdeOne.com 上实时运行的代码。
tl;dr
char
type is a primitive, and primitives do not have methods.Furthermore,
char
is obsolete. Use code point integer numbers instead when working with individual characters.Primitive versus Object
You said:
You need to learn the difference between the dual type systems in Java:
You said:
See the Java Tutorial provided by Oracle free of cost. See Primitive Data Types and What is an Object?.
And search Stack Overflow for "java primitive".
Only objects have methods
The
char
type is a primitive. So it has no methods. Only objects have methods.The
Character
class provides astatic
methodtoLowerCase
which takes achar
primitive value as its argument.You asked:
Primitives are not object-oriented. Primitives were originally included in Java to make porting of C code easier, an ability considered quite important back in those days.
char
is obsoleteYou should know that the
char
type has been obsolete since Java 2, and legacy since Java 5. As a 16-bit value,char
is physically incapable of representing most characters — essentially broken.Code point
Instead, use code point integer numbers.
The code point for LATIN CAPITAL LETTER A is 65 decimal, 41 hexadecimal.
Get the code point of a character.
Get a
String
containing the character for a code point.To get lowercase version of a character being represented by its code point.
Dump to console.
See this code run live at IdeOne.com.