data types primitives. d ata t ype can you ‘add’ apples and oranges? all data has a ‘type’...

45
Data Types Primitives

Upload: everett-nathan-watson

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Data Types

Primitives

Page 2: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Data Type Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’

A data type is defined by it’s Range of values Supported operations

Data types Some data is considered ‘basic’ or ‘primitive’ Some data is considered ‘complex’ (objects) Complex data is an aggregation of primitives and

other complex data.

There are exactly 8 primitive data types

+ = ?

Page 3: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Primitive data typesRange of values

Type What it isMemory Footprint

Values

byte Integer Number 1 byte -128 to 127

short Integer Number 2 bytes -32768 to 32767

int Integer Number 4 bytes -2,147,483,648 to 2,147,483,647

long Integer Number 8 bytes-9,223,372,036,854,775,808 to

9,223,374,036,854,775,808

float Real Number 4 bytes+/- 3.4028… x 10+38 to

+/- 1.4023… x 0-45

double Real Number 8 bytes+/- 1.767… x 10+308 to

+/- 4.940… x 0-324

char Single character 2 bytes Any keyboard character (0 to 65535)

boolean Boolean 1 byte {true, false}

Page 4: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Int primitive typeSyntax

Values are denoted by a single digit, followed by one or more digits optionally preceded by a ‘+ ’ or ‘-’.

These values are called ‘literals’.

+-

digit

3.0+3-1033525835299

3.0+3-1033525835299

Page 5: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Double primitive typeSyntax

Values are denoted by zero-or-more digits followed by a ‘.’ followed by zero-or-more digits. There must be at least one digit in the number. The whole is optionally preceded by a ‘+ ’ or ‘-’.

3.+0.3-1033.525835299.

3.+0.3-1033.525835299.

Page 6: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Int Primitivte typeOperations Each data type supports operations An operation has

Notation: prefix / postfix / infix Arity: binary / unary Precedence (later)

Operations on ‘int’ type (partial)

Description Notation Arity Example

Negation - Unary -3

Addition + Binary 3+12

Subtraction - Binary 3-12

Multiplication * Binary 3*12

Division / Binary 3/12

Remainder % Binary 3%12

Page 7: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Double Primitivte typeOperations

Operations on ‘double’ type (partial)

Description Notation Arity Example

Negation - Unary -3.5

Addition + Binary 3.1+12.5

Subtraction - Binary 3.1-12.1

Multiplication * Binary 0.3*1235.5

Division / Binary 36./12.

Page 8: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Another Java program

Write a program to compute the area of a circle if you are given the radius of the circle.

Develop an algorithm: Read the radius Compute the area Display the area

Page 9: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

SYNTAX OF A ‘PROGRAM’ CLASS Recall the syntax of a ‘program class’

What should our name be? What should the statement sequence be?

StatementSequence

public class ClassName {

public static void main(String[] args) {

}

}

Page 10: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

OUR ‘PROGRAM’ CLASS

// Author: Kenny// Purpose: Compute the area of a circle given radiuspublic class AreaOfCircle {

public static void main(String[] args) { // 1: Read in the radius // 2: Compute the area // 3: Display the area }}

Page 11: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Step 1: Reading the radius

Must be able to get a number from somewhere Use the Keyboard class (described next slide)

Must be able to store the radius in memory Variables are used to store data All variables have a name and, of course, a type The name really stands for a memory location There are two data in this problem:

Radius – what type of thing is this? Area – what type of thing is this?

Page 12: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Keyboard class

We will use the Keyboard class to read data A Keyboard can be used to

readInt() readDouble() readString()

Examples Keyboard.readInt() Keyboard.readDouble() Keyboard.readString()

Page 13: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

OUR ‘PROGRAM’ CLASS

// Author: Kenny// Purpose: Compute the area of a circle given radiuspublic class AreaOfCircle {

public static void main(String[] args) { // 1: Read in the radius double radius; double area; radius = Keyboard.readDouble();

// 2: Compute the area // 3: Display the area }}

Page 14: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Variable declaration Statement

All variables must be declared prior to use. A declaration provides The name of the variable The type of the variable

Why declare? Computer allocates enough memory for storage Computer knows what operations can be applied to the

variable.

SYNTAX: dataType variableName;SYNTAX: dataType variableName;

Page 15: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Assignment statement

Variables are given values by an assignment

SYNTAX: variableName = value;SYNTAX: variableName = value;

Semantics: The name is bound to a value (a.k.a. binding) A name can be bound to no more than one value The binding may change as the program runs

Additional Rules: The value on the right must have the same type as

the variable on the left. The value on the right might be a complex

expression. The ‘= ‘ symbol does NOT mean ‘is equivalent to’

Page 16: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Examples

double x;int y;int z;

x = 3.0;y = 5;z = 2;x = 3.0 + 12.5;y = 12 / 20;y = 10 + z;y = 10 + y;z = 30.5;

Page 17: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Declaring and Assigning Shortcut

Declaration must occur before assignment (or any other use)

Declaration and assignment can be combined in one step

SYNTAX: dataType variableName = value;SYNTAX: dataType variableName = value;

double x = 3.5;

int y = 15;

double x = 3.5;

int y = 15;

double x;

int y;

x = 3.5;

y = 15;

double x;

int y;

x = 3.5;

y = 15;

Page 18: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

OUR ‘PROGRAM’ CLASS

// Author: Kenny// Purpose: Compute the area of a circle given radiuspublic class AreaOfCircle {

public static void main(String[] args) { double radius; double area; radius = Keyboard.readDouble();

// 2: Compute the area // ????? // 3: Display the area }}

Page 19: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

// Author: Kenny// Purpose: …public class AreaOfCircle {public static void main(String[] args) {

double radius; double area; radius = Keyboard.readDouble(); area = radius * radius * 3.141592; System.out.println(area); }}

OUR ‘PROGRAM’ CLASS

Page 20: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

// Author: Kenny// Purpose: …public class AreaOfCircle {

public static void main(String[] args) { double radius; double area; System.out.print(“Enter radius: “); radius = Keyboard.readDouble(); area = radius * radius * 3.141592; System.out.print(“The area of the circle is

“); System.out.println(area); }}

