Literals. Identifiers. Keywords. Comments
1. What are literals?
Literals represent fixed values (constants), which have a specific type. For more information about primitive data types can be read here.
Literal’s type is determined by its value. The literals most often found in expressions, the parameters of functions (methods).
Examples of literals:
5 '5' 0x8f 5.663 'x' "This is a string" true false
2. How the literals of integer types are defined?
The type of literal compiler recognizes by its value. By default, integer literals are of type int. In Java, integer literal of ‘int‘ type has a width of 32 bits.
Example of integer literals type int:
25 -1000 073 0x25 0XF5A 0xabcd
In order to an integer literal had a ‘long‘ type, need to supplement the value of the literal by symbols ‘ l ‘ or ‘L‘.
Example of integer literals of type ‘long‘:
25l 20000000000L 0L
3. How the assignment of literal is realized for variables of ‘byte‘ and ‘short‘ types?
A literal of type “int” can be assigned to an variables of types “byte“, “short“, if the literal’s value is within the boundaries of the acceptable data values.
Example. Assigning of the literals to the variables of types “byte” and “short“.
byte b; short sh; b = -20; // it works, the number -20 is part of a value range of "byte" type sh=1000; // it works // error, number 1000 goes beyond the range of type "byte" // b = 1000; // the same error // sh = 50000;
If you pick up a comments from a string
b = 1000;
then the compiler generates an error message:
Unresolved compilation problem: Type mismatch: cannot convert from int to byte
4. How to write an integer literal in the octal or hexadecimal notation?
To write a literal in octal notation, the character ‘0‘ (zero) is put in front of it.
To write the literal in hexadecimal literal value is necessary to put the characters ‘0x‘ or ‘0X‘ in front of it.
An example of using literals in octal and hexadecimal notations.
byte b; short sh; int d; long l; // octal notation b = -027; // -23 in decimal notation sh = 0233; // 155 in decimal notation d = 03546; // 1894 l = 032451l; // 13609 // hexadecimal notation b = 0x0f; // 15 in decimal notation sh = 0XAF; // 175 d = 0xabcd; // 43981 l = 0XaF90FCDEDl; // 47128235501
If you try to write:
byte b; b = 09;
then the compiler will generate the following error message:
The literal 09 of type int is out of range Type mismatch: cannot convert from int to byte
This means that the number ‘9‘ is not included in the set of digits of octal notation.
5. What features of literal’s representation of floating point in Java?
Floating point literals can be presented in standard or exponential form.
By default, the floating-point literals are the type ‘double‘. If for the literal must force specify the type “double“, then after it’s value you need add the symbol ‘d‘ or ‘D‘.
If you want to specify a literal of type “float“, then its value is supplemented by the symbol ‘f‘ or ‘F‘.
Examples of floating point literals.
2.66 -3.390 0.3003f 239.39F 1E6 -2.3e-4 -3.3E-2f 0.3d -0.3D
6. How are represented the floating point literals in exponential form?
In the exponential form to the standard form of the literal is added symbol ‘e‘ or ‘E‘. After the ‘e‘ or ‘E‘ symbol is followed a number, which is a power of 10. The number in standard form must be multiplied at this number.
Example representation of floating point numbers in exponential form:
double d; d = 1.0e6; // d = 1000000.0 d = 1.1E-2; // d = 0.011 d = -3.9e-3; // d = -0.0039
7. How are represented the floating point literals in hexadecimal notation?
The Java language allows to represent the floating point literals in hexadecimal notation. In this case the literal consists the following parts:
- ‘ – ‘ sign, it means that the number is negative;
- symbol ‘0x‘ or ‘0X‘ (it means that the literal is written in hexadecimal notation);
- directly the number in a standard format (eg 2.3);
- symbol ‘p‘ or ‘P‘;
- the integer, which is a power of 2, that multiplies at this number.
Example. Presentation of literals in hexadecimal notation.
double d; d = 0XF.1P3; // d = 120.5 d = 0x1p1; // d = 2.0 d = -0x1p3; // d = -8.0
8. How in Java are represented the logical literals?
In Java logical literals can be of two values: true or false. Logical literals can be assigned to variables of type boolean.
boolean b; b = true; b = true == false; // b = false
9. How are written the character literals in Java?
Character literals – the characters which support the Unicode character set (Unicode). One character takes a memory of 16 bits (2 bytes).
There are many characters which, for various reasons, can not be represented directly. In this case, are used the escape sequences.
Examples of character literals.
'g' '3' ';' '\145' '\u00af'
10. What are the escape sequences?
Sequences can display characters that are:
- can not be entered from the keyboard (eg, newline);
- need to be represented in octal form
- need to be represented in hexadecimal form.
There are following escape sequences in Java:
\uxxxx - hexadecimal character (xxxx) \ddd - octal character (ddd) \' - single quote \" - double quote \\ - backslash \b - backspace \f - page feed \n - new line \r - carriage return \t - tabulation
An example of using of character literals and escape sequences in the program.
char c; c = 'g'; c = '\n'; // new line c = '\"'; // double quote c = '\u00af'; // c = '?' c = '\145'; // c = 'e' c++; // c = 'f'
11. How the string literals are shown?
String literals are taken in double quotes.
Examples of string literals.
"This is a string of characters" "Two\nstrings" "Theme of the lesson \'Literals\'"
12. What is the identifier in the Java programming language?
Identifiers are used for naming the variables, methods, classes. In Java, an identifier is any sequence of:
- uppercase and lowercase letters;
- digits from ‘0‘ to ‘9‘;
- underscore ‘_‘;
- the currency symbol (in special cases).
The identifier must begin with a letter.
The Java language recognizes uppercase and lowercase letters as different. This means that the MAX name differs from the Max – are two different names.
Examples of identifier names:
x weight a1 array1 MyClass Max a1b2c3
13. What are keywords?
Keywords – these are the reserved words of Java language. The key words can be operators, instructions, statements that form the basis of the Java language. Keywords can not be used as names of identifiers, variables, classes, methods and etc.
The following keywords are defined in Java:
abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile
14. What kinds of comments exist in the language Java?
Comments allow to accompany the program code by explanations. Comments make it easier the reading of the program code details by the programmers.
In the Java programming language, there are three kinds of comments:
- single-line comments are begun with the characters ‘//‘;
- multi-line comments, they start from the characters ‘/*‘ and end with ‘*/‘;
- documentation comments, they start from the characters ‘/**‘ and end with ‘*/‘.
Examples of comments:
... // a single line comment ... /* multiline comment */ ... /** documenting comment */ ...
- Data types in Java
- Variables. Declaring variables. Initialization of variables
- Converting of types. Automatic type cast in expressions. Automatic advancement in expressions