欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java常用类之Character类

程序员文章站 2024-03-04 21:27:42
...

Character字符型类

  • Character是char基本类型的包装类,它包含一个类型为char的字段

Character类中的常量

/**
 * 
 */
package CommonClasses;

/**
*  @Description     Character字符类常量
*  @author          枳洛淮南
*  @version         
*  @Date            2020年10月3日下午12:40:03
*/
public class CharacterConstant
{
	public static void main(String[] args)
	{
		System.out.println("char类型中最大值,为'\\uFFFF:" + Character.MAX_VALUE);
		System.out.println("char类型中最小值,为'\\u0000;" + Character.MIN_VALUE);
		System.out.println("char类型的无符号二进制位数:" + Character.SIZE);
		System.out.println("char类型的Class对象:" + Character.TYPE);
		System.out.println("Unicode规范中的'Co'类别:" + Character.PRIVATE_USE);
	}
}

运行结果:
Java常用类之Character类

Character类构造方法

Character类只提供了一种构造方法:Character(char value)
代码如下:

/**
 * 
 */
package CommonClasses;

/**
*  @Description     Character构造方法
*  @author          枳洛淮南
*  @version         版本
*  @Date            2020年10月3日下午12:48:54
*/
public class CharacterConstryctor
{
	@SuppressWarnings("deprecation")
	public static void main(String[] args)
	{
		Character c = new Character('c');
		System.out.println("以char类型为入参构造Character对象:" + c);
	}
}

运行结果:
Java常用类之Character类

Character类中的常用方法

代码如下:

/**
 * 
 */
package CommonClasses;

/**
*  @Description     Character常用方法
*  @author          枳洛淮南
*  @version         版本
*  @Date            2020年10月3日下午12:52:45
*/
public class CharacterMethod
{
	@SuppressWarnings("deprecation")
	public static void main(String[] args)
	{
		Character c1 = new Character('c');
		Character c2 = new Character('c');
		Character c3 = new Character('b');
		Character c8 = new Character('d');
		char c4 = new Character('c');
		char c5 = new Character('c');
		char c6 = new Character('b');
		char c7 = new Character('d');

		System.out.println("比较此对象与指定对象值是否相等:" + c1.equals(c2) + " " + c1.equals(c3) + " " + c1.equals(c1));
		System.out.println("(静态方法)比较两个char对象:" + Character.compare(c4, c5) + " " + Character.compare(c4, c6) + " "
				+ Character.compare(c4, c7));
		System.out.println("比较两个Character对象:" + c1.compareTo(c2) + " " + c1.compareTo(c3) + " " + c1.compareTo(c8));
		System.out.println("(静态方法)返回指定char数值的Character对象:" + Character.valueOf(c4));
		System.out.println("返回该Character对象值的String:" + c1.toString());
		System.out.println("(静态方法)返回指定char数值的String对象:" + Character.toString(c7));
		System.out.println("返回该Character对象的char基本类型值:" + c1.charValue());
		System.out.println("(静态方法)判断入参字符是否小写:" + Character.isLowerCase(c7) + " " + Character.isLowerCase('A'));
		System.out.println("(静态方法)判断入参字符是否大写:" + Character.isUpperCase(c7) + " " + Character.isUpperCase('A'));
		System.out.println("(静态方法)判断入参字符是否为空格:" + Character.isWhitespace(' ') + Character.isWhitespace('\t')
				+ Character.isWhitespace(c7));
		System.out.println("(静态方法)将小写字符转换为大写:" + Character.toLowerCase('A'));
		System.out.println("(静态方法)将大写字符转换为小写:" + Character.toUpperCase(c7));
	}
}

运行结果:
Java常用类之Character类