Identifiers Names of variables functions classes all user

Identifiers: Names of variables, functions, classes (all user defined objects), Examples: if else while int float double main. . . a b gcd GCD A COSC 1373 TAX Tax_Rate Tax Rate $ “ ; , . . . Two identifiers, Tax and Rate 9/8/2021 1 123 ITK 168 Can’t be used as identifiers They are reserved word No special chars No numbers 1

A variable: A name of a location in memory to hold value A value of what? int a; String A; 245 11110101 245 0010 0100 0010 0101 1. the name 2. the type 3. the value 9/8/2021 ITK 168 2

Text String Text T e l : EBCDIC E 3 85 93 7 A ASCII 54 65 6 C 2 A 2 2 8 7 2 4 9 40 F 2 F 8 F 7 F 2 F 4 F 9 20 32 32 38 37 32 34 39 Binary: 0101 0100 0110 0101 0110 1100 0010 1010 0000 0011 0010 0011 1000 0011 0111 0010 0011 0100 0011 1001 9/8/2021 ITK 168 3

ASCII http: //www. dynamoo. com/technical/ascii. htm American National Standard Code for Information Interchange n 7 -bit code Arabic 9/8/2021 ASCII Char Binary Hex 0 010 0000 20 1 010 0001 2 ASCII Binary Hex a 110 0001 61 21 b 110 0010 62 010 0010 22 c 110 0011 63 3 010 0011 23 … ……… … 4 0100 24 A 100 0001 41 5 0101 25 B 100 0010 42 6 010 0110 26 C 100 0011 43 7 010 0111 27 …. … 8 010 1000 28 : 011 1010 3 A 9 010 1001 29 # 010 0011 23 ITK 168 4

Numbers inside Computers Java’s Types Bits Signed Short short Integers int long Unsigned Integer Long Signed Unsigned 8 16 32 Minimum Maximum -128 127 0 255 -32, 768 32, 767 0 65, 535 -2, 147, 483, 648 2, 147, 483, 647 0 4, 294, 967, 295 Bits float Floating double Points 9/8/2021 Range Single 32 -3. 4 10 -38 3. 4 1038 Double 64 -1. 7 10 -308 1. 7 10308 ITK 168 5

8 primitive data types in Java short i, j; int no; long sum; float total; double tax; char grade; byte b; boolean yes_or_no; 9/8/2021 i = 3; j = 3. 14; tax = 8. 25/100; grade = ‘A’; grage = ‘B’; grade = 97; yes_or_no = true; yes_or_no = false; ITK 168 Every variable has to be declared before we use it. 6

Variables and Assignments Everything that can receive a value L-value = R-value Assignment operator variable The data types in both sides have to be consistent. If not, type-casting must be done. 9/8/2021 ITK 168 Everything that can give a value variable function expression literal 7

Assignment examples L-value = R-value i = (3+7)*2 -4 -2*5; i = 3/2 -0. 5; illegal i = 3/2. 0 -0. 5; i = i+2; total = i+j; average = total/2; sum = average*10*tax_rate; i = gcd(24, 18); 3 = j; gcd(24, 18) = 2+3; i+1 = j+3; i = Integer. parse. Int( JOption. Pane. show. Input. Dialog("Input y") ); 9/8/2021 ITK 168 8

More on Assignments L-value = R-value This operation itself will give a value L-value = R-value a = b = c = i+j+3+gcd(15, 6); illegal: a = b+1 = c = i+j+3+gcd(15, 6); 9/8/2021 ITK 168 9

Parameter Passing formal parameters int plus (int a, int b) { return a+b; } function call int x = plus(1, 2); function body actual parameters (arguments) • How are parameters passed? • Looks simple enough… • There are some techniques 9/8/2021 ITK 168 10

Parameter Correspondence • Which actual parameters go to which formal parameters? • Most common case: positional parameters – Correspondence determined by positions – nth formal parameter matched with nth actual 9/8/2021 ITK 168 11

Parameter Passing int minus (int a, int b) { return a - b; } function call int x = minus(1, 2); 1 a 2 b 9/8/2021 ITK 168 12

Local variables int minus (int a, int b) { int x = a – b; return x; } public static void main(String[] args) {. . int int d = a = 3; b = 2; d = 0; x = a+b; minus(b, a); . . . } 9/8/2021 ITK 168 13

Arithmetic operators in Java + - * / % (x % y) is the remainder of x divided by y Relational and logical operators in Java Resulting true or false < <= > && || ! >= != | ^ & == (20 < y) && (x <= i) 9/8/2021 ITK 168 14

Logical Operators || && ! Assume x = 10 (1 || 0) true ((18 <= x) && (x <= 50)) false ((18 <= x) || (x <= 50)) true !(x < 5) is same as (x >= 5) true (((x % 2) == 0) && ((x % 3) == 0)) false 9/8/2021 ITK 168 15
- Slides: 15