CSCI 120 Lecture 15 Java Primitive Types Operators

  • Slides: 12
Download presentation
CSCI 120 Lecture 15 Java Primitive Types, Operators, and Strings (Java: An Eventful Approach,

CSCI 120 Lecture 15 Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), 30 October 2012 Slides Credit: Bruce, Danyluk and Murtagh

Primitive Data Type • Can write value as a literal Ex: int a=1732 •

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

Operators vs. Method Invocations • Operators produce new values Ex: if the value of

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

Operators and Precedence Rules • • Arithmetic and logical negations, - and ! Multiplication

Operators and Precedence Rules • • Arithmetic and logical negations, - and ! Multiplication and division: * and / Addition and subtraction: + and – Comparisons: <, <=, >, >= Equality and inequality: == and != And: && Or: || Ex: a+b*c is the same as a+(b*c) but not (a+b)*c

Numeric Types • Integers: Ex: int an. Int=99; • Real numbers: Ex: double a.

Numeric Types • Integers: Ex: int an. Int=99; • Real numbers: Ex: double a. Double=98. 6; An int can be converted into a double without loss of precision, but not vice versa

Dividing int and double The following table summaries the types of result one will

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 int double int result double result 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

How a double is displayed • If you print a double, the output is

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=100000; System. out. print(a); will output 1. 0 E 9

Selecting a Numeric Type • Use int whenever you need whole numbers only. •

Selecting a Numeric Type • Use int whenever you need whole numbers only. • Use double when you need non-integer values • Use int when methods demand it Ex: set. Color( int, int )

Useful Functions on double Math. pow(a, b) Math. exp (b) Math. ln(a) Math. sqrt(a)

Useful Functions on double Math. pow(a, b) Math. exp (b) Math. ln(a) Math. sqrt(a) ab eb ln(a) Square root of a

System. current. Time. Millis() • Used to check elapsed time Ex: printing out the

System. current. Time. Millis() • Used to check elapsed time Ex: printing out the duration of a mouse press public void on. Mouse. Press(Location point){ starting. Time=System. current. Time. Millis(); } public void on. Mouse. Release(Location point){ System. out. println(System. current. Time. Millis()-starting. Time)); }

String • Java uses String to manipulate textual data • Quoted text is of

String • Java uses String to manipulate textual data • Quoted text is of type String • Programs can combine String objects Ex: String a="He"; String b="llo"; System. out. print(a+b); will print out Hello • String accumulators: b = b + " world";

Student To Do’s • HW 09 – – Exercise 4. 9. 2 (Dicey) Exercise

Student To Do’s • HW 09 – – Exercise 4. 9. 2 (Dicey) Exercise 3. 12. 3 (Random Box) Due Monday 11/5 by 1: 25 pm Submit your. java files and bluej project file. • Read Java: An Eventful Approach – Ch. 5 (Today) 12