Char constant in java

Class Character

The Character class wraps a value of the primitive type char in an object. An object of class Character contains a single field whose type is char .

In addition, this class provides a large number of static methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Unicode Conformance

The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.

Character information is based on the Unicode Standard, version 15.0.

The Java platform has supported different versions of the Unicode Standard over time. Upgrades to newer versions of the Unicode Standard occurred in the following Java releases, each indicating the new version:

Shows Java releases and supported Unicode versions
Java release Unicode version
Java SE 20 Unicode 15.0
Java SE 19 Unicode 14.0
Java SE 15 Unicode 13.0
Java SE 13 Unicode 12.1
Java SE 12 Unicode 11.0
Java SE 11 Unicode 10.0
Java SE 9 Unicode 8.0
Java SE 8 Unicode 6.2
Java SE 7 Unicode 6.0
Java SE 5.0 Unicode 4.0
Java SE 1.4 Unicode 3.0
JDK 1.1 Unicode 2.0
JDK 1.0.2 Unicode 1.1.5
Читайте также:  Тег top в css

Variations from these base Unicode versions, such as recognized appendixes, are documented elsewhere.

Unicode Character Representations

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)

The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).

  • The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter(‘\uD840’) returns false , even though this specific value if followed by any low-surrogate value in a string would represent a letter.
  • The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).

In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Источник

Class Character

The Character class wraps a value of the primitive type char in an object. An object of class Character contains a single field whose type is char .

In addition, this class provides a large number of static methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Unicode Conformance

The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.

Character information is based on the Unicode Standard, version 13.0.

The Java platform has supported different versions of the Unicode Standard over time. Upgrades to newer versions of the Unicode Standard occurred in the following Java releases, each indicating the new version:

Shows Java releases and supported Unicode versions
Java release Unicode version
Java SE 15 Unicode 13.0
Java SE 13 Unicode 12.1
Java SE 12 Unicode 11.0
Java SE 11 Unicode 10.0
Java SE 9 Unicode 8.0
Java SE 8 Unicode 6.2
Java SE 7 Unicode 6.0
Java SE 5.0 Unicode 4.0
Java SE 1.4 Unicode 3.0
JDK 1.1 Unicode 2.0
JDK 1.0.2 Unicode 1.1.5

Variations from these base Unicode versions, such as recognized appendixes, are documented elsewhere.

Unicode Character Representations

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)

The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).

  • The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter(‘\uD840’) returns false , even though this specific value if followed by any low-surrogate value in a string would represent a letter.
  • The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).

In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Источник

Constants in Java | Types, Example

Scientech Easy

A value which is fixed and does not change during the execution of a program is called constants in java.

In other words, Java constants are fixed (known as immutable) data values that cannot be changed.

Java supports various types of constants. They are as follows:

Types of constants in java

Integer Constants in Java

An integer constant is a sequence of digits without a decimal point. For example, 10 and -200 are integer constants. There are three types of integer constants. They are as follows:

Decimal Integer:

Decimal integer is a sequence of digits from 0 to 9. These constants are either positive or negative. Plus sign is optional.

For example, 24, -55, 0, and +3425 are valid examples of decimal integer constants in decimal notation.

Non-digit characters, embedded spaces, and commas are not allowed between digits. For example, 12 3450, 20.00, $1200 are illegal numbers.

Octal Integer:

An octal integer constant is a sequence of any combination of digits from 0 to 7. Octal integer always starts with 0. The digits 8 and 9 are not valid in octal notation.

The examples of valid integer constants in octal notation are 024, 0, 0578, and 0123.

Hexadecimal Integer:

Hexadecimal integer constants consist of digits 0 to 9 and alphabets “a” to “f” or “A” to “F”. These constants are preceded by 0x. Letter “A” to “F” represents the numbers 10 to 15.

The valid examples of hexadecimal integer constants are 0x23, 0x5B, 0x9F, 0x, etc. “x” can also be either small or capital.

We rarely use octal and hexadecimal integer numbers system in programming.

Let’s create a program to study decimal, octal, and hexadecimal notation for integers.

Program code 1:

package constantsPrograms; public class IntegerConst < public static void main(String[] args) < int a, b, c; a = 20; // decimal notation. b = 020; // octal notation. c = 0x20F; // hexadecimal notation. System.out.println("Decimal notation 20: " +a); System.out.println("Octal notation 020: " +b); System.out.println("Hexadecimal notation 0x20F: " +c); >>
Output: Decimal notation 20: 20 Octal notation 020: 16 Hexadecimal notation 0x20F: 527

Real (Floating-point) Constants in Java

Real constants consist of a sequence of digits with fractional parts or decimal points. These constants are also called floating-point constants. The valid examples of real constants are 2.3, 0.0034, -0.75, 56.7, etc.

These numbers are shown in the decimal point notation. It is also possible that the number may not have digits before decimal point or digits after the decimal point. For example, 205., .45, -.30, etc.

A real constants number can also be expressed in exponential or scientific notation. For example, the value 235.569 can also be written as 2.35569e2 in exponential notation. Here, e2 means multiply by 10^2.

It has the following general form:

mantissa is either integer number or a real number expressed in decimal notation. The term exponent is an integer with an optional plus or minus sign.

The letter “e” is used to separate mantissa and exponent which can be written in either lowercase or uppercase. The valid examples of floating points constants are 0.54e2, 12e-4, 1.5e+5, 2.13E4, -1.5E-4, etc.

Embedded (blank) space is not permitted in any numeric constants. Exponential notation is useful to represent either very small or very large number in magnitude.

For example, 250000000 can also be written as 2.5e8 or 25E7. Similarly, -0.000000021 is equivalent to -2.1e-8.

Single Character Constants in Java

A single character constant ( or simply character constant) is a single character enclosed within a pair of single quote. The example of single-character constants are as follows:

The last constant is blank space. The character constant ‘5’ is not equivalent to the number 5.

String Constants

A string constant is a sequence of characters within a pair of double-quotes. The characters can be alphabets, special characters, digits, and blank spaces. The valid examples of string constants are given below:

“Hello Java” “1924” “?…!” “2+7” “X” etc.

Backslash Character Constants

Java provides some special backslash character constants that are used in output methods. For example, the symbol ‘n’ which stands for newline character.

There are various types of backslash character constants that are supported by java. The list is given in the below table.

Table: Backslash Character Constants

Constants Meaning
‘ b ‘ back space
‘ f ‘ form feed
‘ n ‘ new line
‘ r ‘ carriage return
‘ t ‘ horizontal tab
‘ ‘ ‘ single quote
‘ ” ‘ double quote
‘ ‘ backslash

Hope that this tutorial has covered almost all important points related to constants in java with example programs. I hope that you will have understood this tutorial and enjoyed it.
Thanks for reading.
Next ⇒ Operators in Java ⇐ PrevNext ⇒

Источник

Оцените статью