Educational Objectives You know the basic blocks of the programming language Java You understand the use of variables in a program and you can use 3. Java - Language Constructs I them properly You know how values are defined in the source code ( literals ) You are able to read and interpret simple arithmetic expressions Names and Identifiers, Variables, Assignments, Constants, You understand the reasons for a type system and are able to Datatypes, Operations, Evaluation of Expressions, Type Conversions determine the type of an expression 82 83 Definition: Names and Identifiers Names and Identifiers A program (that is, a class) needs a name public class SudokuSolver { ... Names denote things in a program like variables, constants, types, methods, or classes. Convention for class names: use CamelCase → Words are combined into one word, each starting with a capital letter Book, on page 21 Allowed names for “entities” in a program: Names begin with a letter or _ or $ Then, sequence of letters , numbers or _ or $ 84 85
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 86 87 Definition: Variables Variables Variables are buckets for a value Have a data type and a name Variables are buckets for values and have a specified type . Variables The data type determines what kind of values are allowed in the need to be declared before first use. variable int y int x float f char c Declaration in Java : Book, on page 23 int x = 23, y = 42; 23 42 0.0f ’a’ float f; char c = ’a’; Initialization 88 89
Definition: Constants Constants Keyword final The value of the variable can be set exactly once Constants are variables that are initialized upon declaration and may not change their value later on. Example final int maxSize = 100; Book, on page 35 Hint : Always use final , unless the value actually needs to change over time. 90 91 Definition: Types Definition: Standard Types A Type defines a set of values that belong to the type as well as a Java provides several predefined types for various numeric ranges set of operations that can be performed with the values of the type. as well as boolean values and strings. Book, on page 24 Book, on page 24 92 93
Standard Types Types and Memory Usage Data Type Definition Value Range Initial Value Reminder: Memory cells contain 1 Byte = 8 bit byte 8-bit integer − 128 , . . . , 127 0 − 32 ′ 768 , . . . , 32 ′ 767 short 16-bit integer 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 unicode-16 character ’\u0000’ ,. . . , ’a’ , ’b’ ,. . . , ’\uFFFF’ char ’\u0000’ long , double ∞ String string null 94 95 Definition: Literals Literals: Integer Numbers Type int (or short , byte ) 12 : value 12 Representation of a value of a standard type in the source code. -3 : value − 3 Book, on page 22 - 23 Type long 25_872_224L : value 25 ′ 872 ′ 224 Hint: Underscores between digits are allowed! 96 97
Literals: Floating Point Numbers Literals: Characters and Strings are different from integers by providing Individual characters: decimal comma 1.23e-7f ’a’ : Type char , value 97 1.0 : type double , value 1 Strings: exponent integer part 1.27f : type float , value 1 . 27 "Hello There!" : Type String and / or exponent. fractional part 1e3 : type double , value 1000 "a" : Type String 1.23e-7 : type double , value 1 . 23 · 10 − 7 Mind: Characters and Strings are two different things! 1.23e-7f : type float , value 1 . 23 · 10 − 7 98 99 Character: In ASCII Table Definition: Assignments An assignment is used to store a (computed) value into a variable. Book, on page 27 100 101
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. 102 103 Definition: Arithmetic Expressions Arithmetic Binary Operators Infix notation: x op y with the following operators op: + − ∗ / % An arithmetic expression consists of operands and operators and computes a numeric value of a given type. modulo Book, on page 28 Precedence : Multiplication, division, and modulo first, then addition and subtraction Associativity : Evaluation from left to right 104 105
Arithmetic Binary Operators Arithmetic Assignment Division x / y : Integer division if x and y are integer. x = x + y Division x / y : Floating-point division if x or y is a floating-poing Examples : � number! x − = 3; // x = x − 3 name += "x" // name = name + "x" x += y Examples num ∗ = 2; // num = num ∗ 2 Integer division and modulo − 5 / 3 evaluates to − 1 Analogous for − , ∗ , / , % 5 / 3 evaluates to 1 − 5 % 3 evaluates to − 2 5 % 3 evaluates to 2 106 107 Arithmetic Unary Operators Increment/Decrement Operators Increment operators ++x and x++ have the same effect: x ← x + 1 .But different return values: Prefix notation: + x or − x Prefix operator ++x returns the new value: Precedence: Unary operators bind stronger than binary operators ⇐ ⇒ a = ++x; x = x + 1; a = x; Examples Postfix operator x++ returns the old value: Assuming x is 3 ⇐ ⇒ a = x++; temp = x; x = x + 1; a= temp; 2 ∗ − x evaluates to − 6 Precedence: Increment and decrement operators bind stronger − x − +1 evaluates to − 4 than unary operators Analogous for x −− and −− x . 108 109
Increment/Decrement Operators Expressions represent computations Examples are either primary Assuming x is initially set to 2 or composed . . . y = ++x ∗ 3 evaluates to: x is 3 and y is 9 . . . from other expressions, using operators y = x++ ∗ 3 evaluates to: x is 3 and y is 6 are statically typed Analogy: Construction kit 110 111 Expressions Celsius to Fahrenheit public class Main { Examples public static void main(String[] args) { primary: “ − 4.1d ” or “ x ” or "Hi" Out.print("Celsius: "); int celsius = In.readInt(); composed: “ x + y ” or “ f ∗ 2.1f ” float fahrenheit = 9 * celsius / 5 + 32; Out.println("Fahrenheit: " + fahrenheit); The type of “ 12 ∗ 2.1f ” is float } } Example : 15 ◦ Celsius are 59 ◦ Fahrenheit 112 113
Celsius to Fahrenheit - Analysis Rule 1: Precedence Multiplicative operators ( * , / , % ) have a higher precedence ("bind 9 * celsius / 5 + 32 stronger") than additive operators ( + , - ). Arithmetic expression, Example 9 * celsius / 5 + 32 contains three literals, one variable, three operator symbols means Where are the brackets in this expression? (9 * celsius / 5) + 32 114 115 Rule 2: Associativity Rule 3: Arity Arithmetic operators ( * , / , % , + , - ) are left-associative: in case of the Unary operators + , - before binary 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 116 117
Bracketing Expression Trees Bracketing leads to an expression tree (((9 * celsius) / 5) + 32) Any expression can be bracketed unambiguously using the associativities 9 celsius 5 32 precedences * arities (number of operands) of the involved operators. / + 118 119 Evaluation Order Expression Trees – Notation “From leafs to the root” in the expression tree Usual notation: root on top 9 * celsius / 5 + 32 9 * celsius / 5 + 32 5 9 celsius 32 + * / 32 / * 5 + 9 celsius 120 121
Definition: Type System Type System Java festures a static type system: All types must be declared A type system is a set of rules that are applied to the different If possible, the compiler checks the typing . . . constructs of the language. . . . otherwise it’s checked at run-time Advantages of a static type system Book, on page 24 Fail-fast Bugs in the program are often found already by the compiler Understandable code 122 123 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 124 125
Recommend
More recommend