1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name...

36
1 Variables and Data Types

Transcript of 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name...

Page 1: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

1

Variables and Data Types

Page 2: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

2

Variable Definition

a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed, and retrieved

Using variables establish its data type and initial value

(declaration) set/change its value (assignment, input) use/display its value (expressions, output)

Page 3: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

3

Example:Using Variables

int dimecount; // declarationdouble dimevalue = 0.10; // declaration with initial

valuedouble totalvalue;

dimecount = Input.readInt(); // input (assignment)totalvalue = dimecount*dimevalue; // assignmentSystem.out.println(totalvalue); // output

Page 4: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

4

0.10

5.30

53

dimevalue

dimecount

totalvalue

dimecount = Input.readInt();

totalvalue = dimecount*dimevalue;

INPUT

OUTPUT

System.out.println(totalvalue);

Page 5: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

5

Back toJava Program Structure A Java program (application or

applet) is a class that consists of methods main(), init(), paint(), action(), setup(),

onButtonPressed(), … Each method has a body

delimited by { } consists of a sequence of statements

(including declarations)

Page 6: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

6

Statements Declarations

int dimecount;double dimevalue = 0.10;

Assignment statementsdimecount = Input.readInt();totalvalue = dimecount*dimevalue;

Output statementsSystem.out.println(totalvalue);

Page 7: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

7

Identifier

A name in a Java program used for variables, classes, methods, ...

Rules in forming an identifier: consists of letters, digits, and underscores

(_) should start with letter or underscore

Examples: ateneo score5 total_credit BigBlue _one4three x if

Some identifiers are reserved words

Page 8: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

8

Data Type Describes a domain or pool of values Helps a compiler impose rules Programming language needs rules for

constructing literals for a given data type e.g., 234 is an integer literal, ‘A’ is a

character literal, 2.1e-3 is a double floating point literal

Some primitive data types in Java: int, char, float, double, boolean

Page 9: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

9

Understanding Data Types

Important components of a data type:

Range of values Literals Possible operations

Page 10: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

10

The int Data Type Range: -2,147,483,648 to

2,147,483,647 applies to all system platforms

Literals sequence of digits Examples: 22, 16, 1, 426, 0, 12900

Operations: usual arithmetic operations +, -, *, /, % negative numbers obtained using - as prefix

Page 11: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

11

The double Data Type Values: decimal numbers

Range: 4.94e-324 to 1.80e+308 precision: n.nnnnn... X 10(+/-)mmm

Literals (examples) 100.5, 0.33333, 200000.0 -8E10 (-80000000000), 2.1e-3 (0.0021)

Operations: arithmetic ops (division?) float: lower precision

Page 12: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

12

Constants Literal values in a program

appear often enough and may be associated with an appropriate name

declare as a “variable with a fixed value” Examples

public static final int MAX = 100; public static final double PI = 3.1415926; public static final double DIMEVALUE =

0.10;

Page 13: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

13

Input and Output

Page 14: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

14

I/O in Java Text output in Java

System.out.print & System.out.println Input in pure Java is not straightforward

need to handle exception cases uses notions of streams and files

Trend in current applications perform I/O through visual components GUIs

Page 15: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

15

Input.java A “home made” class designed to

make console input simpler For use in Java applications

make sure that Input.java is in your working directory

use Input.xxx() for text input of ints/doublesInput.readInt(), Input.readDouble()

Page 16: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

16

Input Statements areAssignment Statements

Examples:

double interestRate;...int count = Input.readInt();

Page 17: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

17

Applets To create applets

need to process GUI events need an init() method to set up visual

objects, an action() method to specify associated actions

use InputOutputApplet extend InputOutputApplet instead of

Applet define setup() and onButtonPressed()

Page 18: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

18

Using InputOutputApplet Make sure InputOutputApplet.class is

present in your directory In the setup() method

addInput(“name”) to add input objects addButton(“label”) to add a button addOutput() to add an output area

In the onButtonPressed method getInt(), getDouble() for retrieving data print(), println() for printing on output area

Page 19: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

19

Operators and Expressions

Page 20: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

20

Operators in Java Arithmetic operators

+, -, *, /, % Special operators

(, ) performs grouping = (assignment)

Other operators

Page 21: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

21

Understanding Operators Operands

count (binary/unary) type

Calculation performed value (and type) returned

Other effects

Page 22: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

22

Example: % Binary operation Both operands are ints Returns the (int) remainder when

left operand is divided by the right operand

No additional effects

Page 23: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

23

Another Example: = 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

Page 24: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

24

Other Operators Increment and decrement operators

++, -- post- or pre-

Assignment operators +=, -=, *=, /=, …

“Built-in” Functions not really operators (but similar) Math.abs(), Math.sqrt(), Math.pow(), ...

Page 25: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

25

Post-increment Operator: ++ Example: number++ Unary operator Operand must be a variable Returns the (original) value of the

operand Effect: variable is incremented

Page 26: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

26

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

the operand Effect: variable is incremented

Page 27: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

27

About ++ Notice that a++; and ++a; are

virtually equivalent 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; a = 5;b = a++; b = ++a;// values of a & b? // values of a & b?

Page 28: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

28

Decrement Operator: --

Analogous definitions for

Post-decrement number-- Pre-decrement --number

Page 29: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

29

Assignment Operators There is a shorthand for constructs

such as sum = sum + number;

sum += number;

+= is an operator: such an operator exists for virtually

every arithmetic operator +=, -=, *=. /=, %=, ...

effect: variable is updated returned value: the updated value

Page 30: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

30

Built-in Functions Provided in Java to provide for more

complex operations Example: Math.pow()

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

prints a list of available math functions

Page 31: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

31

Operand Types vsResult Type There are cases where the type of

the result differs from the types of the operands

Examples division between an int and a double

returns a double Math.round() has an operand

(argument) that is a float but returns an int

Page 32: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

32

Expressions Expression

sequence of variables, literals, operators, and function calls

Uses right operand of an assignment argument for System.out.println()

Expression-statement an expression terminated by a semicolon

Page 33: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

33

Strings

Page 34: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

34

Variables Revisited A variable holds a value

A variable may instead contain a reference

5

“Hello”

int num = 5;

num

String s = “Hello”;

s

Page 35: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

35

String A special kind of data type

called a class allows for string objects

About Strings sequences of characters (letters, digits,

etc) literals: formed by delimiting the

sequence of characters with " " operations?

Page 36: 1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

36

Operations on Strings Concatenation

“Hello” + “ there” equals “Hello there” Obtain length of string

String s = “Hello”; int len = s.length(); // len = 5

Obtain a substring String s = “Hello”; String t = s.substring(0,3); // t = “Hel”