A BETTER ‘PROGRAM’ CLASS

Page 21: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsOperator Precedence

What is the value of the following expression?7 + 3 * 2 – 1

Could be (((7+3)*2)-1) (7+(3*(2-1))) ((7+(3*2))-1)

Operator precedence refers to the order in which operators are evaluated in an expression involving multiple operators.

I always go first

Page 22: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsOperator Precedence

Operator Precedence Table (Partial)

Description Operatorcasting (…)

postfix ++ --

prefix ++ -- + - ~ !

multiplicative * / %

additive + -

relational < > <= >=

equality == !=

logical AND &&

logical OR ||

assignment = += -= *= /= %= &= ^= |=

To evaluate an expression, choose the operator of highest precedence and apply it. If two more operators have the highest precedence, select the left-most one. Repeat until there are no more operators.

Page 23: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsOperator Precedence

To evaluate an expression Choose the operator of highest precedence and

apply it. If two more operators have the highest precedence, select the left-most one.

Repeat until there are no more operators.

3 + 10 * 3 % 5 / 2 – 5 / 10 - 2

Parenthesis can be used to affect precedence Parenthesized parts have ‘high’ precedence

3 + 10 * ((3 % 5) / 2 – 5) / (10 – 2)

Page 24: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Int Primitivte typeMore Operations Shortcut assignment operators do two things!

Shortcut Assignments

Description Notation Example Equivalent

Subtract and assign -= x -= 3; x = x-3;

Add and assign += x+=3; x=x+3;

Multiply and assign *= x*=3; x=x*3;

Divide and assign /= x/=3; x=x/3;

Remainder and assign %= x%=3; x=x%3;

Add one and assign ++ x++; x=x+1;

Subtract one and assign -- x--; x=x-1;

Page 25: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Shortcut Assignment operators

int x = 3;

int y = 5;

int z = x++;

int z = ++x;

int z = ++x + y++;

int z = y++ + ++y;

Don’t ever do this. It’s too silly!

Don’t ever do this. It’s too silly!

Page 26: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsMixed Type

Is the following expression valid?3.5 + 5 / 3

What is the type? What is the value? All math operators require operands to

be of the same type. Since mixed type is common – Java automatically

converts the ‘type’ of an operand to allow the operation to occur.

Page 27: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsMixed Type

The numeric primitives are ordered with respect to width.

bytebyte shortshort intint longlong floatfloat doubledouble

Coercion: when an expression’s type is automatically widened to allow an operation to occur.

Narrowing: can be done by the programmer. Never done automatically.

Page 28: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsMixed Type

When an operator is applied to operands of different type The narrower type is coerced to the wider type if

it will make the operation valid The operator is then applied

3.5 + 5 / 3

5 + 9.0 * 2 / 6

Page 29: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Type casting Type casting is when a programmer explicitly changes the type

of an expression. Almost always a narrowing. Narrowing a real value to an integer means truncation (not rounding) Narrowing is a unary prefix operator with highest precedence

int x = 3.5 + 18.5;

SYNTAX: (newType) expressionSYNTAX: (newType) expression

int x = Keyboard.readInt(); int y = Keyboard.readInt();double z = x / y;

Page 30: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Case Study Write a program where the user enters a double that represents a

monetary value in dollars. Your program will report how to break this amount into change. Specifically, it will report the dollar bills quarters Dimes Nickels And pennies this amount represents.

Concept: Given an amount NHow many dollars?

How many quarters remain?How many dimes remain?How many nickels remain?How many cents remain?

Concept: Given an amount NHow many dollars?

