Getting to know Java (Java wrapper classes - Byte and Character)

One, Byte

    The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains only one field of type byte. In addition, this class provides methods for converting between bytes and Strings, and provides other constants and methods that are useful when dealing with bytes.

    1.1 Constructor

    The Byte class provides overloaded forms of the following two constructors to create Byte class objects.

    ( 1 ) Byte ( byte value )

    The Byte object created by this method can represent the specified byte value.

    eg : Create a Byte object with a byte variable as a parameter.

byte mybyte = 45;
Byte b = new Byte(mybyte);

    ( 2 )Byte ( String str )

    The Byte object created by this method can represent the byte value indicated by the String parameter.

    eg : Create a Byte object with a String variable as a parameter.

Byte mybyte = new Byte("12");

    Use a numeric String variable as a parameter, such as 123, otherwise a NumberFormatException will be thrown.

    1.2 Common methods

    Common methods of the Byte class are as follows:

Common methods of the Byte class
method return value Function description
byteValue() byte Returns the Byte object as a byte value
compareTo(Byte anotherByte) int Numerically compares two Byte objects
doubleValue() double returns the value of this Byte as a double
intValue() int Returns the value of this Byte as an int
parseByte (String s) byte Parse a String parameter into the equivalent byte ( byte )
toString() String returns a String object representing the value of this Byte
valueOf(String str) Byte Returns a Byte object holding the value given by the specified String
equals(Object obj) boolean

compares this object to the specified object,

Returns true if the object on which this method is called is equal to obj, otherwise returns false

    1.3 Constants

    Four constants are provided in the Byte class:

    ( 1 ) MIN_VALUE : the minimum value that the byte type can take.

    (2) MAX_VALUE: the maximum value that the byte type can take.

    (3) SIZE: The number of bits used to represent the byte value in two's complement form.

    (4) TYPE: represents the Class instance of the basic type byte.

2. Character

    The Character class wraps a value of primitive type char in an object. An object of type Character contains a single field of type char. Several methods are provided to determine the class of a character (lowercase letters, numbers, etc.) and convert characters from uppercase to lowercase and vice versa.

    2.1 Constructor

    The constructor of the Character class has the following syntax:    

    ( 1 ) Character ( char value )

    The constructor of this class must be a data of type char. The Character class object created through this constructor contains the value provided by the char type parameter. Once the Character class is created, the values ​​it contains cannot be changed.

    eg : Create a Character object with a char variable as a parameter.

Character mychar = new Character('s');

    2.2 Common methods

    The Character class provides many methods to perform operations on characters. Common methods are as follows:

Common methods of the Character class
method return value Function description
charValue () cahr returns the value of this Character object
compareTo(Character anotherCharacter) int 根据数值比较两个 Character 对象,若这两个对象相等则返回 0
equals(Object obj) Boolean 将调用该方法的对象与指定的对象相比较
toUpperCase(char ch) char 将字符参数转换为大写
toLowerCase(char ch) char 将字符参数转换为小写
toString() String 返回一个表示指定 char 值的 String 对象
charValue() char 返回此 Character 对象的值
isUpperCase(char ch) boolean 判断指定字符是否是大写字符
isLowerCase(char ch) boolean 判断指定字符是否是小写字符

    eg :创建类,在主方法中创建 Character 类的对象,并判断字符的大小写状态。

public class UpperOrLower{                                 //创建类
    public static void main(String[] args){                //主方法
        Character mychar1 = new Character('A');            //声明 Character 对象
        Character mychar2 = new Character('B');            //声明 Character 对象
        System.out.println(mychar1 +" 是大写字母吗?" + Character.isUpperCase(mychar1));
        System.out.println(mychar2 +" 是小写字母吗?" + Character.isLowerCase(mychar2));
    }
}

    The running result is:

Is A an uppercase letter? true
Is B a lowercase letter? false
    2.3 Constants

    The Character class provides a number of constants that represent specific characters. E.g:

    ( 1 ) CONNECTOR_PUNCTUATION : Returns a value of type byte, representing the general category "Pc" in the Unicode specification.

    (2) UNASSIGNED: Returns a value of type byte, representing the general category "Cn" in the Unicode specification.

    (3) TITLECASE_LETTER: Returns a value of type byte, representing the general category "Lt" in the Unicode specification.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631650&siteId=291194637