Showing posts with label Character Class. Show all posts
Showing posts with label Character Class. Show all posts

Character constants in Java Part -VIII

Some of the constants of the Character class in Java.

1. Character.ENCLOSING_MARK

Syntax:

byte java.lang.Character.ENCLOSING_MARK: 7

General category "Me" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

        System.out.println(Character.ENCLOSING_MARK);
    }
}

Output:

7


2. Character.END_PUNCTUATION

Syntax:

byte java.lang.Character.END_PUNCTUATION: 22

General category "Pe" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

        System.out.println(Character.END_PUNCTUATION);

    }
}

Output:

22


3. Character.FINAL_QUOTE_PUNCTUATION

Syntax:

byte java.lang.Character.FINAL_QUOTE_PUNCTUATION: 30

General category "Pf" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

        System.out.println(Character.FINAL_QUOTE_PUNCTUATION);

    }
}

Output:

30


4. Character.FORMAT

Syntax:

byte java.lang.Character.FORMAT: 16

General category "Cf" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

    System.out.println(Character.FORMAT);

    }
}

Output:

16


5. Character.INITIAL_QUOTE_PUNCTUATION

Syntax:

byte java.lang.Character.INITIAL_QUOTE_PUNCTUATION: 29

General category "Pi" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

        System.out.println(Character.INITIAL_QUOTE_PUNCTUATION);
    }
}

Output:

29


6. Character.LETTER_NUMBER

Syntax:

byte java.lang.Character.LETTER_NUMBER: 10

General category "Nl" in the Unicode specification.

Java

public class CharacterConstants6 {
    public static void main(String[] args) {

        System.out.println(Character.LETTER_NUMBER);

    }
}

Output:

10


7. Character.TITLECASE_LETTER

Syntax:

byte java.lang.Character.TITLECASE_LETTER: 3

General category "Lt" in the Unicode specification.

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {

        System.out.println(Character.TITLECASE_LETTER);

    }
}

Output:

3


8. Character.UNASSIGNED

Syntax:

byte java.lang.Character.UNASSIGNED: 0

General category "Cn" in the Unicode specification.

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {

        System.out.println(Character.UNASSIGNED);

    }
}

Output:

0


9. Character.UPPERCASE_LETTER

Syntax:

byte java.lang.Character.UPPERCASE_LETTER: 1

General category "Lu" in the Unicode specification. 

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {

        System.out.println(Character.UPPERCASE_LETTER);

    }
}

Output:

1


Some more constants of Character class:


Character constants Part -I


Character constants Part -II


Character constants Part -III


Character constants Part - IV


Character constants Part -V


Character constants Part -VI


Character constants Part- VII


Character.valueOf() in Java

Character.valueOf(): This method is available in java.lang.Character class of Java.

Syntax:

Character java.lang.Character.valueOf(char c)

This method takes one argument of type char as its parameter. This method returns a Character instance representing the specified char value. This method will always cache values in the range '\u005Cu0000' to '\u005Cu007F', inclusive, and may cache other values outside of this range.

Parameters: One parameter is required for this method.

c: a char value.

Returns: a Character instance representing c.

Exceptions: NA

Approach

Java

public class CharactervalueOf {
    public static void main(String[] args) {

        char c = 'a';
        System.out.println(Character.valueOf(c));

    }
}

Output:

a

Character.toUpperCase() in Java

Character.toUpperCase(): This method is available in java.lang.Character class of Java.

Approach 1: When the argument is of type char.

Syntax:

char java.lang.Character.toUpperCase(char ch)

This method takes one argument of type char as its parameter. This method converts the character argument to uppercase using case mapping information from the UnicodeData file.

Parameters: One parameter is required for this method.

ch: the character to be converted.

Returns: the uppercase equivalent of the character, if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoUpperCasechar {
    public static void main(String[] args) {

        char ch = 'a';
        System.out.println(Character.toUpperCase(ch));
    }
}

Output:

A


Approach 2: When the argument is of type int.

Syntax:

