Java - public int indexOf (int ch)

I came across the following:

public int indexOf(int ch)

      

by http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int) when I was revisiting some String related Java concepts.

According to my knowledge, when we use the method indexOf()

for java.lang.String

, the parameter should be char

, and therefore I assumed that it

public int indexOf(char ch)

      

So please explain to me why this is public int indexOf(int ch)

.

+3


source to share


2 answers


Unicode contains much more than 2 ^ 16 characters. Java 'char' and 'String' use the Unicode transform format (UTF-16) to represent the complete character set. Basic multilingual characters are represented as a single 16-bit 'char'. The rest are represented by a surrogate pair: two special 16-bit values ​​from the set reserved for this purpose.

An alternative representation is UTF-32. In this representation, each character represents one 32-bit element, a period.

For example, Cuneiform is in SMP; the first character of the block is U + 12000. In UTF-32, this is only 0x12000. In UTF-16 it is "\uD808\uDC00"

. Here are some pictures.



The Character

and classes String

(among others) provide several methods that work with UTF-32 characters for convenience. You ask about one of them. Whenever you see "int" as the datatype of a character, this is what the "int" contains: a UTF-32 value. It's not hard to see how convenient it is to perform some operations on a single UTF-32 value instead of a couple of surrogates.

Please note that this has nothing to do with composed and inconsistent accents. Γ‘ can be represented in Unicode as one or two UTF-16 characters, but there are no surrogates. All three of U + 0061 (a), U + 00E1 (a with a preset accent), and U + 0301 (constituting acute accent) are normal BMP characters. So, even in UTF-32, you can have a sequence of two elements: U + 0061, U + 0301.

The ICU4J library provides a more complete set of UTF-32 classes and methods.

+3


source


Each char has an int value which can be used to get that char and also you can convert the chart to int in the same way by assigning char to int try the following lines

char ch = 65;
System.out.println(ch);
int i = 'A';
System.out.println(i);

      



I am using char values ​​in a loop and this is only allowed because every char is int. try this code, it will print alphabets from A to Z and its equivalent int values

    for(char j = 'A'; j <= 'Z'; j++){
        System.out.println("int "+((int) j)+" = "+j);
    }

      

+1


source







All Articles