CS2_05_Datatypes_Casting

CS2_05_Datatypes_Casting - CS2 Module 5 Category: Elements...

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

View Full Document Right Arrow Icon
CS2 Module 5 Category: Elements of Java Topic: Datatypes Objectives Data typing Casting
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 5 Elements of Java Data Types
Background image of page 2
Data Types Interacting with the real world requires representing a wide variety of different types of data Integer, real and imaginary numbers Differing precision and range depending on application Characters, strings and other textual data True, false values On/Off Stop/Go
Background image of page 3

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

View Full DocumentRight Arrow Icon
Computer Numbers Integers (byte, short, int, long) whole numbers exact Relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often approximations (0.33333) larger magnitude (~10 308 ) 6.023 x 10 23
Background image of page 4
Why so many data types? Performance Smaller data items (byte, short, float) take up less space Integer calculations are faster than floating point calculations Nevertheless, sometimes we need to convert data from one type into another
Background image of page 5

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

View Full DocumentRight Arrow Icon
Type Checking What happens when data is changed from one representation to another? Some languages (such as Java) enforce a strict set of rules to try and catch problems early This may seem annoying at first Later, it will really get under your skin!
Background image of page 6
Problems? Let's say that you have a floating point number in a variable f and you decide to store it in an integer variable i i = f; Any problems possible?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Built-in Data Types Java primitives (whole numbers) byte short int long Java primitives (real numbers) float double
Background image of page 8
Built-in Data Types Java primitives (text) char (character, use single quotes: ‘b’) String * Java primitives (True/False) boolean * String is not a primitive
Background image of page 9

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

View Full DocumentRight Arrow Icon
Public Service Announcement We start by discussing simple primitive data types Later we will discuss user defined data types as part of the Object Oriented portion of the material For now, you should know that Java has a "built-in" type: String We introduce it know as a useful tool but we will come back later and explain more fully
Background image of page 10
Data Type Default Values Primitive Type Default Value boolean false char '\u0000' (null) byte (byte) 0 short (short) 0 int 0 long 0L float 0f double 0d
Background image of page 11

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

View Full DocumentRight Arrow Icon
Variable Declarations Simple form <datatype> <identifier>; Example int total; Optional initialization at declaration <data type> <identifier> = <init value>; Example int total = 0;
Background image of page 12
Examples int counter; int numStudents = 583; float gpa; double batAvg = .406; char gender; char gender = ‘f’; boolean isSafe; boolean isEmpty = true; String personName; String streetName = “North Avenue”;
Background image of page 13

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

View Full DocumentRight Arrow Icon
Primitive Type Facts Type Size Min Default boolean false Max 1 false* true* char '\u0000' (null) 16 byte (byte) 0 8 -128 127 short (short) 0 16 -32,768 32,767 int 0 32 -2,147,483,648 2,147,483,647
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 47

CS2_05_Datatypes_Casting - CS2 Module 5 Category: Elements...

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

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