int java.lang.Character.toUpperCase(int codePoint)

This method takes one argument of type int as its parameter. This method converts the character (Unicode code point) argument to uppercase using case mapping information from the UnicodeDatafile.

Parameters: One parameter is required for this method.

codePoint: the character (Unicode code point) to be converted.

Returns: the uppercase equivalent of the character, if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoUpperCaseint {
    public static void main(String[] args) {

        int codePoint = 'a';
        System.out.println(Character.toUpperCase(codePoint));
    }
}

Output:

65

Character.toTitleCase() in Java

Character.toTitleCase(): This method is available in java.lang.Character class of Java.

Approach 1: When the argument is of type char.

Syntax:

char java.lang.Character.toTitleCase(char ch)

This method takes one argument of type char as its parameter. This method converts the character argument to titlecase using case mapping information from the UnicodeData file.

Parameters: One parameter is required for this method.

ch: the character to be converted.

Returns: the titlecase equivalent of the character, if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoTitleCasechar {
    public static void main(String[] args) {

        char ch = 'a';
        System.out.println(Character.toTitleCase(ch));
    }
}

Output:

A


Approach 2: When the argument is of type int.

Syntax:

int java.lang.Character.toTitleCase(int codePoint)

This method takes one argument of type int as its parameter. This method converts the character (Unicode code point) argument to titlecase using case mapping information from the UnicodeData file.

Parameters: One parameter is required for this method.

codePoint: the character (Unicode code point) to be converted.

Returns: the titlecase equivalent of the character, if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoTitleCaseint {
    public static void main(String[] args) {

        int codePoint = 'a';
        System.out.println(Character.toTitleCase(codePoint));
    }
}

Output:

65

Character toString() in Java

toString(): This method is available in java.lang.Character class of Java.

Approach 1: When the method does not take any argument.

Syntax:

String java.lang.Character.toString()

This method returns a String object representing this Character's value. The result is a string of length 1 whose sole component is the primitive char value represented by this Character object.

Returns: a string representation of this object.

Exceptions: NA

Java

public class CharactertoString {

    public static void main(String[] args) {

        Character c = 'a';

        System.out.println(c.toString());
    }
}

Output:

a


Approach 2: When the method takes an argument of type char.

Syntax:

String java.lang.Character.toString(char c)

This method takes one argument of type char as its parameter. This method returns a String object representing the specified char. The result is a string of length1 consisting solely of the specified char.

Parameters: One parameter is required for this method.

c: the char to be converted.

Returns: the string representation of the specified char.

Exceptions: NA

Java

public class CharactertoString2 {
    public static void main(String[] args) {

        char c = 'a';
        System.out.println(Character.toString(c));
    }
}

Output:

a

Approach 3: When the method takes an argument of type int.

Syntax:

String java.lang.Character.toString(int codePoint)

This method takes one argument of type int as its parameter. This method returns a String object representing the specified character (Unicode code point). The result is a string of length 1 or 2, consisting solely of the specified codePoint.

Parameters: One parameter is required for this method.

codePoint: the codePoint to be converted.

Returns: the string representation of the specified codePoint.

Throws:

IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.

Java

public class CharactertoStringint {
    public static void main(String[] args) {

        int codePoint = 'A';
        System.out.println(Character.toString(codePoint));
    }
}

Output:

A

Approach 3.1: IllegalArgumentException

Java

