Unit%203%20 %20 Lecture%20slides%20 %20 Simple%20programs

326 views
274 views

Published on

lecture nots

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
326
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Unit%203%20 %20 Lecture%20slides%20 %20 Simple%20programs

  1. 1. Primitive data types Values Bytes Type - 3.4028234663852886e+38 to - 1.4012984632481707e-45 (and +ve) 4 float -1.7976931348623157e+308 to -4.94065645841246544e-324 (and +ve) 8 double (- 2 63 to 2 63 -1) 8 long -2,147,483,648- +2,147,483,648 (2 31 ) 4 int -32,768 - +32,767 (- 2 15 to 2 7 -1) 2 short 0 to 65535 (-2 7 to 2 7 -1) 2 char false. true 2 boolean
  2. 2. <ul><li>int year; </li></ul><ul><li>Declares a variable to store a whole number </li></ul><ul><li> year </li></ul><ul><li>char initial; </li></ul><ul><li>Declares a variable to store a character </li></ul><ul><li> initial </li></ul>Declaring a variable ? ?
  3. 3. Declaring a constant final double TAXRATE = 17.5; Declares a named constant which will hold a decimal value, this is initialised as part of the declaration. The value may then not be changed during program execution. 17.5 TAXRATE
  4. 4. <ul><li>The assignment operator = places a value into a variable </li></ul><ul><li> year </li></ul><ul><li>int year = 1945; </li></ul><ul><li>Declares a variable to store a whole number and </li></ul><ul><li>Initialises its value to 1945 </li></ul><ul><li>year = 1604; </li></ul><ul><li>The value on right is placed into the memory location </li></ul><ul><li>known by the identifier year any existing value is lost </li></ul><ul><li>year = 1604 + age; </li></ul><ul><li>The expression on right is evaluated and the result </li></ul><ul><li>placed into the memory location known by the identifier </li></ul><ul><li>year any existing value is lost (assume age was 25) </li></ul><ul><li> </li></ul>Assigning values 1604 1945 1629
  5. 5. Arithmetic Operators <ul><li>Java supports the following arithmetic operators and evaluates them in this order of precedence </li></ul><ul><ul><li>* Multiplication </li></ul></ul><ul><ul><li>/ Division </li></ul></ul><ul><ul><li>% Modulus (remainder after integer division) </li></ul></ul><ul><ul><li>+ Addition </li></ul></ul><ul><ul><li>Subtraction </li></ul></ul>
  6. 6. <ul><li>int age1 = 24; </li></ul><ul><li>int age 2 = 35; </li></ul><ul><li>int totalAge = 0; </li></ul><ul><li>float averageAge = 0.0; </li></ul><ul><li>age1 age2 totalAge averageAge </li></ul>Arithmetic Operators 24 35 0 0.0
  7. 7. <ul><li>totalAge = age1 + age2; </li></ul><ul><li>averageAge = totalAge /2; </li></ul><ul><li>Will these statements make a difference to the values stored in the variables? </li></ul><ul><li>age1 age2 totalAge averageAge </li></ul>Arithmetic Operators 24 35 0 0.0
  8. 8. <ul><li>Notice there has been no change to the values of age1 or age2 </li></ul><ul><li>The assignment of new values to totalAge and averageAge has overwritten the original values </li></ul><ul><li>age1 age2 totalAge averageAge </li></ul>Arithmetic Operators 24 35 59 29.5

×