- •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
9.2. Comparison operators and expressions
Many control statements use conditions given as relational expressions built with the help of relational operators or equality operators. Relational operators (<, <=, >, >=) are two argument (binary) operators. Each argument is an expression of numeric type (byte, char, short, int, long, float, double). The value of a relational expression is of boolean type (true or false).
Relational operators |
|
Expression (a and b - arbitrary expressions of numeric types) |
Result |
a > b |
true if the value of a is grater then the value of b false in another case |
a >= b |
true if the value of a greater than or equal to the value of b false in another case |
a < b |
true if the value of a is less then the value of b false in another case |
a <= b |
true if the value of a less than or equal to the value of b false in other case |
Examples:
int a =1, b = 3; if (a > b) System.out.println("Greater than"); else System.out.println("Less than"); |
Prints out: Less than |
int a =1, b = 3; if (a + 1 > b/3) System.out.println("Greater than"); else System.out.println("Less than"); Note: the precedence of relational operators is lower than the arithmetical operators' precedence Thus we don't have to write: if ( (a + 1) > (b/3) ) ... |
Prints out: Greater than |
int a = 1, b = 3; boolean reslt1 = a <= b; boolean reslt2 = 1 >= a; boolean reslt3 = a < 1; Note: the precedence of relational operators is higher than the assignment operator's precedence Thus we don't have to write: boolean reslt = (a <= b); |
The variables have the following values: reslt1 == true reslt2 == true reslt3 == false |
A common error is using the assignment operator (=) in place of the equality operator (==). Usually the compiler detects this error and prints appropriate warning.
The two binary equality operators: == and != do not belong to the group of relational operators for two reasons:
they have lower precedence than relational operators
their arguments may be of any type (numeric, boolean, reference) under the following conditions:
if one of the arguments is of numeric type - the other must be of numeric type too
if one of the arguments is of boolean type - the other must be of boolean type too
if one of the arguments is of reference type then the other must be of reference type compatible with the type of the first argument or the null literal
Expressions built using equality operators have results of type boolean.
Equality operators |
||
Expression |
a == b |
a != b |
a and b of arbitrary numeric type |
true if the value of a is equal to b false in other case |
true if the value of a is not equal to b false in other case |
a and b of type boolean |
true if a and b are both true or false false in other case |
true if a is true and b is false or a is false and b is true false if both a and b are true or false at the same time |
a and b of reference type |
true if both a and b refer to the same object or have the value of null false if a refers to other object than b or exactly one of the variables has the value of null |
true if both variables refer to different objects or exactly one of them is null false if both variables refer to the same object or both have the value of null |
Example:
int a = 2, b = a + 1;
if ( a == b) System.out.println("yes");
else System.out.println("no");
if ( a != --b) System.out.println("yes"); // 2
else System.out.println("no");
int c = 4;
if ( a < b + 1 == b < c) System.out.println("yes"); // 3
else System.out.println("yes");
no no yes
The program prints the following output to the command prompt: Notes:
The prefix operator "--" assures that the value of b in the example 2 will be decreased before its use in the equality expression (!=)
The example 3 uses the fact that the equality operator ("==") precedence is lower than relational operators precedence, which is lower than arithmetical operator precedence (+). Thus two values of boolean type are compared there and because they are true - the result is true too.
However, complex expressions should not be noted as in the example 3. It is better to use parentheses to make the code more clear. Example on comparing references: Pair p1 = new Pair(1,1); Pair p2 = new Pair(1,1); Pair p3 = p1; p1 == p2 // false although components of both pairs are the same p1 == p3 // true because both variables refer to the same object String s1 = "1,1"; p1 == s1 // compilation error: p1 is of type Pair and s1 is of type String which are not compatible String s2 = "1," + 1; s1 == s2 // false because variables refer to different objects s1 == null // false because s1 refer to existing object s1.equals(s2) // true because the strings are the same
To test the contents of different objects for equality the equals method is used. It must be defined in objects' class.
