Identifiers and Literals | ICSE Class 9 Computer Applications Notes
On this page
This note covers Java character sets, ASCII and Unicode, escape sequences, tokens, identifiers, literals, constants and variables, primitive and non-primitive data types, storage sizes, and implicit and explicit type conversions.
What are characters, character sets and character codes?
A program is a set of instructions that a computer executes to perform a task. A programming language supplies the rules for writing those instructions. Java is a programming language; its written instructions form the source code.
A character is a letter, digit, punctuation mark or other textual symbol. A character set is a collection of characters available for representing text. Java source characters include letters, digits, special symbols and white space.
White space includes spaces, tabs and line breaks. It separates parts of source code and helps arrange a program so that it is readable. Inside a quoted string, a space belongs to the text value rather than merely separating instructions.
Property: Character codes represent characters numerically
A character code is a number assigned to a character. Encoding represents characters using an agreed coding scheme. A bit is a binary digit, either 0 or 1; binary means a system that uses these two digits.
Decimal means the number system using ten digits, 0 to 9. The letter A has decimal code 65 in ASCII. Its binary representation can be written as 1000001, or with a leading zero as 0100 0001.
What the figure shows
Encoding a keyboard character
The drawing shows hands at a keyboard and an enlarged view of the A key. Below it, A is labelled Letter, 65 is labelled Code (Decimal Value), and 0100 0001 is labelled Binary Number.
See Fig. 2.1 in your NCERT textbook
A data type specifies the kind of value and the operations supported on it. Keep the character separate from its numerical code. A printed letter is what a reader recognises; its code provides a numerical representation. Knowing that A has code 65 does not mean that the text A and the number 65 have the same Java data type.
How do ASCII and Unicode represent text?
ASCII stands for American Standard Code for Information Interchange. It standardises codes for characters including English letters, digits and punctuation. Original ASCII uses 7 bits and provides 128 code positions. Uppercase and lowercase letters have different codes.
Unicode is a standard for assigning numbers to characters across writing systems. It enables Java to represent and manipulate text from virtually all writing systems used worldwide. This wording does not mean that every visible symbol fits in one Java character value.
Property: Uppercase and lowercase letters have different ASCII codes
| Character group | Decimal ASCII codes | What to remember |
|---|---|---|
| Uppercase A to Z | 65 to 90 | Codes increase in alphabetical order within this group. |
| Lowercase a to z | 97 to 122 | Lowercase letters have codes different from uppercase letters. |
| Digits 0 to 9 | 48 to 57 | A digit character has a code distinct from its numerical value. |
| Space | 32 | A blank space is still a character. |
A space is one kind of white space, rather than the code for every white-space character. A tab moves output to a tab position; a line break starts another line. These are different from inserting an ordinary blank space.
Property: Unicode supports text across writing systems
Globalisation means preparing software for use across languages and regions. Localisation means adapting it for a particular language or region. Unicode supports these tasks by providing a shared basis for representing text, including text outside English.
Data exchange is the transfer of information between systems. A shared character standard helps systems interpret transferred text consistently. Character handling means storing and processing characters in a program. The code for a character should not depend on a programmer inventing a private numbering scheme.
Unicode and ASCII therefore answer related questions about representation. ASCII gives a limited set of standard character codes; Unicode supports a much wider range of writing systems. Neither a character code nor its printed appearance, by itself, declares a Java variable.
What are tokens and how are they classified?
Definition: A token is a smallest meaningful unit recognised when Java source code is analysed. Token categories include keywords, identifiers, literals, punctuators and operators.
A keyword is a reserved word with a defined language purpose. An identifier names a program element. A literal directly represents a value in source code. A punctuator helps organise code. An operator specifies an operation on values.
An integer is a whole number that may be negative, zero or positive. A primitive type is a built-in basic value type. In the statement int userNo = 101;, int is the keyword naming an integer type, userNo is the identifier, = is the assignment operator, 101 is an integer literal, and ; is the semicolon that terminates this statement.
| Token category | Example | Role in source code |
|---|---|---|
| Keyword | int | Specifies an integer primitive type. |
| Identifier | userNo | Names the variable receiving a value. |
| Literal | 101 | Supplies the directly written integer value. |
| Punctuator | ; | Marks the end of this declaration statement. |
| Operator | = | Assigns the value on the right to the variable on the left. |
How do tokens combine into a statement?
A statement is a unit of program instruction. The tokens in a statement work together, so identifying an individual word is only the first step. The declaration above both introduces a variable and supplies its initial value.
A method is a named block of code that performs a task. An argument is a value supplied when calling a method. A block groups code inside braces.
Syntax means the rules governing the arrangement of program symbols. The symbols ( and ) are parentheses; { and } are braces; [ and ] are square brackets. Parentheses enclose method arguments and braces enclose blocks. An array holds a sequence of elements of one declared component type; square brackets occur in array notation.
An expression is code that produces a value. An operand is a value on which an operator acts.
Recognise tokens by their role, not merely by how long they are. An entire quoted string is one literal even when it contains spaces. Spaces between tokens are not themselves identifiers, and the words inside a string do not become keywords.
What makes a Java identifier valid and meaningful?
An identifier is a name used for a variable, method, class or another program element. A class defines a type and groups its members. Names allow a program to refer to its data and operations without repeating their definitions.
Rule: For ordinary English-letter names, start an identifier with a letter, underscore or dollar sign. Later characters may also include digits. Do not use spaces, a Java keyword, or the reserved literal spellings true, false and null as an identifier.
The underscore is the symbol _. The dollar sign is $. Java permits these within identifiers, though clear letter-based names are preferable for ordinary school programs. In modern Java, a lone underscore is not a valid identifier.
Java is case-sensitive: uppercase and lowercase letters are distinct. NUMBER and number are therefore different identifiers. Use the same spelling and case whenever referring to a declared variable. Choosing a meaningful name is a readability convention, separate from the rules that make a name legal.
How should the example programs be read?
Each example below is a complete short application. The main method is its entry point. In public static void main(String[] args), public permits access, static allows invocation without creating an object, and void means that the method returns no value.
String is Java's text class; String[] declares an array of strings. The identifier args names the array of command-line arguments. An object is an instance of a class. The enclosing class names below reuse the data names to keep the examples compact.
System.out.println displays its argument and then ends the output line. System is a library class, out is its standard output stream, and println is the printing method. A stream carries a sequence of data; the dot selects a member.
Worked example 1. Display the integer variable userNo, whose value is 101.
class userNo {
public static void main(String[] args) {
int userNo = 101;
System.out.println(userNo);
}
}Answer: 101
- Line 1 declares the class userNo and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: int declares an integer variable. userNo is its name, = assigns the initial value, 101 is the literal, and the semicolon ends the statement.
- Line 4: The printing method reads the value associated with userNo and displays 101. It does not display the variable name.
- The last two lines close the method body and then the class body.
How do literals differ from identifiers?
A literal writes a value directly in the source. An identifier names something that the program can refer to. In the first example, userNo is the name and 101 is the literal. Removing the identifier does not turn the literal into a name.
A reference is a value through which a program refers to an object. A floating-point number uses a representation suited to numerical values that may have fractional parts.
What kinds of literal occur in Java?
| Kind | Example | Meaning |
|---|---|---|
| Integer literal | 101 | A whole-number value written without quotation marks. |
| Floating-point literal | 987.9 | A numerical value written with a fractional part. |
| Character literal | 'M' | A character value enclosed in single quotation marks. |
| String literal | "Keep Smiling" | A text value enclosed in double quotation marks. |
| Boolean literals | true and false | The two truth values, written in lowercase. |
| Null literal | null | Indicates that a reference does not refer to an object. |
A Boolean value expresses truth or falsity. Java boolean values are distinct from numeric values; do not replace them with digits when declaring a boolean variable.
Quotation marks determine the role of text in source code. Single quotation marks delimit a character literal; double quotation marks delimit a string literal. A delimiter marks a boundary. The surrounding quotes are syntax and are not printed as part of the value.
An ordinary decimal integer literal such as 101 has type int. A decimal floating-point literal such as 987.9 has type double unless a suffix changes its type. A suffix is an ending added to a literal: F or f selects float, while L or l selects long for an integer.
Worked example 2. Display the character stored in gender, initially 'M'.
class gender {
public static void main(String[] args) {
char gender = 'M';
System.out.println(gender);
}
}Answer: M
- Line 1 declares the class gender and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: char declares the character variable gender. The literal 'M' supplies its value; the single quotes delimit the literal.
- Line 4: The method prints M without the surrounding source-code quotation marks. The identifier gender is not itself the character value.
- The last two lines close the method body and then the class body.
What are escape sequences and how do they affect output?
An escape sequence represents a special character using a backslash followed by another character. The backslash is the symbol \. Java interprets the complete sequence inside a character or string literal instead of printing its two source characters separately.
| Escape sequence | Represents | Effect when used in text output |
|---|---|---|
| \n | Newline | Starts the following output on another line. |
| \t | Horizontal tab | Moves to a tab position. |
| \\ | Backslash | Displays one backslash character. |
| \" | Double quotation mark | Includes a double quote inside a string literal. |
| \' | Single quotation mark | Includes a single quote, including within a character literal. |
How should an output trace treat quoted text?
To trace a program is to follow its instructions in order and record their effects. When tracing text output, first locate the opening and closing quotes. Then interpret any escape sequences within them. Finally account for the behaviour of the printing method itself.
System.out.print displays its argument without automatically ending the line. System.out.println ends the line after displaying the argument. An embedded newline escape belongs to the value being printed, while println supplies its own line ending after that value.
Note: A horizontal tab is not a promise of a fixed number of printed spaces. Its visual width depends on the current position and the output environment's tab stops.
An ordinary space needs no escape sequence. In the next example, the space between Keep and Smiling is part of the string value. The output preserves that space and omits the two quotation marks used to delimit the literal.
Worked example 3. Display the string stored in message.
class message {
public static void main(String[] args) {
String message = "Keep Smiling";
System.out.println(message);
}
}Answer: Keep Smiling
- Line 1 declares the class message and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: String declares the text reference message and the quoted literal supplies its initial value. The internal space belongs to the text.
- Line 4: The method displays the text Keep Smiling and ends the line. It neither prints message nor includes the surrounding double quotes.
- The last two lines close the method body and then the class body.
How are variables declared, initialised and kept constant?
A variable is a named storage location associated with a declared type. Its value can change through permitted assignments. A declaration introduces the variable and its type. Initialisation supplies its first value. One statement can perform both actions.
Syntax: type identifier = value; Here type specifies the permitted kind of value, identifier supplies the variable name, = performs assignment, value supplies the initial data, and ; terminates the declaration.
Assignment stores a value in a variable. It is not an assertion that two mathematical quantities are equal. Read an assignment by evaluating the right-hand side and then storing the resulting value in the variable on the left.
What is the difference between a constant and a literal?
A constant is a value intended to remain fixed. A named primitive constant can be declared with the keyword final. After that variable has been assigned its value, Java prevents another assignment to it. The modifier final does not replace its data type.
A literal and a named constant are different ways of using fixed values. The literal is written directly; the constant has an identifier. Uppercase lettering can be a naming convention, but uppercase spelling alone does not prevent a variable from being reassigned.
A local variable is declared inside a method or another local block. It must receive a value before the program reads it. Declaring its type does not, by itself, supply the local variable's initial value. The examples initialise their local variables explicitly.
Worked example 4. Add num1, initially 10, and num2, initially 20; store the sum in result.
class result {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int result = num1 + num2;
System.out.println(result);
}
}Answer: 30
- Line 1 declares the class result and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: The integer variable num1 receives 10 as its initial value.
- Line 4: The integer variable num2 receives 20 as its initial value.
- Line 5: The plus sign is the addition operator. It adds the two stored values and initialises result with their sum, 30.
- Line 6: The method displays the value stored in result. It does not print the expression or its variable names.
- The last two lines close the method body and then the class body.
What are primitive data types and their sizes?
A data type specifies the kind of value a variable can hold and the operations supported on it. A primitive type is one of Java's built-in basic value types. Java has eight primitive types, covering integers, floating-point numbers, characters and Boolean values.
A byte, as a storage unit, contains 8 bits. The keyword byte also names one of Java's integer types. Storage size describes the width of the value representation. It does not describe the number of characters in the variable's identifier.
A signed integer type represents negative as well as non-negative whole numbers. Unsigned means that the representation does not include negative values.
UTF-16 is a Unicode encoding using 16-bit code units. A code unit is one unit of an encoding's representation. A Java char holds one such unit; some Unicode characters need two units, so one char does not represent every Unicode character.
| Primitive type | Kind of value | Size in bits | Size in bytes |
|---|---|---|---|
| byte | Signed integer | 8 | 1 |
| short | Signed integer | 16 | 2 |
| int | Signed integer | 32 | 4 |
| long | Signed integer | 64 | 8 |
| float | Floating-point number | 32 | 4 |
| double | Floating-point number | 64 | 8 |
| char | Unsigned UTF-16 code unit | 16 | 2 |
| boolean | true or false | No fixed language-specified storage size | No fixed language-specified storage size |
Precision concerns how much numerical detail can be represented. Floating-point types have finite precision, so not every decimal fraction is stored exactly. Keep this qualification when describing float and double; a decimal point does not imply unlimited accuracy.
Worked example 5. Store price as the double value 987.9 and display it.
class price {
public static void main(String[] args) {
double price = 987.9;
System.out.println(price);
}
}Answer: 987.9
- Line 1 declares the class price and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: The double keyword declares a floating-point variable. The decimal literal 987.9 has type double without requiring a suffix.
- Line 4: The printing method displays 987.9. This displayed decimal form does not establish that every decimal fraction has an exact binary representation.
- The last two lines close the method body and then the class body.
How do primitive and non-primitive types differ?
A non-primitive type, also called a reference type, includes class types and array types. A variable of such a type holds a reference to an object or the null reference. String is a class type and therefore is not one of Java's eight primitive types.
| Feature | Primitive type | Reference type |
|---|---|---|
| Examples | int, double, char, boolean | String, array types and other class types |
| Value held | A primitive value of the declared type | A reference to an object, or null |
| Text representation | char holds one UTF-16 code unit | String represents a sequence of characters |
| Null assignment | null is not a primitive value | A reference variable may hold null |
How does the task influence the choice of type?
Use a type suited to the meaning of the data. Whole-number dimensions can be stored in int variables, while text such as a message belongs in String. The identifier should explain the value's purpose; the declaration should establish its type.
The variable's name does not determine its type. In the next program, length, breadth and area are all explicitly declared int. The dimensions are 10 units and 20 units, and the computed area is 200 square units. The output statement prints the numerical result.
Worked example 6. Find the area of a rectangle of length 10 units and breadth 20 units.
class area {
public static void main(String[] args) {
int length = 10;
int breadth = 20;
int area = length * breadth;
System.out.println(area);
}
}Answer: 200
- Line 1 declares the class area and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: length names the rectangle's length and receives the integer 10, measured in the stated units.
- Line 4: breadth names the rectangle's breadth and receives the integer 20 in the same units.
- Line 5: The asterisk, *, is the multiplication operator. Area equals length multiplied by breadth, giving 200 square units; area stores the numerical value.
- Line 6: The method displays 200. The unit is understood from the problem; this instruction does not append unit text.
- The last two lines close the method body and then the class body.
How do implicit and explicit type conversions work?
Type conversion changes a value's representation from one type to another. Implicit conversion occurs automatically where Java's rules permit it. Explicit conversion, or casting, uses a type written by the programmer to request a permitted conversion.
Widening converts to a type that accommodates a wider range of values. Narrowing converts to a type with a more restricted range or representation. These terms describe permitted type relationships, rather than the length of the printed number.
Rule: Assignment from int to double permits an implicit widening conversion. Assignment from double to int generally needs an explicit cast. A cast requests conversion; it does not guarantee that the original value will be preserved.
How does automatic numeric conversion affect a sum?
When an int value and a double value are added, the int value is converted to double for the operation. In the following program, num1 supplies 10 and num2 supplies 20.0. The result is the double value 30.0.
Worked example 7. Add the int value num1 = 10 to the double value num2 = 20.0.
class sum1 {
public static void main(String[] args) {
int num1 = 10;
double num2 = 20.0;
double sum1 = num1 + num2;
System.out.println(sum1);
}
}Answer: 30.0
- Line 1 declares the class sum1 and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: num1 is declared int and initialised with the integer literal 10.
- Line 4: num2 is declared double and initialised with the floating-point literal 20.0.
- Line 5: The int operand is converted to double before addition. The double result 30.0 initialises sum1.
- Line 6: The method displays 30.0. Neither source variable has its declared type changed by evaluating this expression.
- The last two lines close the method body and then the class body.
What information can an explicit cast lose?
Syntax: (targetType) expression requests a cast. targetType stands for the required result type; expression stands for the value-producing code being converted. The parentheses identify the type used for the cast.
Converting the finite double value 20.67 to int discards its fractional part, giving 20. This is truncation towards zero, not rounding to the nearest integer. The original variable retains its value.
Worked example 8. Convert x = 20.67 to int, storing the result in num1.
class x {
public static void main(String[] args) {
double x = 20.67;
int num1 = (int) x;
System.out.println(num1);
}
}Answer: 20
- Line 1 declares the class x and opens its body.
- Line 2 opens the main method, where execution of this application begins.
- Line 3: x is the identifier of a double variable holding the given value 20.67.
- Line 4: The cast (int) converts the value read from x to an integer. It discards the fractional part and initialises num1 with 20; x itself remains unchanged.
- Line 5: The method displays 20, the converted result. No rounding instruction occurs in this program.
- The last two lines close the method body and then the class body.
Do not replace the warning that conversion can lose information with a claim that every conversion loses information. The particular int value 10 converts exactly to double. Some other widening conversions, including long to float, can lose precision even though they are permitted automatically.
For a conversion question, identify the source type, destination type and exact value first. Then decide whether the conversion is automatic or requires a cast. Finally check whether a fractional part or precision can be lost, and state the resulting value separately from the original variable.
Glossary
- Character set — A collection of characters available for representing text in a computing system.
- ASCII — American Standard Code for Information Interchange, a coding standard including English letters, digits and punctuation.
- Unicode — A character standard providing numerical representations for text across a wide range of writing systems.
- Escape sequence — A backslash-led sequence representing a special character inside a character or string literal.
- Token — A smallest meaningful unit recognised during analysis of the program's source code.
- Identifier — A name used to identify a variable, method, class or another program element.
- Literal — A directly written representation of a value within the program's source code.
- Variable — A named storage location with a declared type and a value that can change.
- Named constant — A named fixed value, represented here by a primitive variable declared using final.
- Data type — A classification specifying permitted values and the operations that may be performed on them.
- Initialisation — The act of supplying a variable with its first value before that value is used.
- Implicit conversion — A permitted type conversion carried out automatically without a programmer-written cast requesting it.
- Explicit conversion — A conversion requested by writing a cast that specifies the required target type.
Common errors and misconceptions
- Misconception: An identifier and a literal are interchangeable. Correct: In int userNo = 101;, userNo names the variable while 101 directly represents its initial value. They perform different roles within the same statement.
- Misconception: Java ignores differences between uppercase and lowercase letters. Correct: Java is case-sensitive. NUMBER and number are different identifiers, so a reference must match the intended declaration's spelling and case.
- Misconception: Single and double quotation marks are interchangeable in Java. Correct: Single quotes delimit a character literal such as 'M'; double quotes delimit a string literal such as "Keep Smiling".
- Misconception: Every primitive type has a fixed published byte size in Java. Correct: Java does not specify a fixed storage size for boolean. Do not infer its actual storage from its two possible truth values.
- Misconception: A char can store any complete Unicode character. Correct: A char stores one 16-bit UTF-16 code unit. Some Unicode characters require two such units, so a single char is insufficient for them.
- Misconception: An uppercase variable name automatically makes its value constant. Correct: Capitalisation is a naming choice. The final modifier prevents another assignment once a variable has received its value.
- Misconception: Casting 20.67 to int rounds it to the nearest integer. Correct: The cast discards the fractional part and produces 20. It does not change the double variable from which that value was read.
- Misconception: Automatic widening conversions cannot lose information. Correct: Some widening conversions can lose precision. Decide from the source type, target type and particular value instead of assuming that automatic conversion guarantees exact preservation.
Exam-style questions with model answers
Q1. In the Java statement int userNo = 101;, identify the identifier and the literal, explaining the role of each. [2 marks]
- userNo is the identifier: it names the integer variable introduced by the declaration.
- 101 is the integer literal: it directly supplies the initial numerical value assigned to userNo.
Q2. Explain three differences between ASCII and Unicode, considering character coverage, their use in multilingual software, and their relationship to Java text. [3 marks]
- Original ASCII is a 7-bit character coding scheme with 128 code positions, including English letters, digits and punctuation; Unicode covers a much wider range of writing systems.
- ASCII alone cannot represent the range of text needed across those writing systems. Unicode supports globalisation and localisation by providing shared character representations.
- Every ASCII character fits in one Java char, whereas some characters supported by Unicode require two UTF-16 code units and therefore two char values. Java uses Unicode for text representation and handling, including ASCII characters within that wider repertoire.
Q3. For each Java escape sequence, state its meaning: \n, \t, \\, \" and \'. [5 marks]
- \n represents a newline character. When printed as part of a string, it moves the following output onto another line rather than displaying a backslash and the letter n.
- \t represents a horizontal tab. It moves output to a tab position; it should not be described as a fixed number of spaces.
- \\ represents one backslash character. The doubled source spelling allows the backslash itself to be included within the value of a literal.
- \" represents a double quotation mark. It can include that mark within a string without ending the string literal.
- \' represents a single quotation mark. It allows that mark to be represented within a character literal without being mistaken for its closing delimiter.
Q4. In a Java main method, the statements are int num1 = 10; double num2 = 20.0; double sum1 = num1 + num2; System.out.println(sum1);. State the types of both input variables, explain the conversion, and give the output. [4 marks]
- num1 has declared type int. The literal 10 supplies its integer initial value, and evaluating the later addition does not alter that declared type.
- num2 has declared type double and is initialised with the double literal 20.0, so the addition combines an int operand with a double operand.
- Java implicitly converts the int operand to double for the addition. The resulting double value is stored in sum1.
- The output is 30.0 followed by a line ending, because println displays the double sum and then terminates the output line.
Q5. In a Java main method, double x = 20.67; int num1 = (int) x; System.out.println(num1); are executed in order. Identify the source type, target type and conversion, explain the information lost, state the output, and state whether x changes. [6 marks]
- The source variable x has type double. It is initialised with 20.67, a floating-point literal representing the given value with a fractional part.
- The destination variable num1 has type int. This variable receives the converted integer value rather than the original double value directly.
- The expression (int) x uses an explicit cast. The programmer writes the destination type in parentheses to request conversion of the value read from x.
- For this value, the fractional part is discarded during conversion. The process truncates towards zero and does not round to the nearest integer.
- The output is 20 followed by a line ending, because num1 holds the integer value produced by the cast.
- x remains unchanged by the cast. Reading and converting its value for assignment to num1 does not reassign the source variable.
Q6. State the sizes in bits and bytes of Java int, double and char, then explain why a fixed storage size should not be supplied for boolean. [4 marks]
- An int value has a width of 32 bits, equivalent to 4 bytes, and represents a signed integer.
- A double value has a width of 64 bits, equivalent to 8 bytes, and represents a floating-point number.
- A char value has a width of 16 bits, equivalent to 2 bytes, and represents one UTF-16 code unit.
- Java specifies the truth values true and false for boolean but does not specify a fixed storage size for the type.
Q7. In a Java main method, String message = "Keep Smiling"; and System.out.println(message); are executed. State the output and explain why message is non-primitive. [2 marks]
- The output is Keep Smiling followed by a line ending. The internal space appears, but the source-code quotation marks do not.
- message has type String, a class type. It holds a reference to a string object rather than a primitive value.
Q8. A rectangle has length 10 units and breadth 20 units. A Java main method executes int length = 10; int breadth = 20; int area = length * breadth; System.out.println(area);. Identify the dimensions' types, explain *, and give the output and its meaning. [3 marks]
- length and breadth are both declared int, and they receive the supplied whole-number dimensions 10 and 20 respectively. Their identifiers describe what those values represent.
- The asterisk is the multiplication operator. The expression length * breadth applies the rectangle-area rule and stores the resulting integer in area.
- The output is 200 followed by a line ending. In this problem, that number represents 200 square units, although the print statement does not display the unit text.
Key takeaways
- Character codes supply numerical representations for text; the visible character and its code are different things to identify.
- Unicode supports text from virtually all writing systems used worldwide, helping Java programs handle multilingual data.
- Classify Java tokens by their roles: keywords, identifiers, literals, punctuators and operators combine to form instructions.
- Identifiers name program elements, while literals directly represent values; preserve spelling and case when referring to a declared name.
- Single quotation marks delimit character literals and double quotation marks delimit strings; escape sequences represent special characters within them.
- Java has eight primitive types; String is a reference type, and boolean has no fixed language-specified storage size.
- A declaration introduces a variable and its type; initialisation supplies its first value, while final prevents later reassignment.
- Type conversions may be automatic or explicit; check the particular value and types before deciding whether information is preserved.
Test yourself
What does an identifier do in a Java program?
It names a program element, such as a variable, method or class, so that the program can refer to it.
Are NUMBER and number the same Java identifier?
No. Java is case-sensitive, so uppercase and lowercase letters distinguish these two identifiers.
What does \n represent inside a Java string literal?
It represents a newline character, causing following output to begin on another line when printed.
Why is "Keep Smiling" one literal despite its internal space?
The double quotation marks delimit one complete string literal. The internal space is part of its text value.
Does declaring a variable final change its data type?
No. final restricts reassignment after the variable receives its value; the declaration still specifies its data type separately.
Is String a Java primitive type?
No. String is a class type, and a String variable holds an object reference or null.
Given double x = 20.67;, what value does (int) x produce?
It produces 20 by discarding the fractional part. The cast does not reassign x.
What is the difference between the size of char and the representation of every Unicode character?
A char is one 16-bit UTF-16 code unit. Some Unicode characters need two code units and cannot fit into a single char.
