Successfully reported this slideshow.

2 1 data

514 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

2 1 data

  1. 1. Primitive Data Types <ul><li>Represent single values </li></ul><ul><li>Practically unlimited access </li></ul><ul><li>Integers types: </li></ul><ul><ul><li>2010 </li></ul></ul><ul><ul><li>-40 </li></ul></ul><ul><li>Floating point (decimals) numbers types: </li></ul><ul><ul><li>Big integers </li></ul></ul><ul><ul><li>Decimals: 3.14159 </li></ul></ul><ul><ul><li>Scientific notation: 6.03e23 </li></ul></ul><ul><li>Boolean type: true or false </li></ul><ul><li>Character type: ‘c’ </li></ul><ul><li>Strings : “Vote for Pedro” </li></ul>
  2. 2. Variables <ul><li>To store or remember data Java uses variables . </li></ul><ul><li>Variables contain primitive values or object references. </li></ul><ul><li>To be used, variables have to be declared and set </li></ul><ul><li>Declaring a variable: </li></ul><ul><ul><li>Announces is existence to the rest of the block </li></ul></ul><ul><ul><li>Sets aside a section of memory big enough to store the value </li></ul></ul><ul><ul><li>Determines what kind of value can be stored in the variable </li></ul></ul><ul><ul><li>Determines how the number is to be interpreted </li></ul></ul>
  3. 3. Variables <ul><li>Variable names should be describe the value they hold </li></ul><ul><li>Names with multiple words: </li></ul><ul><ul><li>Lower case first word, capitalize subsequent words </li></ul></ul><ul><ul><li>Separate words with underscore. </li></ul></ul><ul><li>Declaring a variable: </li></ul><ul><ul><li>Type name followed by the name and optional initialization </li></ul></ul><ul><ul><ul><li>int studentAge = 18; </li></ul></ul></ul><ul><ul><ul><li>double pi = 3.14159; </li></ul></ul></ul><ul><ul><ul><li>boolean isValid = false; </li></ul></ul></ul><ul><ul><ul><li>char marks_the_spot = ‘x’; </li></ul></ul></ul><ul><ul><ul><li>String vote4 = “Pedro”; </li></ul></ul></ul>
  4. 4. Variable Scope <ul><li>Variables are known inside the block in which they are declared. </li></ul><ul><ul><li>class Foo { </li></ul></ul><ul><ul><li>int x = 0; </li></ul></ul><ul><ul><li>void someMethod() { </li></ul></ul><ul><ul><li>int y; </li></ul></ul><ul><ul><li>y = x + 1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>void anotherMethod() { </li></ul></ul><ul><ul><li>x = y + 1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  5. 5. Integers <ul><li>Integers are 32 bits long </li></ul><ul><li>-2,147,483,648 <= int <= 2,147,483,647 </li></ul><ul><li>Example declarations and values </li></ul><ul><li>int m; </li></ul><ul><li>m = 0; </li></ul><ul><li>int n = 23; </li></ul><ul><li>int o = -40; </li></ul>
  6. 6. Floating Point <ul><li>The type is called double </li></ul><ul><li>64 bits long </li></ul><ul><ul><li>Some bits used for the exponent, some for the mantissa </li></ul></ul><ul><li>+/- 1.7 x 10 ± 308 with 15 significant digits </li></ul><ul><li>double pi; </li></ul><ul><li>pi = 3.14159; </li></ul><ul><li>double x = 10.0; </li></ul><ul><li>double y = 6.02e23; </li></ul><ul><li>double z = -40; </li></ul>
  7. 7. Boolean <ul><li>A boolean value represents true or false </li></ul><ul><li>A boolean also can be used to represent any value with two states, such as a light bulb being on or off. </li></ul><ul><li>The reserved words true and false are the only valid values for a boolean type </li></ul><ul><li>boolean done = false; </li></ul><ul><li>boolean isMale = true; </li></ul>
  8. 8. Characters <ul><li>A char variable stores the numeric representation of a character </li></ul><ul><li>The Unicode character set lists the characters and their corresponding numeric representation </li></ul><ul><ul><li>The Unicode character set uses sixteen bits per character, allowing for 65,536 unique symbols and characters from many world languages </li></ul></ul><ul><li>Character literals are delimited by single quotes: </li></ul><ul><li>'a' 'X' '7' '$' ',' ' ‘ </li></ul><ul><li>char marksTheSpot = ‘X'; </li></ul>
  9. 9. Escape Sequences <ul><li>The unicode set recognizes ‘characters’ that are not printable </li></ul><ul><li>These unprintable characters are referred to with escape sequences </li></ul>Escape Sequence  Unprintable character backspace tab newline carriage return
  10. 10. Escape sequences <ul><li>Printing unprintable characters moves the terminal window cursor </li></ul><ul><ul><li>Printing a backspace character moves the cursor backwards on the same line </li></ul></ul><ul><ul><li>Printing a carriage return moves the cursor to the front of the current line </li></ul></ul><ul><ul><li>Printing a new line drops the cursor to the beginning of the next line </li></ul></ul><ul><ul><li>Print a tab moves the cursor to ahead to the next tab stop </li></ul></ul>
  11. 11. Escape sequences <ul><li>Special characters must be escaped when using them as just a character </li></ul><ul><ul><li>char singleQuote = ‘’’; </li></ul></ul><ul><ul><li>char doubleQuote = ‘”’; </li></ul></ul><ul><ul><li>char backslash = ‘; </li></ul></ul><ul><li>In other words, the back-slash tells Java to ignore the normal meaning of the very next character </li></ul>
  12. 12. Character Strings <ul><li>A character string is a sequence of characters delimited by double-quotes: </li></ul><ul><ul><li>“ To be, or not to be” </li></ul></ul><ul><ul><li>(Actually, character strings are not a primitive data type, but an object .) </li></ul></ul><ul><ul><li>String eagles; </li></ul></ul><ul><ul><li>eagles = “Hell, no!!”; </li></ul></ul><ul><ul><li>String ssn = “147431904”; </li></ul></ul>
  13. 13. Escape Sequences <ul><li>What if we wanted to print a double quote character? System.out.println (&quot;I said &quot;Hello&quot;.&quot;); </li></ul><ul><li>An escape sequence is a series of characters that represents a special character </li></ul><ul><li>An escape sequence begins with a backslash character ( ), which indicates that the character(s) that follow should be treated in a special way: </li></ul><ul><li>System.out.println (&quot;I said &quot;Hello&quot;.&quot;); </li></ul>
  14. 14. Constants <ul><li>A constant is a variable that can only be assigned a value once. </li></ul><ul><li>The final modifier to declares a constant </li></ul><ul><ul><li>final int MIN_HEIGHT = 60; </li></ul></ul><ul><ul><li>final int MAX_HEIGHT; </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>MAX_HEIGHT = 74; </li></ul></ul><ul><li>Constants: </li></ul><ul><ul><li>give names to otherwise unclear literal values </li></ul></ul><ul><ul><li>facilitate updates of values used throughout a program </li></ul></ul><ul><ul><li>prevent inadvertent attempts to change a value </li></ul></ul>
  15. 15. Assignment <ul><li>An assignment statement changes the value of a variable </li></ul><ul><li>The assignment operator is the = sign </li></ul>total = 55; <ul><li>The expression on the right is evaluated and the result is stored in the variable on the left </li></ul><ul><li>The value that was in total is overwritten (and lost) </li></ul><ul><li>You can only assign a value to a variable that is consistent with the variable's declared type </li></ul>
  16. 16. Keyboard input <ul><li>Reads typing from the console </li></ul><ul><ul><li>Terminal window or DOS window </li></ul></ul><ul><li>Based on the concept of the token </li></ul><ul><ul><li>Tokens are string or printable characters separated by delimiters </li></ul></ul><ul><ul><li>E.g., “Vote for Pedro” has three tokens separated by a white-space delimiter. </li></ul></ul><ul><ul><li>E.g., “09/15/2010” has three tokens separated by a slash delimiter </li></ul></ul>
  17. 17. Reading Streams <ul><li>The scanner object extracts tokens from the input stream. </li></ul><ul><li>Input stream can be the keyboard, a file, or a socket. </li></ul><ul><li>For example, creating the scanner </li></ul><ul><ul><li>// Create a variable of type Scanner </li></ul></ul><ul><ul><li>// Connect the scanner to the keyboard </li></ul></ul><ul><ul><li>Scanner scan = new Scanner(System.in); </li></ul></ul><ul><li>Read the scanner </li></ul><ul><ul><ul><li>String data = scan.nextLine(); </li></ul></ul></ul>
  18. 18. Reading Streams <ul><li>Read the scanner, again </li></ul><ul><ul><ul><ul><li>// get the next token, hope it’s an integer </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int i = scan.nextInt(); </li></ul></ul></ul></ul>

×