Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Primitive data Week 3 Lecture outcomes • Primitive data – – – – – – – • • • • • integer double string char Float Long boolean Declaration Initialisation Assignments Arithmetic operators Boolean operators Example • • • • • 123 (int) 1.5 (double) “HelloWorld” (String) `H’ (Char) …. Data Types • Constants • Variables What is a Constant? • 456—a literal numerical constant – System.out.println(456); // Java – Console.writeline(456); // Visual C# • “A Literal String Constant” – System.out.println(“My First Java”); // Java – Console.writeline(“My First C#”); // Visual C# What is a variable? • It is a named computer location in memory that holds values that might vary • Must that location have an address? – YES • What has addresses? Bits, bytes, words, what? – Bytes • Can a variable be more than one byte long? – YES Data type Declarations • Specify the type of data and the length of the data item in bytes • int, short, long • float, double • boolean • char Data Types -- Integer • • • • Int – the default declaration – 4-byte integer Byte—1-byte integer Short—2-byte integer Long—8-byte integer Floating Point • Float—a 4-byte floating point number • Double—an 8-byte floating point number There are eight primitive data types • Boolean, byte, char, int, double, float, long, short • In bytes, how long is the short data type? The int data type, the long data type? • In bytes, how long is the float data type? The double data type? • How long is the char data type? Primitives sizes and Ranges PRIMITIVE SIZE IN BITS RANGE int 32bits (4 bytes) -231 to 231 -1 -2,147,483,648 to +2,147,483,647 long 64bits -- 8 bytes -263 to 263 - 1 float 32bits- -4 bytes -+(1.40129846432481707e-45 to 3.40282346638528860e+38} double 64 +-(4.94065645841246544e-324 to 1.79769313486231570e+308) char 16bits One character string 16bits per char Not applicable bool (boolean in Java) 8bits--1-byte True or false Examples Type Set of values Sample literal vlues int interges 99 (-12) 214748647 double Floating-point numbers 3.14 (-1.5) 6.0021 1023 boolean Boolean values True or false char characters ‘a’ ‘1’ ‘£’ ‘%’ ‘\n’ String Sequence of characters “AC” ”Hello” ” 1.5” Variable declaration declaration Variable name Variable type Int x; x integer double d; d double char c; c character String s; s string Float f; f float The assignment operator = declaration Variable name Int x; Declare the variable x as an integer x = 36; Sets x to constant 36 at execution time Sets x = to the constant 36 at compile time int x = 36; Initializes x to 36 at the time memory is set aside for it String y; Declare the variable x as an integer y = “Hellow”; Sets y to constant “Hello” at execution time Sets y = to the constant “Hello” at compile time String y = “Hello”; Initializes x to “Hello” at the time memory is set aside for it Initialisation • If no value is assigned prior to use, then the compiler will give an error • Java sets primitive variables to zero or false in the case of a boolean variable • All object references are initially set to null • An array of anything is an object – Set to null on declaration – Elements to zero false or null on creation Declaration Examples int index = 1.2; // compiler error boolean retOk = 1; // compiler error double fiveFourths = 5 / 4; // no error! float ratio = 5.8f; // correct double fiveFourths = 5.0 / 4.0; // correct • 1.2f is a float value accurate to 7 decimal places. • 1.2 is a double value accurate to 15 decimal places. Declaration (Cont) int a, b, c ; b =1; a=b; c =a; System.out.print(“c= “ + c); • What is the value of a, b & c Example Int1.java // uninitialised data // this program will declare and print a number Public class int3 { public static void main(String[] arguments) { int weight; System.out.println("your weight is " + weight); } } //end of program Example Int2.java // this program will declare and print a number class int2 { public static void main(String[] arguments) { int weight = 68; System.out.println("your weight is " + weight); } } //end of program Example Int5.java // uninitialised data // this program will declare and print a number class int5 { public static void main(String[] arguments) { int weight; weight = 65 ; //65 = weight ; System.out.println("your weight is " + weight); } } //end of program Example String2.java // this program will declare and print a string class string2 { public static void main(String[] arguments) { String name = "Lahcen"; String x = "my name is "; System.out.println( x + name ); //print string x and then string name } } //end of program Basic Mathematical Operators • * / % + - are the mathematical operators • * / % have a higher precedence than + or double myVal = a + b % d – c * d / b; • Is the same as: double myVal = (a + (b % d)) – ((c * d) / b); Basic arithmetic Operators Operator Meaning E=3 xample + Addition 2+3 = 5 * Multiplication 2*3=6 - subtraction 3-2=1 / division 4/2=2 % mod 5 % 2 = 1, 6 % 2= 0 Precedence Rules 1. Evaluate all sub-expressions in parentheses 2. Evaluate nested parentheses from the inside out 3. In the absence of parentheses or within parentheses a. Evaluate *, /, or % before + or – b. Evaluate sequences of *, /, and % operators from left to right c. Evaluate sequences of + and – operators from left to right Example SumStr.java public class SumStr { public static void main(String args[]) { System.out.print(args[0] + args[1] ); Java Argument 2 7 } } } } 27 parse a string to integer. SumInt.java public class SumInt { public static void main(String args[]) { System.out.print( Integer.parseInt(args[0] )+ Integer.parseInt(args[1] ) ); Java SumInt 2 7 } } } } 9 Basic boolean Operators Operator Meaning E=3 xample == equal (4-2)==(8-6) != Not equal 3 !=2 is true but 4!=(6-2) is false > Greater than (3>2) is true >= Greater or equal (3>=2) is true < Less than (3<2) is false <= Less or equal (3<=4) is true Statements & Blocks • A simple statement is a command terminated by a semi-colon: name = “Fred”; • A block is a compound statement enclosed in curly brackets: { name1 = “Fred”; name2 = “Bill”; } • Blocks may contain other blocks Flow of Control • Java executes one statement after the other in the order they are written • Many Java statements are flow control statements: Alternation: if, if else, switch Looping: for, while, do while Escapes: break, continue, return If – The Conditional Statement • The if statement evaluates an expression and if that evaluation is true then the specified action is taken if ( x < 10 ) x = 10; • If the value of x is less than 10, make x equal to 10 • It could have been written: if ( x < 10 ) x = 10; • Or, alternatively: if ( x < 10 ) { x = 10; } If… else • The if … else statement evaluates an expression and performs one action if that evaluation is true or a different action if it is false. if (x != oldx) { System.out.print(“x was changed”); } else { System.out.print(“x is unchanged”); } Nested if … else if ( myVal > 100 ) { if ( remainderOn == true) { myVal = mVal % 100; } else { myVal = myVal / 100.0; } } else { System.out.print(“myVal is in range”); } else if • Useful for choosing between alternatives: if ( n == 1 ) { // execute code block #1 } else if ( j == 2 ) { // execute code block #2 } else { // if all previous tests have failed, execute code block #3 } A Warning… WRONG! if( i == j ) if ( j == k ) System.out.print( “i equals k”); else System.out.print( “i is not equal to j”); CORRECT! if( i == j ) { if ( j == k ) System.out.print( “i equals k”); } else System.out.print(“i is not equal to j”); // Correct! The switch Statement switch ( n ) { case 1: // execute code block #1 break; case 2: // execute code block #2 break; default: // if all previous tests fail then //execute code block #4 break; } Summary • • • • • • • • Different data type Declarations Arithmetic operators Parse string to integer. Boolean operators Assignments If statement Switch statement. Home work • Practice with all the exercise on the website • Read chapter 4 on the study guide: – Redo all the examples – Do all the exercises.