What does a variable consist of? | Data type - int, double, String
Identifier (name) -
•Combination of letters, digits and special characters (without spaces etc. and operators like +, -,*, etc.)
•Must start with a letter
•Identifiers are case sensitive
•Java keywords are not allowed (e.g. void, int)
•Convention for compound names: myName |
Does variable have initial values? | Initially, variables have no well-defined value (not even 0!), but can have arbitrary (possibly also invalid) contents. |
What are the primitive data types? | Photo |
Floating point numbers (float, double) | Literals:
• 0.23, -47.11
• Scientific notation: -1.23E-45 (= -1,23x10-45)
• In principle double; float can be forced by a suffix f or F
Operations:
• additive: +, - (binary and unary)
• multiplicative: *, /
• comparison: == (attention – often imprecise!), !=, >, <, >=, <= |
Logical values (boolean) | Literals:
• true or false
Operations (boolean algebra):
• Negation: ! (unary)
• Conjunction (∧, AND): &&
• Disjunction (∨, OR): ||
• Antivalence (≢, XOR): ^
• Comparison: ==, != |
Characters (char) | For storing individual characters of a text:
•Upper and lower case letters, digits, special characters
•Value range: 216 possible characters (Unicode)
Literal: between ' '
Comparisons (lexicographic): ==, !=, >, <, >=, <=
• +, - with whole numbers (»code arithmetic«):
'A' + 1 ➝ (Unicode of A) + 1 ➝ Unicode of B
No possibility of input via a Scanner variable |
Character Strings (String) | For storing texts.
Actually not a primitive data type, but a class
Input via a Scanner variable in: in.next() |
Expressions | Expressions are combinations of operands and corresponding operators.
operators: +, -, *, /, %
operands: x, y, z, 1, 2, 3 |
Precedence and associativity of operators | a |
Type compatibility & Type conversions (1) | Operands of an expression must be (data) type compatible to each other –
each operation (+, *, ...) ultimately requires operands of the same data type.
Two types are compatible if they are
• equal, or
• one can be converted to the other by implicit type conversion (performed autonomously by the compiler).
Implicit (automatic) data type conversion (type promotion)
• byte → short → int → long → float → double
• char → int
• All primitive types → String
These conversions never cause data loss! |
Type compatibility & Type conversions (2) | If no implicit type conversion is possible, an explicit conversion must take place.
Data loss is possible in this case.
e.g int area = (int) (r*r*3.14159); |
Conversion of strings to primitive types | int valueI = Integer.parseInt("123");
double valueD = Double.parseDouble("123.4"); |
Assignment as an expression | Assignments are actually expressions
expression: result = a + b * c
assignment: = |
Shortcut assignment operators | Compund assignment operators following the pattern •= (for any binary operator •):
a += b; // is equivalent to a = a + b;
a -= b; // is equivalent to a = a - b;
a *= b; // is equivalent to a = a * b;
a /= b; // is equivalent to a = a / b; |
Shortcut assignment operators | d |
Evaluation order (1) | Subexpressions are evaluated (with respect to the run-time) strictly from left to right.
Logical expression are »lazyly« evaluated (conditional evaluation, lazy evaluation) –
only as long as the final result is not yet determined:
• x && y yields always false, if x == false
• x || y yields always true, if x == true
Therefore, y is not evaluated in these cases |
Evaluation order (2) | The order is especially important if side effects occur in the expression!
Example 1:
int i = 2;
int j = (i=3) * i; // j = 9 |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |