- •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
8.3. Explicit initialization
Class fields may be explicitly initialized. With the following definition: class Pair { int a = 1; int b = 1; Pair() { a++; b++; } } values of components of newly created objects (using the above constructor) of the class Pair are (2, 2). To initialize a field any expression may be used:
The static method max from the class Math returns the greater of the two arguments.
class Pair { int a = 2; int b = Math.max(a, 1) + a + 1; Pair() { } } new Pair() creates an object with elements 2, 5.
Initialization of non-static fields takes place before constructor invocation and is performed in the order in which the fields are defined in the class.
Static fields are initialized by the first use of the class. Order of the initialization is determined by the order in which they are defined in the class.
8.4. Packages and imports
A package is a kind of a class library which groups classes having similar functionality: input-output operations, network applications, and so on. Each class belongs to some package. Classes compiled with package declaration package ... belong to the declared package. Classes compiled without any package declaration belong to the default (unnamed) package. Packages have hierarchical structure: aPackage aSubPackage aSubPackage For example, the package java.awt contains the subpackage java.awt.event. Hierarchical structure of packages is mapped into directory structure (the consecutive levels of directories in the directory hierarchy correspond to consecutive levels in package hierarchy). Why use packages? Consider an example: class A { Button b = new Button("Ok"); // new object of the class Button .... } Two cases are possible:
the class Button is defined in the same file (as the class A) or in some other file in the current directory
The class Button is defined somewhere else
In the first case the compiler (and the Java virtual machine) may find the class. In the second case arises the problem: where to search for the class? Moreover, use of the name Button may be ambiguous (is it the Button from the current directory or from somewhere else?). Thus, packages group classes into namespaces and protect against the name conflicts. Qualified names A qualified name of a class (type) belonging to a named package has a form:
package_name.class_name
for example: java.awt.Button class A { java.awt.Button b = new java.awt.Button("Ok"); .... } For the classes defined in the default (unnamed) package the qualified name is the same as the name of a class (for example: Pair). In general, all class names used in a program should be qualified. Fortunately, the import statement allows using simple names. import java.awt.Button; // imports the name of the class java.awt.Button class A { java.awt.Frame f = new java.awt.Frame("Title"); Button b = new Button("Ok"); // use of the simple name of the class java.awt.Button .... } Because the name of the class java.awt.Button was imported, we may use its simplified name in the program. But the name of the class java.awt.Frame was not imported, so we must use its fully qualified name. To import all the class names from a given package we must use an asterisk: import java.awt.*; // imports all the class names from the package java.awt class A { Frame f = new Frame("Title"); // now the simplified name may be used Button b = new Button("Ok"); .... } The classes String and System used frequently in programs are in the package java.lang.
The package java.lang does not require the import statement. The classes from this package are imported automatically.
