You are on page 1of 7

AP Computer Science

Lesson 1 Types and Variables

Types
Integers (int): a fundamental type used to define numeric variables holding whole numbers
-n,,-3,-2,-1,0,1,2,3,,n where n=-1+2^31 int i=0; Typically Counting Variables

Types ctd.
Doubles: a double precision floating point number. (ignore formal definition)
Any value with a decimal Ex: 0.1, 3.14, 2., etc. double e=2.718;

Int vs Double
int Used to count Answers How many? Ex:
Number of People Number of Items in a List Number of Steps to Solve a Puzzle

double Used to measure Answers How Much? Ex:


Balance of Bank Account Weight, Mass, Volume Grade In Class

Types ctd.
Boolean: used to hold values of true or false
Most basic data type Typically the result of a comparison or test Ex: int a = 0; int b = 3; boolean c = b>a;
Here, true is assigned to c

Final Variables
Final Variables: user-defined constants that do not change after assignment
Variables that you want fixed throughout the program Typically in all caps with underscores for spaces Ex: final double TAX_RATE = .075; final double MONTHLY_FEE = 20.00; final int MIN_CLASS_SIZE = 15;

Assignment
Assign values to variables using equal sign
int x=0; double rate = .05;

Shortcuts:
q= q + 1 is the same as q+=1 same for all other operators Incrementing/decrementing is i++ or j- Same as i=i+1 and j=j-1 respectively

You might also like