Character Sets used in Java
Java language uses letters, digits, operators and delimiters. It uses Unicode to represent characters.
Unicode is a standard encoding system created by Unicode consortium that is used to encode a character in any computer language.
Unicode uses 16 bits to represent a character.
Advantages of Unicode
- It is the universal coding scheme.
- More efficient than ISO or IEC.
- Supports uniform coding width for all characters.
- Character codes are unique for each character.
ASCII Character Codes
ASCII stands for American Standard Code for Information Interchange. It uses 7 bits to represent a character.
ASCII code of white space blank is 32.
Difference between Unicode and ASCII
|It is a generalized form of coding scheme for numerous characters of different scripts.||It is a specific coding scheme used for limited number of characters.|
|It represents higher range of codes.||It represents limited range of codes.|
They are the non-graphic characters, used as commands to direct the cursor while printing. Escape sequences always begin with a backslash (\) character.
Following are some of the escape sequences:
- \t for Horizontal tab
- \v for Vertical tab
- \n for New line
- \’ for Single quote
- \” for Double quote
- \b for Backspace
- \f for Form feed
- \r for Carriage return
- \0 for Null
- \\ for Backslash
Tokens are the individual components of a Java statement.
Following are the various types of tokens:
- Literals or Constants: These are the fixed values that do not change during program execution.
- Identifiers: These are the variables used in a Java program.
- Assignments: The = sign is an assignment as it helps to store values in a variable.
- Punctuators: Special characters like question mark (?), dot (.), semicolon (;) are punctuators.
- Separators: Special characters used to separate various parts of a Java program are separators.
- Operators: These are symbols that perform some calculation or operation with values.
- Keywords: These are reserved words which have special meaning for the compiler.
Initializing a Variable
Initialization of a variable can be done in one of two ways:
- Static initialization: It uses direct assignment of a constant to a defined variable.
- Dynamic initialization: It occurs when a variable gets initialized at run time.
Data Types in Java
There are two types of data types in Java: Primitive and Composite.
Primitive Data Type: These are the predefined data types. Following are the primitive data types:
- byte (1 byte)
- short (2 bytes)
- char (2 bytes)
- int (4 bytes)
- long (8 bytes)
- float (4 bytes)
- double (8 bytes)
- boolean (1 byte)
Converting one data type to another is called type conversion.
There are two types of type conversion:
- Implicit: Conversion that takes place automatically into higher type is implicit type conversion. It is also known as coercion.
- Explicit: Conversion that takes place by the user by force is explicit type conversion.
Multiple Choice Questions:
- A constant which gives the exact representation of data is called:
- A word used in a high-level language which has a special meaning attached to it is called:
- A character literal is assigned to a:
a. char variable
b. char type literal
c. String variable
d. String literal
- A character literal is enclosed in:
a. ‘ ‘
b. ” ”
- A set of characters is assigned to:
a. String variable
b. static variable
c. boolean variable
- The ASCII codes of uppercase alphabets range from:
a. 65 – 90
b. 60 – 85
c. 65 – 91
d. 97 – 122
- Which of the following results in integer type?
a. 11.4F / 32.D
b. 13.8F / 4.6F
c. 12 / 3
- Which of the following is non-primitive data?
- Which of the following type is an exact representation of fractional values?
- boolean data is used to test a particular condition i.e. true or false. Which of the following is a correct representation?
a. boolean m = true
b. boolean m = ‘true’
c. boolean m = “true”
Fill in the blanks:
- The character sets of Java is like alphabets of English language.
- A standard encoding system of representing characters is Unicode.
- ASCII code is the decimal representation to represent a character.
- Each individual component of a Java statement is known as tokens.
- In Java, the constants are also called literals.
- Assignment operator is used to store a value in the variable.
- The comma, exclamation, question mark, etc. are termed as punctuators in Java language.
- An element of Java program that is used to identify a class, function or value is called as identifier.
- Integer type value occupies 4 bytes in memory.
- A Java expression that contains all the elements of same data type is pure expression.