# references to objects

Λογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

69 εμφανίσεις

Data Types, Strings, and
Calculations, Oh My!

Chapter 2, Day 1

AP Computer Science A

Conard High School

The Plan

Strings

a reminder

Data Types

what are they and why do
we care?

Calculations

when primitive data types
act, well, primitive.

Data Types (Primitives)

What are they? Just ways to store
information in Java.

Primitive Data Types we care about:

int

double

boolean

Why are some data types called
“primitive”?

Data Types (Non
-
Primitives)

Non
-
primitive data types are
references
to objects

Examples of non
-
primitive data types:

String

Integer

ArrayList

Random

JFrame

int dozen = 12;

//An Example

The Good:

They are given a memory location.

Meaning is easy to picture.

Not referring to this memory location. The name of the
memory location
IS
dozen.

But Caution…

You have 32 bits to make an int. 1 of these is the
sign. So 4,294,967,296 possibilities. Why?
_______________

Why then does the book say that you can enter int
values from
-
2147483648 to 2147483648?

12

dozen

int double gasPrice = 3.98;

//An Example

The Good:

More memory (stored with 64 bits).

Meaning is easy to picture b/c still a primitive.

Just like int, gasPrice is not referring to this memory
location. The name of the memory location
IS
gasPrice.

But Caution…

You have 64 bits to make a double. Same idea as int.

So you are limited to doubles from
-
1.7x10
308

to
1.7x10
308

3.98

gasPrice

Calculations

Java Weirdness

Can you ever enter 1/3 accurately?

What about sqrt(2) or pi?

Calculations

Java Weirdness

Errors happen if you do not pay attention to
“caution” parts of the last few slides.

OVERFLOW error

trying to store a number that is
too large for data type

UNDERFLOW error
-

_________________________________________
_________________________________________

ROUNDING ERROR

fractional part of double or
decimal of integer is rounded or truncated

Calculations

Java Weirdness

Assignments happen right to left

Int truncates

If you do not have a decimal point and a number, it is an int, not a
double.

double x = 3;

double y = 3.0;

You can cast variables to change them. See above.

(int) y is a _______________

(double) x is a _____________

There are FIVE operations

+

-

*

/

%

The ORDER OF OPERATIONS is crucial

MY PROBLEM WITH AUNT SALLY