1 / 37

Primitive data

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.

Download Presentation

Primitive data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  12. Examples

  13. Variable declaration

  14. The assignment operator =

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

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

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

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

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

  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

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

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

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

  27. Basic boolean Operators

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

  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.

More Related