Java. Class Character. Review of methods and constants

Class Character. Review of methods and constants. Class constructor


Contents


Search other resources:

1. Class Character. Constructor Character(char)

The Character class wraps the char type. The class contains a constructor, which has the following general form:

public Character(char value);

here

  • value – the value passed to the constructor.

Example. Ways for initializing a variable of the Character type are given.

// Initializing a variable of type Character when creating an object of a class
// Method 1 - using the new operator and constructor
Character ch1 = new Character('f');
System.out.println(ch1.charValue());

// Method 2 - without using the new operator and constructor
Character ch2 = 'z';
System.out.println(ch2.charValue());

 

2. Methods and constants of the Character class. Overview

The following are the main methods of the Character class without listing the methods that support Unicode code points:

  • charValue() – returns the character wrapped in Character type;
  • equals() – compare the current object with another object of type Character;
  • hashCode() – get the hash code of the object;
  • compareTo() – compare the current symbol with another symbol;
  • equals() – compare the current object with another object for equality;
  • toString() – get a representation of a character value as a String type;
  • compare() – compare two char values;
  • digit() – return the numerical value of a character in the specified number system;
  • fordigit() – determine the symbolic representation of a number in a given number system;
  • isAlphabetic() – determine if a character is alphabetic;
  • isDefined() – determines if the character is defined in the Unicode encoding;
  • idDigit() – determines if the character is a digit;
  • isIdentifierIgnorable() – determines whether the specified symbol should be considered as ignored;
  • isLowerCase() – determines whether the specified character is a lowercase letter;
  • isSpace() – determines if the given character is a space based on the ISO-Latin1 code page;
  • isUpperCase() – determines if the specified character is an uppercase character;
  • isWhiteSpace() – determines if a specific character is a space according to the Java classification;
  • reverseBytes() – returns the value obtained by reversing the byte order in the given char value;
  • valueOf() – returns an instance of type Character representing a specific value of type char.

In addition to methods, the Character class defines a number of the constants:

  • BYTES – contains the size of the char type in bytes;
  • MAX_RADIX – the maximum radix available for converting to a string and vice versa;
  • MAX_VALUE – the maximum available char value;
  • MIN_VALUE – the minimum allowed value of the char type;
  • SIZE – size of the type in bits;
  • TYPE – contains an instance of a class of type Class for type char.

This topic covers the basic methods of the Character class. This overview does not include some of the methods that use Unicode code points and override the methods of the Object class.

 

3. Method charValue(). Get a character of type char

The charValue() method returns a primitive char, represented by an object of type Character. According to the Java documentation, the general form of declaring this method is as follows:

public char charValue();

Example.

// Method charValue(), char <= Character

// 1. Create an object of type Character
Character objC = '+';

// 2. Get a character of type char
char c;
c = objC.charValue();

// 3. Display the result
System.out.println(c);

 

4. Method compareTo(). Compare the current character with another character

If you need to compare the current symbol with some other symbol, the compareTo() method is used. The syntax for declaring a method is as follows:

public int compareTo(Character anotherCharacter);

here anotherCharacter is another character to compare to the character from the current instance.

If the characters are equal, then the function returns 0. If the character of the current instance is less than anotherCharacter, then a value less than 0 is returned. “Less than” means that the character code of the current instance is less than anotherCharacter character code. If the character code of the current instance is greater than the character code passed by the parameter, the function returns a value greater than 0.
In fact, the function returns the difference between the current character code and the character code that is an input parameter.

Example.

// Method compareTo() - compare character codes.

// 1. Create an object of type Character
Character objC = 'G';

// 2. Compare the current object with other characters.
int result;
result = objC.compareTo('C'); // result = 4
result = objC.compareTo('G'); // result = 0

Character objC2 = 'F';
result = objC.compareTo(objC2); // result = 1

 

5. Method toString(). Get a representation of a character value as a String

The toString() method overrides the method of the same name of the Object class. The method allows you to get a string representation of the char stored in the current instance. The length of the resulting string is 1. The general form of the method declaration is as follows

public String toString();

Example.

// Method toString() - get a representation of a character value as a String type

// 1. Create an object of Character type
Character objC = '8';

// 2. Call the method toString()
String resStr;
resStr = objC.toString(); // resStr = "8" - строка

 

6. Constant BYTES. Read the size of the type

The BYTES constant can be used to get the size, in bytes, of the char represented in the current object in unsigned binary notation.
According to Java documentation, constant is declared as

public static final int BYTES;

Example.

// Constant BYTES - get size of char type in bytes

// 1. Create an object of type Character
Character objC = '+';

// 2. Call the BYTES constant
int size = objC.BYTES; // size = 2

 

7. Constant MAX_RADIX. The maximum value of the numeral system

The MAX_RADIX constant returns the maximum number system that can be converted to strings and vice versa. According to the Java documentation, a constant declaration is of the form

public static final int MAX_RADIX;

Example.

// MAX_RADIX constant - get the maximum number system value

// 1. Create an object of Character type
Character objC = 'T';

// 2. Call the MAX_RADIX constant
int maxR = objC.MAX_RADIX; // maxR = 36

 

8. Constant MAX_VALUE. Maximum allowed value of the char type

The constant MAX_VALUE has a declaration

public static final char MAX_VALUE;

and it returns the maximum char value, which is ‘\ uFFFF’.

Example.

// Constant MAX_VALUE - get the maximum value of the char type

// 1. Create the object of Character type
Character objC = 'f';

