Java中的char和Character有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24823420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between char and Character in Java?
提问by Mido
I need to know what is the difference between char and Character in Java because when I was making a java program, the char worked while the Character didn't work.
我需要知道 Java 中 char 和 Character 之间的区别是什么,因为当我制作 Java 程序时,char 有效而 Character 无效。
采纳答案by user3589907
char is a primitive type that represents a single 16 bit Unicode character while Character is a wrapper class that allows us to use char primitive concept in OOP-kind of way.
char 是表示单个 16 位 Unicode 字符的原始类型,而 Character 是一个包装类,它允许我们以 OOP 类的方式使用 char 原始概念。
Example for char,
字符示例,
char ch = 'a';
Example of Character,
字符示例,
Character.toUpperCase(ch);
It converts 'a' to 'A'
它将“a”转换为“A”
回答by dimzak
From the JavaDoc:
从JavaDoc:
The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char. In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
Character information is based on the Unicode Standard, version 6.0.0.
Character 类将原始类型 char 的值包装在一个对象中。Character 类型的对象包含一个类型为 char 的字段。此外,此类提供了多种方法来确定字符的类别(小写字母、数字等)以及将字符从大写转换为小写,反之亦然。
字符信息基于 Unicode 标准 6.0.0 版。
So, char is a primitive type while Character is a class. You can use the Character to wrap char from static methods like Character.toUpperCase(char c)
to use in a more "OOP way".
所以,char 是一个原始类型,而 Character 是一个类。您可以使用 Character 来包装静态方法中的字符,例如Character.toUpperCase(char c)
以更“OOP 方式”使用。
I imagine in your program there was an 'OOP' mistake(like init of a Character) rather than char vs Character mistake.
我想在你的程序中有一个“OOP”错误(比如字符的初始化)而不是字符与字符错误。
回答by Aimee Borda
Character is an Object - thus contains a number of static methods e.g. valueOf(char),toUpperCase()
字符是一个对象 - 因此包含许多静态方法,例如 valueOf(char),toUpperCase()
where char is a primitive data-type
其中 char 是原始数据类型
回答by bmdeveloper
char is a primitive type and Character is a class that acts as a wrapper for char.
char 是一种原始类型,而 Character 是一个充当 char 包装器的类。
The point of the Character class is so you can apply a range of methods to your char if needed.
Character 类的重点是,如果需要,您可以将一系列方法应用于您的字符。
More information here http://docs.oracle.com/javase/tutorial/java/data/characters.html
更多信息在这里http://docs.oracle.com/javase/tutorial/java/data/characters.html