Variables and Data Types 1 Variable n Definition

Variables and Data Types 1

Variable n Definition n n a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed, and retrieved Using variables n n n establish its data type and initial value (declaration) set/change its value (assignment, input) use/display its value (expressions, output) 2

Example: Using Variables int dimecount; // declaration double dimevalue = 0. 10; // declaration with initial value double totalvalue; dimecount = Input. read. Int(); // input (assignment) totalvalue = dimecount*dimevalue; // assignment System. out. println(totalvalue); // output 3

OUTPUT dimevalue System. out. println(totalvalue); 0. 10 totalvalue 5. 30 dimecount 53 totalvalue = dimecount*dimevalue; INPUT dimecount = Input. read. Int(); 4

Back to Java Program Structure n A Java program (application or applet) is a class that consists of methods n n main(), init(), paint(), action(), setup(), on. Button. Pressed(), … Each method has a body n n delimited by { } consists of a sequence of statements (including declarations) 5

Statements n Declarations int dimecount; double dimevalue = 0. 10; n Assignment statements dimecount = Input. read. Int(); totalvalue = dimecount*dimevalue; n Output statements System. out. println(totalvalue); 6

Identifier n A name in a Java program n n Rules in forming an identifier: n n used for variables, classes, methods, . . . consists of letters, digits, and underscores (_) should start with letter or underscore Examples: ateneo score 5 total_credit Big. Blue _one 4 three x if Some identifiers are reserved words 7

Data Type n n Describes a domain or pool of values Helps a compiler impose rules Programming language needs rules for constructing literals for a given data type n e. g. , 234 is an integer literal, ‘A’ is a character literal, 2. 1 e-3 is a double floating point literal Some primitive data types in Java: n int, char, float, double, boolean 8

Understanding Data Types Important components of a data type: n n n Range of values Literals Possible operations 9

The int Data Type n Range: -2, 147, 483, 648 to 2, 147, 483, 647 n n Literals n n n applies to all system platforms sequence of digits Examples: 22, 16, 1, 426, 0, 12900 Operations: usual arithmetic operations n n +, -, *, /, % negative numbers obtained using - as prefix 10

The double Data Type n Values: decimal numbers n n n Literals (examples) n n Range: 4. 94 e-324 to 1. 80 e+308 precision: n. nnnnn. . . X 10(+/-)mmm 100. 5, 0. 33333, 200000. 0 -8 E 10 (-800000), 2. 1 e-3 (0. 0021) Operations: arithmetic ops (division? ) float: lower precision 11

Constants n Literal values in a program n n n appear often enough and may be associated with an appropriate name declare as a “variable with a fixed value” Examples n n n public static final int MAX = 100; public static final double PI = 3. 1415926; public static final double DIMEVALUE = 0. 10; 12

Input and Output 13

I/O in Java n Text output in Java n n Input in pure Java is not straightforward n n n System. out. print & System. out. println need to handle exception cases uses notions of streams and files Trend in current applications n n perform I/O through visual components GUIs 14

Input. java n n A “home made” class designed to make console input simpler For use in Java applications n n make sure that Input. java is in your working directory use Input. xxx() for text input of ints/doubles Input. read. Int(), Input. read. Double() 15

Input Statements are Assignment Statements Examples: double interest. Rate; . . . int count = Input. read. Int(); 16

Applets n To create applets n need to process GUI events need an init() method to set up visual objects, an action() method to specify associated actions use Input. Output. Applet n n extend Input. Output. Applet instead of Applet define setup() and on. Button. Pressed() 17

Using Input. Output. Applet n n Make sure Input. Output. Applet. class is present in your directory In the setup() method n n add. Input(“name”) to add input objects add. Button(“label”) to add a button add. Output() to add an output area In the on. Button. Pressed method n n get. Int(), get. Double() for retrieving data print(), println() for printing on output area 18

Operators and Expressions 19

Operators in Java n Arithmetic operators n n Special operators n n n +, -, *, /, % (, ) performs grouping = (assignment) Other operators 20

Understanding Operators n Operands n n n Calculation performed n n count (binary/unary) type value (and type) returned Other effects 21

Example: % n n Binary operation Both operands are ints Returns the (int) remainder when left operand is divided by the right operand No additional effects 22

Another Example: = n n Binary operation Left operand must be a variable Returns the value of the right operand Effect: value of right operand is assigned to left operand * Note that a = b = c = 0; is valid 23

Other Operators n Increment and decrement operators n n n Assignment operators n n ++, -post- or pre+=, -=, *=, /=, … “Built-in” Functions n n not really operators (but similar) Math. abs(), Math. sqrt(), Math. pow(), . . . 24

Post-increment Operator: ++ n n n Example: number++ Unary operator Operand must be a variable Returns the (original) value of the operand Effect: variable is incremented 25

Pre-increment Operator: ++ n n n Example: ++number Unary operator Operand must be a variable Returns the incremented value of the operand Effect: variable is incremented 26

About ++ n Notice that a++; and ++a; are virtually equivalent n n n return value is ignored in both cases could be used as shorthands for a = a+1; Distinction apparent when the return value is used a = 5; b = a++; // values of a & b? a = 5; b = ++a; // values of a & b? 27

Decrement Operator: -Analogous definitions for n n Post-decrement Pre-decrement number---number 28

Assignment Operators n There is a shorthand for constructs such as sum = sum + number; sum += number; n += is an operator: n such an operator exists for virtually every arithmetic operator n n n +=, -=, *=. /=, %=, . . . effect: variable is updated returned value: the updated value 29

Built-in Functions n n Provided in Java to provide for more complex operations Example: Math. pow() n n n double result = Math. pow(5. 5, 3. 0); can be viewed as a binary operation that calculates some power of a number javap java. lang. Math n prints a list of available math functions 30

Operand Types vs Result Type n n There are cases where the type of the result differs from the types of the operands Examples n n division between an int and a double returns a double Math. round() has an operand (argument) that is a float but returns an int 31

Expressions n Expression n n Uses n n n sequence of variables, literals, operators, and function calls right operand of an assignment argument for System. out. println() Expression-statement n an expression terminated by a semicolon 32

Strings 33

Variables Revisited n A variable holds a value num int num = 5; n 5 A variable may instead contain a reference String s = “Hello”; s “Hello” 34

String n A special kind of data type n n n called a class allows for string objects About Strings n n n sequences of characters (letters, digits, etc) literals: formed by delimiting the sequence of characters with " " operations? 35

Operations on Strings n Concatenation n n Obtain length of string n n n “Hello” + “ there” equals “Hello there” String s = “Hello”; int len = s. length(); // len = 5 Obtain a substring n n String s = “Hello”; String t = s. substring(0, 3); // t = “Hel” 36
- Slides: 36