{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Recitation3 - CS180 Recitation 3 Lecture Overflow byte b b...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS180 Recitation 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out of bounds and wrapped around. Overflow. Example 1 0 0 + 1 1 0 1 0 1 0
Background image of page 2
Lecture: Underflow Similarly underflow can occur. byte b; b = -128; b -= 1; Every type has their own range. Variable types should be chosen wisely.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Data Types * Primitive Data Types Numeric Data Types Integer Numbers byte (8-bits), short (2-bytes) int(4-bytes), long (8-bytes) Real Numbers float (4-bytes), double (8-bytes) Character Data Types char (2-bytes) Logic boolean – Requires 1-bit information (true or false) but its size is not precisely defined. * Reference Data Types Object (varied-sized bytes) String
Background image of page 4
- Why many data types? Small numbers require less bytes for representation. Precision – Some numbers need to be represented precisely for computation. -Type safety in Java Java is strongly typed. The type of every variable must be declared before its use. This allows Java to perform many safety and sanity checks during compilation and avoids many program errors.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Variables The format of variable declaration is data_type variable_name; Variable Declaration: - Variable declaration sets aside memory to hold the variable and attaches a name to that space. - No need to use new operator. Examples: double volume; Memory is allocated to store double values. int num; - Memory is allocated to store integer values.
Background image of page 6
Variable assignment and Initialization * float x = 10.05f; //x is initialized to 10.05.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}