Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 2: Data Types, Variables, Operators, and Expressions"— 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, double: 3.1, , char: 'a', 'X', '?', '\n' boolean: true, false Special (Object) Type: String: "Tuesday", "Java", "Hello World!"

4 Numeric Operators Numeric Binary Operators: Numeric Unary Operator:
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 int short try catch final interface static void char finally long strictfp 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 " "Interstate " "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 / / 3 = " + 6 / / 3); System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 ); System.out.println(" * 2 = " * 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 is (1 - 2) - 3 which is -4 But * / % have a higher level of precedence than * 4 is 13 6 + 8 / 2 * 3 * 3 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
\_/ | * 5 % 4 \_/ | % 4 \___/ | \________/ | 1 + 8 % 3 * 2 - 9 \_/ | * 2 - 9 \___/ | \______/ | \_________/ |

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

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 " ); System.out.println("Interstate " ); System.out.println( " Class"); System.out.println("Class " ); }

12 Homework II What String result from the following expressions?
"hello" + 42 is ______________ 1 + "abc" + 2 is ______________ "abc" is ______________ "abc" is ______________ "abc" + 9 * 3 is ______________ "1" + 1 is ______________ "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: / 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.0 / 2.0 \___/ | / 2.0 \_____/ | \____________/ |

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: Syntax
Sets aside memory for storing a value Variables must be declared before they can be used Syntax type name; 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 int x; 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; x 3 x 6 taxRate 0.06

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

19 Printing a Variable’s Value
Using '+', attach a variable’s value to the existing String object double grade = ( ) / 3.0; System.out.println("Your grade was " + grade); int students = ; 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 = ; 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); }


Download ppt "Lecture 2: Data Types, Variables, Operators, and Expressions"

Similar presentations


Ads by Google