Names and Identifiers A program (that is, a class) needs a name public class SudokuSolver { ... 3. Java - Language Constructs I Convention for class names: use CamelCase → Words are combined into one word, each starting with a capital letter Names and Identifiers, Variables, Assignments, Constants, Allowed names for “entities” in a program: Datatypes, Operations, Evaluation of Expressions, Type Conversions Names begin with a letter or _ or $ Then, sequence of letters , numbers or _ or $ 62 63 Names - what is allowed Keywords The following words are already used by the language and cannot be used as names: _myName me@home abstract continue for new switch assert default goto package synchronized TheCure strictfp ?! 49ers boolean do if private this break double implements protected throw byte else import public throws __AN$WE4_1S_42__ side-swipe case enum instanceof return transient catch extends int short try char final interface static void $bling$ Ph.D’s class finally long strictfp volatile const float native super while 64 65
Variables and Constants Constants Variables are buckets for a value Keyword final Have a data type and a name The value of the variable can be set exactly once The data type determines what kind of values are allowed in the variable Example int y int x float f char c Declaration in Java : final int maxSize = 100; int x = 23, y = 42; 23 42 0.0f ’a’ float f; Hint : Always use final , unless the value actually needs to change char c = ’a’; over time. Initialization 66 67 Standard Types Types and Memory Usage Data Type Definition Value Range Initial Value Reminder: Memory cells contain 1 Byte = 8 bit − 128 , . . . , 127 byte 8-bit integer 0 short 16-bit integer − 32 ′ 768 , . . . , 32 ′ 767 0 − 2 31 , . . . , 2 31 − 1 boolean int 32-bit integer 0 − 2 63 , . . . , 2 63 − 1 long 64-bit integer 0L byte ± 1 . 4 E − 45 , . . . , ± 3 . 4 E +38 float 32-bit floating point 0.0f short , char ± 4 . 9 E − 324 , . . . , ± 1 . 7 E +308 double 64-bit floating point 0.0d boolean logical value true , false false int , float char unicode-16 character ’\u0000’ ,. . . , ’a’ , ’b’ ,. . . , ’\uFFFF’ ’\u0000’ long , double String string ∞ null 68 69
Literals: Integer Numbers Literals: Floating Point Numbers are different from integers by providing Type int (or short , byte ) decimal comma 1.23e-7f 12 : value 12 1.0 : type double , value 1 -3 : value − 3 exponent integer part 1.27f : type float , value 1 . 27 and / or exponent. Type long fractional part 1e3 : type double , value 1000 25_872_224L : value 25 ′ 872 ′ 224 1.23e-7 : type double , value 1 . 23 · 10 − 7 Hint: Underscores between digits are allowed! 1.23e-7f : type float , value 1 . 23 · 10 − 7 70 71 Literals: Characters and Strings Character: In ASCII Table Individual characters: ’a’ : Type char , value 97 Strings: "Hello There!" : Type String "a" : Type String Mind: Characters and Strings are two different things! 72 73
Value Assignment Value Assignment Examples Copies a value into variable x int a = 3; double b; x A nested assignment: In pseudo code: x ← value The expression a = 0 stores the value value In Java: x = value b = 3.141; (copy) value 0 into variable a . and then returns the value int c = a = 0; “ = ” is the assignment operator and not a comparison! String name = "Inf"; Therefore, int y = 42 is both a declaration + an assignment. 74 75 Arithmetic Binary Operators Arithmetic Binary Operators Infix notation: x op y with the following operators Division x / y : Integer division if x and y are integer. op: + − ∗ / % Division x / y : Floating-point division if x or y is a floating-poing number! modulo Examples Integer division and modulo Precedence : Multiplication, division, and modulo first, then − 5 / 3 evaluates to − 1 5 / 3 evaluates to 1 addition and subtraction − 5 % 3 evaluates to − 2 5 % 3 evaluates to 2 Associativity : Evaluation from left to right 76 77
Arithmetic Assignment Arithmetic Unary Operators Prefix notation: + x or − x x = x + y Precedence: Unary operators bind stronger than binary operators Examples : � x − = 3; // x = x − 3 Examples name += "x" // name = name + "x" x += y Assuming x is 3 num ∗ = 2; // num = num ∗ 2 2 ∗ − x evaluates to − 6 Analogous for − , ∗ , / , % − x − +1 evaluates to − 4 78 79 Increment/Decrement Operators Increment/Decrement Operators Increment operators ++x and x++ have the same effect: x ← x + 1 .But different return values: Examples Prefix operator ++x returns the new value: Assuming x is initially set to 2 ⇐ ⇒ a = ++x; x = x + 1; a = x; y = ++x ∗ 3 evaluates to: x is 3 and y is 9 Postfix operator x++ returns the old value: y = x++ ∗ 3 ⇐ ⇒ evaluates to: x is 3 and y is 6 a = x++; temp = x; x = x + 1; a= temp; Precedence: Increment and decrement operators bind stronger than unary operators Analogous for x −− and −− x . 80 81
Expressions Expressions represent computations Examples are either primary primary: “ − 4.1d ” or “ x ” or "Hi" or composed . . . composed: “ x + y ” or “ f ∗ 2.1f ” . . . from other expressions, using operators are statically typed The type of “ 12 ∗ 2.1f ” is float Analogy: Construction kit 82 83 Celsius to Fahrenheit Celsius to Fahrenheit - Analysis public class Main { 9 * celsius / 5 + 32 public static void main(String[] args) { Out.print("Celsius: "); int celsius = In.readInt(); Arithmetic expression, float fahrenheit = 9 * celsius / 5 + 32; contains three literals, one variable, three operator symbols Out.println("Fahrenheit: " + fahrenheit); } } Where are the brackets in this expression? Example : 15 ◦ Celsius are 59 ◦ Fahrenheit 84 85
Rule 1: Precedence Rule 2: Associativity Multiplicative operators ( * , / , % ) have a higher precedence ("bind Arithmetic operators ( * , / , % , + , - ) are left-associative: in case of the stronger") than additive operators ( + , - ). same precedence, the evaluation happens from left to right. Example Example 9 * celsius / 5 + 32 9 * celsius / 5 + 32 means means (9 * celsius / 5) + 32 ((9 * celsius) / 5) + 32 86 87 Rule 3: Arity Bracketing Unary operators + , - before binary operators + , - . Any expression can be bracketed unambiguously using the associativities Example precedences 9 * celsius / + 5 + 32 arities (number of operands) means of the involved operators. 9 * celsius / (+5) + 32 88 89
Expression Trees Evaluation Order Bracketing leads to an expression tree “From leafs to the root” in the expression tree (((9 * celsius) / 5) + 32) 9 * celsius / 5 + 32 9 celsius 5 32 9 celsius 5 32 * * / / + + 90 91 Expression Trees – Notation Type System Usual notation: root on top Java festures a static type system: All types must be declared 9 * celsius / 5 + 32 If possible, the compiler checks the typing . . . + . . . otherwise it’s checked at run-time / 32 Advantages of a static type system Fail-fast Bugs in the program are often found already by the * 5 compiler 9 celsius Understandable code 92 93
Type errors Explicit Type Conversion Example Example int pi_ish; int pi_ish; float pi = 3.14f; float pi = 3.14f; pi_ish = (int) pi; pi_ish = pi; Compiler error: Explicit type conversion using casts ( type ) Statically type-correct, compiler is happy ./Root/Main.java:12: error: incompatible types: possible lossy conversion from float to int pi_ish = pi; Run-time behavior: depends on the situation ^ Here: loss of precision: 3.14 ⇒ 3 Can crash a program at run-time 94 95 Type Conversion - Visually for Integer Numbers Mixed Expressions, Conversion implicit conversion Floating point numbers are more general than integers. explicit cast byte In mixed expressions integers are converted to floating point numbers. short int 9 * celsius / 5 + 32 long Potential loss of information when casting explicitly, because less memory available to represent the number 96 97
Type Conversions for Binary Operations Numeric operands in a binary operation are being converted according to the following rules: 4. Efficiency of algorithms If both operands have the same type, no conversion will happen If one operand is double , the other operand is converted to double as well Efficiency of Algorithms, Random Access Machine Model, Function If one operand is float , the other operand is converted to float as well Growth, Asymptotics [Cormen et al, Kap. 2.2,3,4.2-4.4 | If one operand is long , the other operand is converted to long as well Ottman/Widmayer, Kap. 1.1] Otherwise: Both operands are being converted to int 98 99 Efficiency of Algorithms Technology Model Random Access Machine (RAM) Execution model: instructions are executed one after the other (on Goals one processor core). Quantify the runtime behavior of an algorithm independent of the Memory model: constant access time. machine. Fundamental operations: computations ( + , − , · ,...) comparisons, Compare efficiency of algorithms. assignment / copy, flow control (jumps) Understand dependece on the input size. Unit cost model: fundamental operations provide a cost of 1 . Data types: fundamental types like size-limited integer or floating point number. 100 101
Recommend
More recommend