- •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
Identifiers
Names of variables, constants, methods and classes are called identifiers. An identifier begins with a letter or underscore (the _ character) followed by a sequence of alphanumeric characters (letters and digits) or underscores. The keywords of the Java language cannot be used as identifiers. see Java reserved words
Naming conventions:
names of variables and methods are written in lowercase with each component (except first) distinguished with capital letter (for example: x, price, numOfAllOccurs, increment(), getBackground())
names of constants are written in uppercase, separating components with underscores (for example: NUM, EXIT_ON_CLOSE)
names of classes start with capital letter, each component distinguished with uppercase too. (Frame, ArrayList)
Remark: the convention of starting each component with capital letter is called the hungarian notation.
5.4. More on operators and expressions
In complex expressions (made up of a number of operators) evaluation order of operators depends on operator precedence and how they group. Operator precedence determines the order of evaluation of different operations contained in an expression (i.e. in the expression a + b * c, is addition or multiplication to be evaluated first?). Operators with the same precedence may be evaluated either left-to-right or right-to-left. The ones that group left-to-right are called left-associative, those that group right-to-left are called right-associative. For example, the expression a + b - c may be evaluated as (a+b)-c or a+(b-c). Although in this case grouping does not matter, there are situations when it is important. Operators have arguments, also called operands. In Java there are many unary and binary operators and one operator which takes three arguments. Arguments for operators are always expressions. In the expression a + (b - 1) arguments of the binary + are the expressions a and (b-1), in the expression b-1, arguments to binary - are the expressions b and 1. The Java language defines the following operators:
Precedence, grouping |
Operator |
Name |
1, right-associative |
! |
Logical Complement |
~ |
Bitwise Complement |
|
+ |
Unary Plus |
|
- |
Unary Minus |
|
++ |
Increment |
|
-- |
Decrement |
|
(type) |
Cast Operator |
|
2, left-associative |
* |
Multiplication |
/ |
Division |
|
% |
Remainder |
|
3, left-associative |
+ |
Addition |
- |
Subtraction |
|
4. left-associative |
<< |
Left Shift |
>> |
Right Shift |
|
>>> |
Unsigned Right Shift |
|
5, left-associative |
< |
Relational Operators
|
<= |
||
>= |
||
> |
||
instanceof |
Type Comparison Operator |
|
6, left-associative |
== |
Equality Operators |
!= |
||
7. |
& |
Bitwise AND |
8. |
^ |
Bitwise XOR (exclusive OR) |
9. |
| |
Bitwise OR |
10. |
&& |
Conditional AND |
11. |
|| |
Conditional OR |
12, right-associative |
?: |
Conditional Operator |
13. right-associative |
= =* =/ =% =+ =- =& =^ =| =<< =>> |
Assignment Operators
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: lower numbers in precedence column specify higher precedence unary operators have blue background, binary operators - white background Assignment operators are right-associative. It means that the expression: x = a + b is evaluated from right to left. It does not make any difference here, but consider an expression which itself is an assignment (the value of assignment expression is the value of its left-hand side variable) and its value is assigned to some variable: int x, a = 3, b = 4; int c = 1; x = a = b = c + 7 Here, the precedence of binary + is higher than that of assignment operator =. Thus the expression c + 7 is evaluated first, and its value is assigned to the variable b. The value of the expression b = c + 7 is now 8 and it is assigned to the variables on the left-hand side: a and then x. Consequently, the variables x, a, and b store the same value: 8. In the above table there are a number of the so called Compound Assignment Operators of the form: op= where op is one of the operators: * / % + - << >> >>> & ^ |
These operators are applied the following way: x op= expression is the abbreviated form of: x = x op ( expression ) where: x - a variable expression - an arbitrary expression op - operator
Thus, instead of: numOfChildren = numOfChildren + 1 we can write: numOfChildren += 1 There are two kinds of unary increment (++) and decrement (--) operators:
postfix (the operator is placed after its argument)
prefix (the operator is placed before its argument)
Moreover:
++ increases and -- decreases the value of operand by 1.
the value of the expression with the prefix operator is the value of its argument after application of the operator (increment or decrement)
the value of the expression with the postfix operator is the value of its argument before application of the operator - it equals the original value of the argument.
Thus, after the evaluation of the following: int n, i = 1; n = i++; // postfix ++ the value of n is 1, but the value of a is 2. However, after int n, i = 1; n = ++i; // prefix ++ the value of both n and i is 2. The following program explains the above remarks:
public class Express1 {
public static void main(String[] args) {
int a = 1, b = 2, c = 3;
a = b = c * 1 + 2;
System.out.println("a=" + a + " b=" + b + " c=" + c);
a = b = c * (1 + 2);
System.out.println("a=" + a + " b=" + b + " c=" + c);
a = b++;
System.out.println("a=" + a + " b=" + b + " c=" + c);
c = --b;
System.out.println("a=" + a + " b=" + b + " c=" + c);
a++;
b++;
c++;
System.out.println("a=" + a + " b=" + b + " c=" + c);
a = b++*++c;
System.out.println("a=" + a + " b=" + b + " c=" + c);
int longVariableName = 20;
longVariableName = longVariableName * 10;
longVariableName *= 10;
System.out.println( longVariableName );
}
}
a=5 b=5 c=3 a=9 b=9 c=3 a=9 b=10 c=3 a=9 b=9 c=9 a=10 b=10 c=10 a=110 b=11 c=11 2000
The binary + operator is used here not only to add numbers, but also to concatenate strings. Further discussion of this feature will be presented in the next lecture. Here is the output of the program:
