1 / 14

Chapter 5 – Data Types

Chapter 5 – Data Types. Primitives vs Objects Operators vs Methods Comparing objects in if statements Using double for non integer numbers large values, or those with decimals (3.14) Arithmetic using ints and doubles Math class for doing trig and other calcs Strings. Primitive Data Type.

Download Presentation

Chapter 5 – Data Types

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. Chapter 5 – Data Types • Primitives vs Objects • Operators vs Methods • Comparing objects in if statements • Using double for non integer numbers • large values, or those with decimals (3.14) • Arithmetic using ints and doubles • Math class for doing trig and other calcs • Strings

  2. Primitive Data Type • Can write value as a literal Ex: int a=1732 • Can perform operations using operator symbols Ex: x+1

  3. Operators vs. Method Invocations • Operators produce new values Ex: if the value of count is 3 count+1 will produce 4, but will not change count • Method Invocations can modify objects Ex: box.move(-1,-1); changes the position of the box

  4. Comparing Primitives vs Objects • Primitive variables like int and double: • int x = 2, y = 5; • To see if two int variables contain the same value, we say: if (x == y)

  5. Comparing Primitives vs Objects • However, for objects: • point = new Location(10,10); • click = new Location(10, 10); • point2 = point; // point2 refers to Location named point // point and ponit2 are 2 names for the same object // click and point are different objects with the same data • if (point == click) is ALWAYS False (differnt objects) • if (point == point2) is ALWAYS True (same objects) • if (point.equals(click) is true if they contain same data. Use this to compare 2 different objects, i.e. objects created with different constructors.

  6. Point Point2 click [10, 10] [10, 10]

  7. Operators and Precedence Rules • PARENTHESES!!! ( ) • Numeric and logical negations, - and ! ( - temp or ! grabbed ) • Multiplication and division: * and / • Addition and subtraction: + and – • Comparisons: <,<=,>,>= • Equality and inequality: == and != • And: && • Or: || Ex: a+b*c=a+(b*c) but not (a+b)*c

  8. Numeric Types • Integers: Ex: int anInt=99; • Real numbers: Ex: double aDouble=98.6; An int can be converted into a double without loss of precision, but not vice versa

  9. Dividing int and double The following table summaries the types of result one will get when dividing two integers, two doubles, or a double and an integer Clearly, unless an integer is divided by another integer, all results are double. Ex: 3.0 / 4.0 = 0.75 3 / 4.0=0.75 3 / 4 = 0

  10. How a double is displayed • If you print a double, the output is always accompanied by a decimal place. Ex:double a=1000; System.out.print(a);will output 1000.0 • Large numbers use scientific notation Ex: double a=1000000000; System.out.print(a); will output 1.0E9

  11. Selecting a Numeric Type • Use double instead of int whenever possible • Use int when methods demand it Ex: setColor( int, int, int )

  12. Useful Functions on double

  13. System.currentTimeMillis() • Used to check elapsed time Ex: printing out the duration of a mouse press public void onMousePress(Location point){ startingTime=System.currentTimeMillis(); } public void onMouseRelease(Location point){ System.out.println(System.currentTimeMillis()-startingTime)); } • Time in milliseconds since January 1, 1970 !!

  14. String • Java uses Stringto manipulate textual data • Quoted text is of type String • Programs can combine Stringobjects Ex: Stringa="He"; Stringb="llo"; System.out.print(a+b); will print out Hello

More Related