370 likes | 481 Views
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.
E N D
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?
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
ExampleInt1.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
ExampleInt2.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
ExampleInt5.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
ExampleString2.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);
Precedence Rules • Evaluate all sub-expressions in parentheses • Evaluate nested parentheses from the inside out • In the absence of parentheses or within parentheses • Evaluate *, /, or % before + or – • Evaluate sequences of *, /, and % operators from left to right • Evaluate sequences of + and – operators from left to right
ExampleSumStr.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
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 }
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! A Warning…
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.