Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.

Similar presentations


Presentation on theme: "CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall."— Presentation transcript:

1 CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall

2 Data in a Computer stored in memory at address locations addresses and data are in binary code letter 'A' stored as 01000001 could be stored in location such as: 10010000 01000101 00101011

3 Program Data Can Be variables the program can change the values e.g., different customers could have different values for customerAge constants will always be the same in program LA_COUNTY_SALES_TAX

4 Data a variable or constant is defined by: name (looks up its location in memory) must start with a letter (not a number) data type e.g., various types of numbers, character, logical, etc. actual data value

5 Naming Java Data can include letters, numbers, and the special characters $ and _ (underscore) must start with a letter convention: use lower case for 1 st letter must not be a Java "reserved word" eg., can't name a variable Class or public

6 Declaring Data declaring is identifying a variable before it is used Java is "strongly typed" must declare all variables with data types declaration is a program statement, so it needs to end with ; int myAge; // type int (integer)

7 Declaring Data - 2 can declare a variable and also assign a value in same statement char middleInitial = 'P'; can declare multiple variables in one statement, if they are same type char firstInitial, middleInitial ;

8 Java "Primitive" Data Types individual items, not combinations numeric integer: byte, short, int, long decimal: float, double logical boolean character char

9 Numeric Data Types integers (whole numbers) byte (–128 to +127) 8 bits (1 byte) short (-32768 to + 32767) 16b (2B) int (± 2 billion) 32b (4 B) long (± 9 quintillion) 64b (8B) # of (decimal) digits = 30% x # of bits

10 Numeric Data Types - 2 floating point (have decimals) e.g., 1.2, 297,765,432.99971) float (7 significant [accurate] digits) 32b can have 38 0s before or after decimal point (contra text p. 92) double (15 significant digits) 64b can have 308 0s before or after decimal point

11 Numeric Data Types - 3 tradeoff: memory vs. reliability declaring reserves memory long data type uses 8 bytes per variable storing a lot of small integers in long variables will waste memory program will crash if try to put a large number in a data type that isn't big enough

12 Arithmetic Operators + addition - subtraction * multiplication / division

13 Arithmetic Operators - 2 integer arithmetic is different: / division 8/5 = 1 % modulus (remainder) 8%5 = 3

14 Numeric Type Conversions if variables in a calculation are of same type, result is same type if add, divide, etc. integers, result is integer for different types, result is type that holds largest number (unifying type) largest to smallest types: double, float, long, int, short, byte

15 Boolean Data Type boolean value is either true or false boolean goodStudent = true comparison operators less, greater than (4<8) ==, != equal, not equal (4!=9) = less or equal, greater or equal boolean ok = (age < weight) ;

16 Character Data Type holds one character ('A', 'b', '3', '@', etc.) must be enclosed in single quotes 1 != '1'

17 Character Data Type - 2 can be an "escape character" escape means don't treat as normal letter '\b' has same effect as backspace key '\n' moves down to next line '\r' moves to start of line '\n' + '\r' is same as hitting Enter key

18 Java Constants value is "hard coded" program can't change the value float BASE_TAX_RATE =.15 "naming convention" is to use all capital letters to make easier to spot in code


Download ppt "CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall."

Similar presentations


Ads by Google