The Java language provides the wrapper class character class for the built-in data type Char
The character class provides a series of methods to manipulate characters. You can create a character class object using character's construction method
New Character (' a ');
In some cases, the Java compiler automatically creates a character object.
For example, when a parameter of type char is passed to a method that requires a character type parameter, the compiler automatically converts the char type parameter to the character object. This feature is called Boxing (boxing), which in turn is called unpacking (unboxing).
// the original character ' a ' is boxed into the Character object ch Character ch = ' a '; // The original character ' X ' is boxed with the test method // returns the value of the unboxing to ' C ' char c = Test (' x ');
Java Tutorial Java Tutorials Java Introduction Java Development environment Configuration Java Basic Syntax Java object and class Java basic data type Java variable type java modifier java operator Java Loop Structure Java branch structure Java number & Math class Java Character class Java String class java Stringbufferjava array java datetime Java Regular expression Java method Java Stream, File, Iojava Scanner class Java Exception handling
JavaObject OrientedJava inherited Java Override/overloadjava polymorphic Java abstraction Java wrapper Java Interface Java package
JavaAdvanced TutorialsJava Data Structure Java collection framework Java generic Java Serialization Java Network programming Java send mail Java multithreaded programming Java Applet Basic Java Documentation Comments Java Instance Java 8 new features Java MySQL connectionJava Number & Math classJava String class Java Character class
The Character class is used to manipulate individual characters.
The Character class wraps the value of a base type char in an object
InstanceChar Ch=‘A‘;//Unicode character representationChar Unichar=‘\u039A‘;//Character arraychar[] chararray ={ "a b ' , c ' d" ' e ' }
However, in the actual development process, we often encounter situations where objects need to be used rather than built-in data types. To solve this problem, the Java language provides the wrapper class character class for the built-in data type char.
The character class provides a series of methods to manipulate characters. You can create a character class object using character's construction method, for example:
Character ch = new Character('a');
In some cases, the Java compiler automatically creates a character object.
For example, when a parameter of type char is passed to a method that requires a character type parameter, the compiler automatically converts the char type parameter to the character object. This feature is called boxing, which in turn is called unpacking.
Instance//The original character ' a ' is boxed into the Character object chCharacter ch = 'a'; The original character ' X ' is boxed with the test method//Returns the value of the unboxing to ' C 'char c = test('x') ; Escape sequences
A character preceded by a backslash (\) represents an escape character, which has a special meaning for the compiler.
The following list shows the escape sequences for Java:
Escape Sequences |
Description |
\ t |
Insert a TAB key in the text |
\b |
Insert a back key in the text |
\ n |
The line wraps in the text. |
\ r |
Insert a carriage return at this point in the text |
\f |
Insert a page break at this point in the text |
\‘ |
Insert single quotation marks at the same place in the text |
\" |
Insert double quotation marks at this point in the text |
\\ |
Inserts a backslash in the text |
Instance
When a print statement encounters an escape sequence, the compiler can interpret it correctly.
The following instance escapes the double quotes and outputs:
Test.java File Code:Public Class Test {Public Static void main ( String args[ ]) {system. Out. Println ( " access \ " Rookie Tutorial! \ "" ) ;}}
The results of the above example compilation run as follows:
Visit "Rookie Tutorial!"
Character method
Here are the methods of the character class:
Serial Number |
Method and Description |
1 |
Isletter () Whether it is a letter |
2 |
IsDigit () Whether it is a numeric character |
3 |
Iswhitespace () Whether it is a space |
4 |
Isuppercase () Whether it is an uppercase letter |
5 |
Islowercase () Whether it is a lowercase letter |
6 |
toUpperCase () Specify the uppercase form of the letter |
7 |
toLowerCase () Specifies the lowercase form of the letter |
8 |
ToString () Returns the string form of a character with a string length of only 1 |
Java Character Class