- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
5.2. Literals
Types of literals are determined by the compiler applying some rules:
integer numbers are of type int
real numbers noted either with decimal point (1.2) or in scientific notation (1e+9) are of type double
character literals are of type char
true and false are literals of boolean type and are reserved keywords of the language
The programmer may change the default interpretation of numerical literals with the help of specific modifiers:
the letter L or l used as suffix of an integral literal enforces treating it as the literal of type long (i.e. 3L is of type long)
similarly, the letter f or F makes real number literal be of the type float (0.3f)
An integer literal may be expressed in:
decimal (base 10)
octal (base 8) - prefixed with the 0 (zero) character (i.e. 03 is an octal number)
hexadecimal (base 16) - prefixed with 0x or 0X (for example, 0x1a, 0X11, 0xFF) Note that hexadecimal digits with values 10 through 15 are represented by the ASCII letters a through f or A through F respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.
Real number literals may be expressed in scientific notation: 2e+9 denotes 2 multiplied by 10 raised to the 9th power whereas 3e-11 denotes 3 multiplied by 10 raised to the -11th power In expressions of the form 10/3 (division of integers), where both operands are of type int, the type of the result is int too. As a consequence its value turns out to be 3 (not 3.33333....). To enforce treating it as a real value, the suffix d must be appended to at least one of the operands (specifying literal of the type double). The other solution is to use real number literal as an operand by including the decimal point in its notation (for example 10.). The following program illustrates the various possibilities discussed above:
public class LitLicz {
public static void main(String[] args) {
System.out.println( 10 + 0x10 );
System.out.println( 10/3 );
System.out.println( 10./3 );
System.out.println( 10d/3);
System.out.println( 2147483648L );
System.out.println( 2147483647 + 1 );
System.out.println( 2147483647L + 1 );
}
}
26 3 3.3333333333333335 3.3333333333333335 2147483648 -2147483648 2147483648
The result of the execution of this program is shown on the figure. Note that:
operations on the real numbers are not precise.
for the value of an arithmetical expression to be of the real type, it is enough to make only one of operands of the real type (instead of writing 10d/3d, you may write 10d/3). The type of the result is determined by the type of the greater (allowing greater possible values) operand.
Character literals (of the type char) are noted as single characters enclosed in apostrophes. String literals are sequences of characters enclosed in quotation marks.
String literals are objects of the class String.
An escape sequence is a sequence of characters starting with the escape character (backslash). Escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash characters in character literals
Escape sequence |
Notation |
Linefeed - LF |
\n |
Horizontal tab - HT |
\t |
Backspace - BS |
\b |
Carriage return - CR |
\r |
Form feed - FF |
\f |
Single quote - ' |
\' |
Double quote - " |
\" |
Backslash - \ |
\\ |
Unicode escape - arbitrary character which Unicode encoding is NNNN (N is a hexadecimal digit) |
\uNNNN |
Note:
a character literal represents a single character, usually enclosed in single quotes.
a string literal consists of zero or more characters enclosed in double quotes. The empty string consists of zero characters: "".
The following test demonstrates the usage of character and string literals. Note alternative way of displaying data to the command prompt: the method System.out.println(...) prints its argument and terminates the current line by writing the line separator string, whereas the method System.out.print(...) prints its argument without terminating the line.
public class LitZn {
public static void main(String[] args) {
System.out.print('a');
System.out.print('\u0061');
System.out.print('\\');
System.out.print('\'');
System.out.println(" First Second Third ");
System.out.println("All work and no play\nMakes Jack a dull boy");
System.out.println("c:\\util\\bak");
System.out.println("");
System.out.println("\"");
System.out.print("abcd\r12\n1234\rab");
}
}
aa\' First Second Third All work and no play Makes Jack a dull boy c:\util\bak " 12cd ab34
The output of the above program is shown on the nearby figure. Note that '\u0061' is the hexadecimal Unicode encoding of the character 'a'. The single quote and the backslash characters in literals are prefixed with backslash: \' and \\. Escape sequences may occur in string literals: the '\n' character inside the string "All work and no play\nMakes Jack a dull boy" is the linefeed character which terminates the current line. That is why the substring "Makes Jack a dull boy" is printed in a new line. A pair of backslashes contained in a string literal is printed as a single backlash. Double quotes must be prefixed with escape character (backslash). The last instruction of the program acts as follows:
firstly, the string "abcd" is printed
then the carriage return character ('\r') is printed what results in drawing back the caret to the beginning of the line.
at the beginning of the line the characters 1 and 2 are printed resulting in overwriting of the characters a and b
the linefeed character '\n' terminates the current line, thus the following characters are printed on a new line
the string 1234 is printed on the new line
the carriage return character draws back the carriage to the beginning of the line
the characters a and b are printed overwriting the two initial characters 1 and 2 of the previously printed string 1234.
