Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.

Similar presentations


Presentation on theme: "Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments."— Presentation transcript:

1 Primitive data Week 3

2 Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments Arithmetic operators Boolean operators

3 Example 123 (int) 1.5 (double) “HelloWorld” (String) `H’ (Char) ….

4 Data Types Constants Variables

5 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#

6 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

7 Data type Declarations Specify the type of data and the length of the data item in bytes int, short, long float, double boolean char

8 Data Types -- Integer Int – the default declaration – 4-byte integer Byte—1-byte integer Short—2-byte integer Long—8-byte integer

9 Floating Point Float—a 4-byte floating point number Double—an 8-byte floating point number

10 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?

11 Primitives sizes and Ranges PRIMITIVESIZE IN BITSRANGE int32bits (4 bytes)-2 31 to 2 31 -1 -2,147,483,648 to +2,147,483,647 long64bits -- 8 bytes-2 63 to 2 63 - 1 float32bits- -4 bytes-+(1.40129846432481707e-45 to 3.40282346638528860e+38} double64+-(4.94065645841246544e-324 to 1.79769313486231570e+308) char16bitsOne character string16bits per char Not applicable bool (boolean in Java)8bits--1-byteTrue or false

12 Examples TypeSet of valuesSample literal vlues intinterges99 (-12) 214748647 doubleFloating-point numbers3.14 (-1.5) 6.0021 10 23 booleanBoolean valuesTrue or false charcharacters‘a’ ‘1’ ‘£’ ‘%’ ‘\n’ StringSequence of characters“AC” ”Hello” ” 1.5”

13 Variable declaration declarationVariable nameVariable type Int x;xinteger double d;ddouble char c;ccharacter String s;sstring Float f;ffloat

14 The assignment operator = declarationVariable name Int x; x = 36; Declare the variable x as an integer Sets x to constant 36 at execution time int x = 36; Sets x = to the constant 36 at compile time Initializes x to 36 at the time memory is set aside for it String y; y = “Hellow”; Declare the variable x as an integer Sets y to constant “Hello” at execution time String y = “Hello”; Sets y = to the constant “Hello” at compile time Initializes x to “Hello” at the time memory is set aside for it

15 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

16 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.

17 int a, b, c ; b =1; a=b; c =a; System.out.print(“c= “ + c); What is the value of a, b & c Declaration (Cont)

18 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

19 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

20 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

21 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

22 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);

23 Basic arithmetic Operators OperatorMeaningE=3 xample +Addition2+3 = 5 *Multiplication2*3=6 -subtraction3-2=1 /division4/2=2 %mod5 % 2 = 1, 6 % 2= 0

24 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

25 Example SumStr.java public class SumStr { public static void main(String args[]) { System.out.print(args[0] + args[1] ); } 27 Java Argument 2 7

26 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] ) ); } 9 Java SumInt 2 7

27 Basic boolean Operators OperatorMeaningE=3 xample == equal(4-2)==(8-6) !=Not equal3 !=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

28 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

29 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

30 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; }

31 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”); }

32 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”); }

33 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 }

34 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!

35 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; }

36 Summary Different data type Declarations Arithmetic operators Parse string to integer. Boolean operators Assignments If statement Switch statement.

37 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.


Download ppt "Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments."

Similar presentations


Ads by Google