public class CharactertoStringint {
    public static void main(String[] args) {

        int codePoint = -1;
        System.out.println(Character.toString(codePoint));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.String.valueOfCodePoint(String.java:3760) at java.base/java.lang.Character.toString(Character.java:8651)



Character.toLowerCase() in Java

Character.toLowerCase(): This method is available in java.lang.Character class of Java.

Approach 1: When the argument is of type char.

Syntax:

char java.lang.Character.toLowerCase(char ch)

This method takes one argument of type char as its parameter. This method converts the character argument to lowercase using case mapping information from the UnicodeData file.

Parameters: One parameter is required for this method.

ch: the character to be converted.

Returns: the lowercase equivalent of the character, if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoLowerCasechar {
    public static void main(String[] args) {

        char ch = 'A';
        System.out.println(Character.toLowerCase(ch));
    }
}

Output:

a


Approach 2: When the argument is of type int.

Syntax:

int java.lang.Character.toLowerCase(int codePoint)

This method takes one argument of type int as its parameter. This method converts the character (Unicode code point) argument to lowercase using case mapping information from the Unicode Datafile.

Parameters: One parameter is required for this method.

codePoint: the character (Unicode code point) to be converted.

Returns: the lowercase equivalent of the character (Unicode codepoint), if any; otherwise, the character itself.

Exceptions: NA

Java

public class CharactertoLowerCaseint {
    public static void main(String[] args) {

        int codePoint = 'A';
        System.out.println(Character.toLowerCase(codePoint));
    }
}

Output:

97

Character.toCodePoint() in Java

Character.toCodePoint(): This method is available in java.lang.Character class of Java.

Syntax:

int java.lang.Character.toCodePoint(char high, char low)

This method takes two arguments of type char as its parameters. This method converts the specified surrogate pair to its supplementary codepoint value.

Parameters: Two parameters are required for this method.

high: the high-surrogate code unit.

low: the low-surrogate code unit.

Returns: the supplementary code point composed from the specified surrogate pair.

Exceptions: NA

Approach

Java

public class CharactertoCodePoint {
    public static void main(String[] args) {

        char high = 'm', low = 'a';
        System.out.println(Character.toCodePoint(high, low));

    }
}

Output:

-56502175

Character.toChars() in Java

Character.toChars(): This method is available in java.lang.Character class of Java.

Approach 1: When the method takes one argument.

Syntax:

char[] java.lang.Character.toChars(int codePoint)

This method takes one argument of type int as its parameter. This method converts the specified character (Unicode code point) to itsUTF-16 representation stored in a char array.

Parameters: One parameter is required for this method.

codePoint: a Unicode code point.

Returns: a char array having codePoint's UTF-16 representation.

Throws:

IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.

Java

import java.util.Arrays;

public class ChraractertoChars1 {
    public static void main(String[] args) {

        int codePoint = 124;
        System.out.println(Arrays.toString(Character.toChars(codePoint)));
    }
}

Output:

[|]


Approach 1.1: IllegalArgumentException

Java

import java.util.Arrays;

public class ChraractertoChars1 {
    public static void main(String[] args) {

        int codePoint = -1;
        System.out.println(Arrays.toString(Character.toChars(codePoint)));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.Character.toChars(Character.java:9175)


Approach 2: When the method takes three arguments.

Syntax:

int java.lang.Character.toChars(int codePoint, char[] dst, int dstIndex)

This method takes three arguments, one of type char array and the other two are of type int. This method converts the specified character (Unicode code point) to itsUTF-16 representation.

Parameters: Three parameters are required for this method.

codePoint: the character (Unicode code point) to be converted.

dst: an array of char in which the codePoint's UTF-16 value is stored.

dstIndex: the start index into the dst array where the converted value is stored.

Returns: 1 if the code point is a BMP code point, 2 if thecode point is a supplementary code point

Throws:

1. IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.

2. NullPointerException - if the specified dst is null.

3. IndexOutOfBoundsException - if dstIndex is negative or not less than dst.length, or if dst at dstIndex doesn't have enough array element(s) to store the resulting char value(s). (If dstIndex is equal to dst.length-1 and the specified codePoint is a supplementary character, the high-surrogate value is not stored in dst[dstIndex].)

Java

public class CharactertoChars2 {
    public static void main(String[] args) {

        int codePoint = 1234;
        char[] dst = { 'H''e''l''l''o' };
        int dstIndex = 0;
        System.out.println(Character.toChars(codePoint, dst, dstIndex));

    }
}

Output:

1


Approach 2.1: IllegalArgumentException 

Java

public class CharactertoChars2 {
    public static void main(String[] args) {

        int codePoint = -1;
        char[] dst = { 'H''e''l''l''o' };
        int dstIndex = 0;
        System.out.println(Character.toChars(codePoint, dst, dstIndex));

    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.Character.toChars(Character.java:9146)


Approach 2.2: NullPointerException

Java

public class CharactertoChars2 {
    public static void main(String[] args) {

        int codePoint = 1234;
        char[] dst = null;
        int dstIndex = 0;
        System.out.println(Character.toChars(codePoint, dst, dstIndex));

    }
}


Output:

Exception in thread "main" java.lang.NullPointerException: Cannot store to char array because "dst" is null at java.base/java.lang.Character.toChars(Character.java:9139)


Approach 2.3 : IndexOutOfBoundsException

Java

public class CharactertoChars2 {
    public static void main(String[] args) {

        int codePoint = 1234;
        char[] dst = { 'H''e''l''l''o' };
        int dstIndex = -1;
        System.out.println(Character.toChars(codePoint, dst, dstIndex));

    }
}


Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 at java.base/java.lang.Character.toChars(Character.java:9139)


Character constants in Java Part -VII

Some of the constants of the Character class in Java.

1. Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING

Syntax:

byte java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING: 16

Strong bidirectional character type "RLE" in the Unicode specification.

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {

        System.out.println(Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING);

    }
}

Output:

16


2. Character.DIRECTIONALITY_RIGHT_TO_LEFT_ISOLATE

Syntax:

byte java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_ISOLATE: 20

Weak bidirectional character type "RLI" in the Unicode specification. 

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {
        System.out.println(Character.DIRECTIONALITY_RIGHT_TO_LEFT_ISOLATE);

    }
}

Output:

20


3. Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE

Syntax:

byte java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE: 17

Strong bidirectional character type "RLO" in the Unicode specification.

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {

        System.out.println(Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE);

    }
}

Output:

17


4. Character.DIRECTIONALITY_SEGMENT_SEPARATOR

Syntax:

byte java.lang.Character.DIRECTIONALITY_SEGMENT_SEPARATOR: 11

Neutral bidirectional character type "S" in the Unicode specification.

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {

        System.out.println(Character.DIRECTIONALITY_SEGMENT_SEPARATOR);

    }
}

Output:

11


5. Character.DIRECTIONALITY_UNDEFINED

Syntax:

byte java.lang.Character.DIRECTIONALITY_UNDEFINED: -1

Undefined bidirectional character type. Undefined char values have undefined directionality in the Unicode specification.

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {

        System.out.println(Character.DIRECTIONALITY_UNDEFINED);

    }
}

Output:

-1


6. Character.DIRECTIONALITY_WHITESPACE

Syntax:

byte java.lang.Character.DIRECTIONALITY_WHITESPACE: 12

Neutral bidirectional character type "WS" in the Unicode specification.

Approach

Java

public class CharacterConstants5 {
    public static void main(String[] args) {
        System.out.println(Character.DIRECTIONALITY_WHITESPACE);

    }
}

Output:

12


7. Character.SPACE_SEPARATOR

Syntax:

byte java.lang.Character.SPACE_SEPARATOR: 12

General category "Zs" in the Unicode specification.

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {

        System.out.println(Character.SPACE_SEPARATOR);

    }
}

Output:

12


8. Character.START_PUNCTUATION

Syntax:

byte java.lang.Character.START_PUNCTUATION : 21

General category "Ps" in the Unicode specification.

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {

        System.out.println(Character.START_PUNCTUATION);

    }
}

Output:

21


9. Character.SURROGATE

Syntax:

byte java.lang.Character.SURROGATE: 19

General category "Cs" in the Unicode specification. 

Approach

Java

public class CharacterConstants9 {
    public static void main(String[] args) {
        System.out.println(Character.SURROGATE);

    }
}

Output:

19


Some more constants of Character class:


Character constants Part -I


Character constants Part -II


Character constants Part -III


Character constants Part - IV


Character constants Part -V


Character constants Part -VI


Character constants Part -VIII