// 2. Use the MAX_VALUE constant
int maxV = objC.MAX_VALUE; // maxV = 65535

 

9. Constant MIN_VALUE. Minimum allowed char value

The MIN_VALUE constant returns the minimum allowed char value, which is ‘\ u0000’. The constant declaration is as follows:

public static final char MIN_VALUE;

Example.

// Constant MIN_VALUE - get the minimum value of the char type

// 1. Create an object of type Character
Character objC = 'q';

// 2. Use the MIN_VALUE constant
int minV = objC.MIN_VALUE; // minV = 0

 

10. Constant SIZE. Size of the type in bits

Using the SIZE constant, you can determine the size of a char value in bits, which is 16. The declaration of the constant is as follows

public static final int SIZE

Example.

// Constant SIZE - size of char value in bits

// 1. Create an object of type Character
Character objC = '%';

// 2. Use the SIZE constant
int size = objC.SIZE; // size = 16

 

11. Constant TYPE. Type in class

The type encapsulated in the Character class is defined in the TYPE constant. This constant has the following declaration

public static final Class<Character> TYPE

Example.

// 1. Use the TYPE constant
Class<Character> tp = Character.TYPE; // tp = char

 

12. The static method compare(). Compare two char values

The compare() method is static and is used to compare any two char values. The codes of these values are compared. The general form of a method declaration is as follows:

public static int compare(char x, char y);

The method returns 0 if the x and y values are equal. The method returns a value less than 0 if x<y. This assumes a comparison of the character code x with the character code y. The method returns a value greater than 0 if x>y. Again, character codes are compared.

The formula for calculating the difference between x and y is as follows:

Character.valueOf(x).compareTo(Character.valueOf(y));

Example.

// Method compare() - compare character codes

// 1. Compare character codes taken by the Character wrapper
Character objC1 = 'k';
Character objC2 = 'm';

// 2. Invoke the compare() method
int result = Character.compare(objC1, objC2); // result = -2

// 3. Invoke the compare() for primitive types
char c = 'k';
result = Character.compare(c, objC1); // result = 0
result = Character.compare('8', '4'); // result = 4

 

13. The static method digit(). Return the numeric value of a character in a given number system

The static method digit() returns the value of a character of type char in the specified numeral system. General form of method declaration

public static int digit(char ch, int radix);

here

  • ch – the character to be converted to numeric representation;
  • radix – the base of a system of numeration. The radix value must be between MIN_RADIX and MAX_RADIX.

If the radix value is out of range, the function returns –1. If the value of the ch character is not a persistent digit, the function returns -1. A persistend digit is a digit (or letter) included in the set of digits of a given radix number system.
For example, in the 16th number system, there is the following set of numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. If you set ch = ‘ K ‘, then the function will return –1.

Example.

// Method digit() - get the numeric representation of a character in a given number system

// 1. Using method for the instance of type Character
Character objC = '8';

// Number 8 in decimal notation
int res = Character.digit(objC, 10); // res = 8

// The number 8 in decimal notation.
res = Character.digit(objC, 2); // res = -1 - error

// 2. Using a method for the char type
// Number 5 in the number system with base 7.
res = Character.digit('5', 7); // res = 5

// The number F in base 16.
res = Character.digit('F', 16); // res = 15

// The number F in the base 10 number system.
res = Character.digit('F', 10); // res = -1 - error

 

14. The static method forDigit(). Determine the symbolic representation of a number in a given number system

This method allows you to get a symbol based on its numeric representation in a given number system. This method performs the opposite function of the digit() method.

General form of method declaration

public static char forDigit(int digit, int radix);

here

  • digit – an integer, which is the value in the radix number system;
  • radix – the number system in which the digit is converted to the resulting character.

The radix value must be between MIN_RADIX and MAX_RADIX. If digit is not in the radix character set, then the zero value ‘\u0000’ is returned. Function returns the same if the radix value is invalid.

Example.

// Method forDigit() - get the symbolic representation of a number in a given number system

// Number 10 in the base 12 number system
char symbol = Character.forDigit(10, 12); // symbol = 'a'

// Number 15 with base 16.
symbol = Character.forDigit(15, 16); // symbol = 'f'

// Number 8 in the number system 3.
symbol = Character.forDigit(8, 3); // symbol = '\u0000' - error

// Number 12 in base 10 number system.
symbol = Character.forDigit(12, 10); // symbol = '\u0000' - error

// Number 3 in base 10 number system.
symbol = Character.forDigit(3, 10); // symbol = '3'

 

15. Method reverseBytes(). Reverse bytes in a char value

The static reverseBytes() method returns an input char value with bytes in reverse order. According to the Java documentation, the method declaration is as follows

public static char reverseBytes(char ch);

here

  • ch – the value, the bytes of which are to be represented in reverse order.

Example.

// Method reverseBytes() - get the reversed value of a char.
char c1 = '\u3800';
char c2 = Character.reverseBytes(c1); // c2 = '\u0038' = '8'
System.out.println("c2 = " + c2); // c2 = 8

 

16. Method valueOf(). Get an instance of the Character type

The valueOf() method returns an instance of type Character based on a value of type char.

The general syntax of method declaring is:

public static Character valueOf(char c)

here

  • c – the character to be wrapped in a Character wrap.

Example.

// Method valueOf() - get the instance of type Character
Character objC = Character.valueOf('x');

char ch = objC.charValue(); // ch = 'x'

 


Related topics