Names of variables, functions, classes Identifiers: Names of variables, functions, classes (all user defined objects), Examples: if else while int float double main ... Can’t be used as identifiers They are reserved word a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate No special chars $ “ ; , ... Two identifiers, Tax and Rate 1 123 No numbers 5/6/2019 ITK 168
A name of a location in memory to hold value A variable: A name of a location in memory to hold value A value of what? 11110101 int a; String A; 245 0010 0010 0010 0100 0010 0101 the name the type the value 5/6/2019 ITK 168
Text String Binary: Text T e l : 2 8 7 4 9 EBCDIC E3 85 93 7A 40 F2 F8 ASCII 54 65 6C 2A 20 32 38 37 34 39 Binary: 0101 0100 0110 0101 0110 1100 0010 1010 0010 0000 0011 0010 0011 0010 0011 1000 0011 0111 0011 0010 0011 0100 0011 1001 5/6/2019 ITK 168
ASCII American National Standard Code for Information Interchange http://www.dynamoo.com/technical/ascii.htm American National Standard Code for Information Interchange 7-bit code Arabic ASCII Binary Hex 010 0000 20 1 010 0001 21 2 010 0010 22 3 010 0011 23 4 010 0100 24 5 010 0101 25 6 010 0110 26 7 010 0111 27 8 010 1000 28 9 010 1001 29 Char ASCII Binary Hex a 110 0001 61 b 110 0010 62 c 110 0011 63 … ……… A 100 0001 41 B 100 0010 42 C 100 0011 43 …. ……. : 011 1010 3A # 010 0011 23 5/6/2019 ITK 168
Numbers inside Computers Bits Minimum Maximum Integers Short Signed 8 -128 127 Unsigned 255 Integer 16 -32,768 32,767 65,535 Long 32 -2,147,483,648 2,147,483,647 4,294,967,295 Range Floating Points Single -3.410-38 3.41038 Double 64 -1.710-308 1.710308 Java’s Types short int long float double 5/6/2019 ITK 168
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; i = 3; j = 3.14; tax = 8.25/100; grade = ‘A’; grage = ‘B’; grade = 97; yes_or_no = true; yes_or_no = false; Every variable has to be declared before we use it. 5/6/2019 ITK 168
Variables and Assignments Everything that can receive a value L-value = R-value Everything that can give a value Assignment operator variable function expression literal variable The data types in both sides have to be consistent. If not, type-casting must be done. 5/6/2019 ITK 168
illegal Assignment examples L-value = R-value i = (3+7)*2-4-2*5; i = 3/2-0.5; illegal 3 = j; gcd(24,18) = 2+3; i+1 = j+3; i = 3/2.0-0.5; i = i+2; total = i+j; average = total/2; sum = average*10*tax_rate; i = gcd(24,18); i = Integer.parseInt( ); JOptionPane.showInputDialog("Input y") 5/6/2019 ITK 168
More on Assignments L-value = R-value L-value = R-value L-value = This operation itself will give a value L-value = R-value L-value = R-value L-value = L-value = a = b = c = i+j+3+gcd(15,6); illegal: a = b+1 = c = i+j+3+gcd(15,6); 5/6/2019 ITK 168
Parameter Passing How are parameters passed? Looks simple enough… formal parameters int plus (int a, int b) { return a+b; } int x = plus(1,2); function body actual parameters (arguments) function call How are parameters passed? Looks simple enough… There are some techniques 5/6/2019 ITK 168
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 5/6/2019 ITK 168
Parameter Passing function call 1 a 2 b int minus (int a, int b) { return a - b; } int x = minus(1, 2); function call 1 a 2 b 5/6/2019 ITK 168
Local variables int minus (int a, int b) { int x = a – b; return x; } public static void main(String[] args) { .... int a = 3; int b = 2; int d = 0; int x = a+b; d = minus(b,a); ..... } 5/6/2019 ITK 168
Arithmetic operators in Java + - * / % (x % y) is the remainder of x divided by y + - * / % (x % y) is the remainder of x divided by y Relational and logical operators in Java Resulting true or false < <= > >= == != && || ! & | ^ (20 < y) && (x <= i) 5/6/2019 ITK 168
Logical Operators || && ! Assume x = 10 true false true true false || && ! Assume x = 10 (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x < 5) is same as (x >= 5) (((x % 2) == 0) && ((x % 3) == 0)) true false true true false 5/6/2019 ITK 168