[英]What is the difference between char and Character in Java?
我需要知道Java中的char和Character之间的区别是什么,因为当我创建一个java程序时,char工作而Character没有工作。
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的例子,
char ch = 'a';
Example of Character, 人物的例子,
Character.toUpperCase(ch);
It converts 'a' to 'A' 它将'a'转换为'A'
From the JavaDoc : 来自JavaDoc :
The Character class wraps a value of the primitive type char in an object.
Character类在对象中包装基本类型char的值。 An object of type Character contains a single field whose type is char.
Character类型的对象包含一个类型为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.
字符信息基于Unicode标准版本6.0.0。
So, char is a primitive type while Character is a class. 因此,char是一种原始类型,而Character是一个类。 You can use the Character to wrap char from static methods like
Character.toUpperCase(char c)
to use in a more "OOP way". 您可以使用Character从像
Character.toUpperCase(char c)
这样的静态方法中包装char,以便以更“OOP方式”使用。
I imagine in your program there was an 'OOP' mistake(like init of a Character) rather than char vs Character mistake. 我想在你的程序中有一个'OOP'错误(比如一个字符的init)而不是char vs Character错误。
Character is an Object - thus contains a number of static methods eg valueOf(char),toUpperCase() Character是一个Object - 因此包含许多静态方法,例如valueOf(char),toUpperCase()
where char is a primitive data-type 其中char是原始数据类型
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类的要点是,如果需要,您可以将一系列方法应用于char。
More information here http://docs.oracle.com/javase/tutorial/java/data/characters.html 更多信息请访问http://docs.oracle.com/javase/tutorial/java/data/characters.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.