How many quarters remain?How many dimes remain?How many nickels remain?How many cents remain?

Page 31: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Text (chars)

A char is the basic building block of text. A single keyboard character

SYNTAX: a single keystroke enclosed in single quotesSYNTAX: a single keystroke enclosed in single quotes

‘a’ ‘3’ ‘K’ ‘k’ ‘hello’ ‘?’ x

Page 32: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Text (chars)

How would you type in the following chars? A backspace A tab An enter A single quote

Some chars are two-key sequences The ‘\’ character is an ‘escape’ key Precedes some characters meaning they are

‘special’

Char description Syntax

backspace ‘\b’

tab ‘\t’

enter ‘\n’

single quote ‘\’’

double quote ‘\”’

backslash ‘\\’

Page 33: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Text (chars)

Although chars do support some operations, we will for now ignore them. Assignment is the only thing we will use with chars.

char x = ‘a’;

char z = x;

Page 34: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Logic (boolean)

Only two logical values true false

Boolean operators include Conjunction (meaning and) Disjunction (meaning or) Negation (meaning not)

Description Operator Properties

Conjunction (and) && Binary infix

Disjunction (or) || Binary infix

Negation (not) ! Unary prefix

Page 35: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Math vs. Java notation

English Math Java

TRUE 1 true

FALSE 0 false

AND &&

OR ||

NOT !

Page 36: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Truth Tables

A truth table shows every possible combination of inputs to an expression and gives the result

P (Q R)

0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1

P Q R

00010001

00011111

Page 37: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Truth tables Two boolean expressions are equivalent (not identical) if they have the

same truth table.

P Q R P (Q R)

0 0 0 0

0 0 1 1

0 1 0 1

0 1 1 1

1 0 0 0

1 0 1 0

1 1 0 0

1 1 1 0

P Q R (P ( Q R))

0 0 0 0

0 0 1 1

0 1 0 1

0 1 1 1

1 0 0 0

1 0 1 0

1 1 0 0

1 1 1 0

Indication of equivalent expressions

Page 38: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

DEMORGAN’S LAW Any logical binary expression remains

unchanged ifChange all variables to their complements. Change all AND operations to ORs. Change all OR operations to ANDs. Take the complement of the entire

expression.

(( A B) C)

Page 39: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Boolean examplesboolean isDry, isBig, hasMoney, isWeird;isDry = true;isBig = !isDry;hasMoney = isDry || isBig;isWeird = (!(isDry && !isBig) && hasMoney);

boolean isSmoker, isHeavy, isAtRisk;// assume that isSmoker and isHeavy are initialized// write a statement saying that isAtRisk is true if // isSmoker and isHeavy are true

isAtRisk = isSmoker && isHeavy;

boolean isSmoker, isHeavy, isActive, isAtRisk;

// assume that isSmoker, isHeavy, and isActive are initialized// write a statement that isAtRisk is true if the person is// (either a smoker or heavy) AND they are inactiveisAtRisk = (isSmoker || isHeavy) && !isActive;

Page 40: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Logic (boolean)

Relational operators Operate on numeric primitives and produce a boolean All relational operators are binary infix

Description Operator

Less than <

Less than or equal to <=

equal to ==

not equal to !=

Greater than or equal to

>=

Greater than >

Page 41: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

ExpressionsOperator Precedence (revisited)

Operator Precedence Table (Partial)

Description Operatorcasting (…)

postfix ++ --

prefix ++ -- + - ~ !

multiplicative * / %

additive + -

relational < > <= >=

equality == !=

logical AND &&

logical OR ||

assignment = += -= *= /= %= &= ^= |=

Page 42: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Logic (boolean)

3 < 5

3 == 3

5 >= 10

3 != 3

3 < 5 && 8 < 10

3 < 5 < 12

15 / 2 % 3 < 3 * 5 / 4

15 / 2 < 10 || 5 > 8 || 9 % 2 < 5

Page 43: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Case Study Write a program that surveys a user and determines if

they have a health risk. Obtain the following input

height (two integer numbers: one for feet and inches) weight (an integer number in pounds) average number of packs they smoke daily (double) age (an integer number)

Define BMI as (weight (lbs)* 703 / height (inches) squared)

Person is at risk in any of the following cases Their BMI exceeds 30 They smoke more than .5 pack per day They are over 50 years of age and their BMI exceeds 20

Page 44: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Short Circuit Evaluation

When applying conjunction The left-hand operand is evaluated If the value is false then output false If the value is true then evaluate the right-hand

operand and output it’s value

When applying disjunction The left-hand operand is evaluated If the value is true then output true If the value is false, then evaluate the right-hand

operand and output it’s value

Page 45: Data Types Primitives. D ATA T YPE Can you ‘add’ apples and oranges? All data has a ‘type’ that determines what it ‘is’ A data type is defined by it’s

Short circuit examples

x != 0 && 10 / x

y < 10 || y++