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 $ 85 86 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 87 88
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 float f int x 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 89 90 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 91 92
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 93 94 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! 95 96
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 b = 3.141; In Java: x = value (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. 97 98 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 99 100
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 101 102 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 . 103 104
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 105 106 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 107 108
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 109 110 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 111 112
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 * * / / + + 113 114 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 115 116
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 117 118 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 119 120
Type Conversions for Binary Operations Numeric operands in a binary operation are being converted according to the following rules: 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 If one operand is float , the other operand is converted to float as well If one operand is long , the other operand is converted to long as well Otherwise: Both operands are being converted to int 121
Recommend
More recommend