a variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102;...

15

Upload: monica-byrd

Post on 14-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done
Page 2: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

A variable is a storage locationfor some type of value.

days

102

taxrate

7.75

int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done = true;

Page 3: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

What is a data type?

Page 4: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

A data type describes what type of data the variable will hold.

There are 8 basic primitive data types

What is a data type?

Page 5: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

byte short intlong float doublechar boolean

int num = 9;double total = 3.4;

Page 6: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

TYPE Holds RANGE

int integers (32 bit)

-2 billion to 2 billion

int num = 3;

double decimals/floating number(64 bits)

-1.7E+308 to 1.7E+308

double num1 = 7.4;

boolean {true, false} Holds two values: {true, false}

boolean done = false;

char One character

‘A’

Page 7: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

Declarations

A declaration is the introduction of a new name in a program.

All variables must declared in advance

General forms to declare variable. Data type and variable name

int num; double digit; boolean done;

Page 8: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

How do you name variables when defining them?

Page 9: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

When naming a variable follow these rules and conventions:

1. The variable name must contain NO spaces.

2. The variable name must begin with a lower case letter.

3. If you run two words together, then the first letter of the second word should begin with a capital letter.

4. The remainder of the variable name contains only letters and digits.

Can use _ $ as symbols and can start with _ but not recommended.

IDENTIFIER variables are identifers

Page 10: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

Below are some examples of valid variable names:

number sum32 testAverage areaOfTrapezoid

and below are some examples of invalid variable names:

2ndValue test Average question#2

Page 11: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

Java is case sensitive.

Brandon does not equal brandon.

Brandon != brandon

Java is case sensitive.(Yes, it’s so important that I said it twice!)

Page 12: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

Keywords are reserved words that the language uses for a specific purpose. You cannot use keywordsas identifier names.

abstract continue for new switch

assert*** default goto* package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum**** instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp** volatile

const* float native super while

Page 13: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done
Page 14: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

assignment operator (=). Called initializing the variable.

int num = 1000;       // This line declares num as an int variable which holds value "1000".boolean bol = true;      // This line declares bol as boolean variable which is set to the value "true".      boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000;

Statements: Statements in Java end with a semicolon ;

Page 15: A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done

OOP: Basic Parts of Java Primitive Data Types, Example

// create some integersint x, int y; // declared but not initializedx = 1234; y = 3; //initialized and ready to use

double v = 3.14, w = 5.5; //declare and Initialize at same time