为什么我可以做char c =' a'; c.tolowercase(),而必须执行字符。Tolowercase(c);

发布于 2025-01-19 14:06:38 字数 152 浏览 4 评论 0原文

只能完成

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-01-26 14:06:38

tl;dr

char 类型是一个原语,而原语没有方法。

此外,char 已过时。在处理单个字符时,请使用代码点整数。

Character.toString(
    Character.toLowerCase(    // Takes a code point integer number.
        "A".codePointAt( 0 )  // Annoying zero-based index numbering.
    )                         // Returns another code point integer number.
)                             // Returns a `String` containing a single character.

一个

与对象

你说:

我觉得这很令人困惑

你需要了解 Java 中双重 类型系统 之间的区别:

  • 原始
  • 对象

你说:

哪里可以找到更多信息

请参阅 Oracle 免费提供的 Java 教程。请参阅原始数据类型什么是对象?

搜索 Stack Overflow “java原语”。

只有对象才有方法

char 类型是原始类型。所以它没有方法。只有对象才有方法。

Character 类提供了一个静态方法< code>toLowerCase 它将 char 原始值作为其参数。

你问:

为什么有必要

基元不是面向对象的。原语最初包含在 Java 中,是为了使 C 代码的移植更容易,这是一种被考虑的能力在那个时代非常重要。

char 已过时

您应该知道,char 类型自 Java 2 起就已过时,并且自 Java 5 起就已过时。作为 16 位值,char code> 在物理上无法表示大多数字符——本质上是损坏的。

代码点

请使用代码点整数。

拉丁文大写字母 A 的代码点是十进制 65,十六进制

获取字符的代码点。

int codePoint = "A".codePointAt( 0 ) ;

获取包含代码点字符的String

String s = Character.toString( codePoint ) ;

获取由其代码点表示的字符的小写版本。

int lowerCaseCodePoint = Character.toLowerCase( codePoint ) ;
String lowerCaseLetter = Character.toString( lowerCaseCodePoint ) ;

转储到控制台。

System.out.println( "codePoint: " + codePoint ) ;
System.out.println( "s: " + s ) ;
System.out.println( "lowerCaseCodePoint: " + lowerCaseCodePoint ) ;
System.out.println( "lowerCaseLetter: " + lowerCaseLetter ) ;

请参阅在 IdeOne.com 上实时运行的代码

codePoint: 65
s: A
lowerCaseCodePoint: 97
lowerCaseLetter: a

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.

Character.toString(
    Character.toLowerCase(    // Takes a code point integer number.
        "A".codePointAt( 0 )  // Annoying zero-based index numbering.
    )                         // Returns another code point integer number.
)                             // Returns a `String` containing a single character.

a

Primitive versus Object

You said:

I find this very confusing

You need to learn the difference between the dual type systems in Java:

  • primitive
  • object

You said:

where I can find more information

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 a static method toLowerCase which takes a char primitive value as its argument.

You asked:

why it's necessary

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 obsolete

You 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.

int codePoint = "A".codePointAt( 0 ) ;

Get a String containing the character for a code point.

String s = Character.toString( codePoint ) ;

To get lowercase version of a character being represented by its code point.

int lowerCaseCodePoint = Character.toLowerCase( codePoint ) ;
String lowerCaseLetter = Character.toString( lowerCaseCodePoint ) ;

Dump to console.

System.out.println( "codePoint: " + codePoint ) ;
System.out.println( "s: " + s ) ;
System.out.println( "lowerCaseCodePoint: " + lowerCaseCodePoint ) ;
System.out.println( "lowerCaseLetter: " + lowerCaseLetter ) ;

See this code run live at IdeOne.com.

codePoint: 65
s: A
lowerCaseCodePoint: 97
lowerCaseLetter: a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文