1 / 20

Lecture 2: Data Types, Variables , Operators, and Expressions

Lecture 2: Data Types, Variables , Operators, and Expressions. Dr. Kyung Eun Park Summer 2017. Data Types as Data Categories. Data Categorization: Integer Real number Character String Boolean Data Types: Determine the operators’ behavior

eking
Download Presentation

Lecture 2: Data Types, Variables , Operators, and Expressions

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. Lecture 2: Data Types, Variables, Operators, and Expressions Dr. Kyung Eun Park Summer 2017

  2. Data Types as Data Categories • Data Categorization: • Integer • Real number • Character • String • Boolean • Data Types: Determine the operators’ behavior • / (division) for integer numbers or for real numbers

  3. Primitive Data Types • Primitive Types: • Java supports 8 simple types for numbers, text, etc.: • byte, short, int, long, float, double, char, & boolean • Commonly Used Primitive Types in Java • int: 42, -3, 0, 926394 • double: 3.1, -0.25, 19.83425 • char: 'a', 'X', '?', '\n' • boolean: true, false • Special (Object) Type: • String: "Tuesday", "Java", "Hello World!"

  4. Numeric Operators • Numeric Binary Operators: • Combines two values (operands: left and right) • 3*4, 9/2, 68%4, etc. • + addition • - subtraction (or negation) • * multiplication • / division • % modulus (a.k.a. remainder) • Numeric Unary Operator: • -5, +6

  5. Keywords (Reserved Words) • keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends intshort try catch final interface static void char finally longstrictfp volatile class float native super while const for new switch continue goto package synchronized

  6. Expressions • Simple Expression: • Numeric expressions: 1+1, 3*4, (1+5)*50, 10/3, 9%3 • String expressions: "Hello World!" • Numeric expression plus ('+')String value • String concatenation (2*6)+(4*4)+10 "Hello " + "World!" "Summer " + 2017 "Interstate " + 9 + 5 20 + 20 + "Class"

  7. Program I: Numeric Data Types public class DataTypes { public static void main(String[] args) { System.out.println("5*8/6 = " + 5*8/6); System.out.println("(2*6)+(4*4)+10= " + (2*6)+(4*4)+10); System.out.println("6 / 2 + 7 / 3= " + 6 / 2 + 7 / 3); System.out.println("6 * 7 % 4= " + 6 * 7 % 4 ); System.out.println("22 + 4 * 2= " + 22 + 4 * 2); } }  Wrong answer! Fix this using () : Higher the evaluation precedence

  8. Evaluation Precedence • Precedence: Order in which operators are evaluated. • Generally operators evaluate left-to-right.1 - 2 - 3 is (1 - 2) - 3 which is -4 • But */% have a higher level of precedence than +-1 + 3 * 4 is 13 6 + 8 / 2 * 3 6 + 4 * 3 6 + 12 is 18 • Parentheses can force a certain order of evaluation:(1 + 3) * 4 is 16

  9. Precedence Examples • 1 * 2 + 3 * 5 % 4 • \_/ |2 + 3 * 5 % 4 • \_/ | 2 + 15 % 4 • \___/ | 2 + 3 • \________/ | 5 • 1 + 8 % 3 * 2 - 9 • \_/ |1 + 2 * 2 - 9 • \___/ |1 + 4 - 9 • \______/ |5 - 9 • \_________/ | -4

  10. Homework I • What values result from the following expressions? 9 / 5 695 % 20 7 + 6 * 5 7 * 6 + 5 248 % 100 / 5 6 * 3 - 9 / 4 (5 - 7) * 4 6 + (18 % (17 - 12))

  11. Program II: Evaluation public class Evaluation { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("Hello " + "World!"); System.out.println("Summer " + 2017); System.out.println("Interstate " + 9 + 5); System.out.println(20 + 20 + " Class"); System.out.println("Class " + 20 + 20); } }

  12. Homework II • What String result from the following expressions? "hello" + 42 is ______________ 1 + "abc" + 2 is ______________ "abc" + 1 + 2is ______________ 1 + 2 + "abc" is ______________ "abc" + 9 * 3 is ______________ "1" + 1 is ______________ 4 - 1 + "abc" is ______________

  13. Real Number • Examples: 6.022 , -42.0 , 2.143e17 • Placing .0 or . after an integer makes it a double • The operators +-*/%() all still work with double. • / produces an exact answer: 15.0 / 2.0 is 7.5 • Precedence is the same: () before */% before +-

  14. Real Number Examples • 2.0 * 2.4 + 2.25 * 4.0 / 2.0 • \___/ |4.8 + 2.25 * 4.0 / 2.0 • \___/ | 4.8 + 9.0 / 2.0 • \_____/ | 4.8 + 4.5 • \____________/ | 9.3

  15. Where to store values? • A Named Container for Rewritable Values: variable!!! • Variables are used to hold different types of values: • Integer number: int container (variable) • Real number: double or real container (variable) • Character symbol: char container (variable) • Boolean logical value: boolean container (variable) • String text: String container (object)

  16. Variable Declaration • Variable declaration: • Sets aside memory for storing a value • Variables must be declaredbefore they can be used • Syntax typename; • The name is an identifier. • An identifier name must start with a letter or _ (underscore) or $ • Construct using letters [A-Za-z], digits [0-9], and ‘_’ and ‘$’ symbols intx; double taxRate;

  17. Assignment • Assignment: Stores a value into a variable. • Syntax name = expression; int x;x = 3; x = x + 3; double taxRate;taxRate= 0.06;

  18. Declaration/initialization • A variable can be declared/initialized in one statement. • Syntax type name= expression; intx = 3; double taxRate= 0.06;

  19. Printing a Variable’s Value • Using '+', attach a variable’s value to the existing String object double grade = (95.1 + 71.9 + 82.6) / 3.0; System.out.println("Your grade was " + grade); int students = 11 + 17 + 4 + 19 + 14; System.out.println("There are " + students + " students in the course.");

  20. Program III: Receipt public class Receipt { public static void main(String[] args) { // Calculate total owed, assuming 6% tax / 15% tip int subtotal = 38 + 40 + 30; double tax = subtotal * .06; double tip = subtotal * .15; double total = subtotal + tax + tip; System.out.println("Subtotal: " + subtotal); System.out.println("Tax: " + tax); System.out.println("Tip: " + tip); System.out.println("Total: " + total); } }

More Related