Recitation3 - CS180 Recitation 3 Lecture: Overflow byte 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 DocumentRight 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 DocumentRight 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 DocumentRight 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. * float x; x = 10.05f; * float x = 10.0f; //float variable x is initialized to 10.0 float y = 20.0f; //float variable y is initialized to 20.0 x = y; //variable assignment. x has value 20.0 Different from Object assignment
Background image of page 7

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

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

This note was uploaded on 02/19/2012 for the course CS 180 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 21

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online