primitive variables what’s a variable? a quantity or function that may assume any given value or...

19
Primitive Variables

Upload: eustace-victor-carson

Post on 13-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

Primitive Variables

What’s a Variable? A quantity or function that may

assume any given value or set of values (source – Dictionary.com)

What types of values have we assigned to variables in mathematics and science? Integers and Real Numbers Temperature and Weight Names and Places

What’s a Primitive? Being the first or earliest of the

kind or in existence Given just one group of binary

digits, what types of things could we come up with? A Binary Value (True/False) Numbers (1, 4, 21) Characters (A, k, &)

JAVA’s Primitive Variables Binary Value (boolean) Numbers

Integer (byte, short, int, long) Real Numbers (float, double)

Characters (char)

Why so many Integer names? Each one consists of a different

number of bits. For example, a byte consists of 8

bits and holds a value from –128 to 127.

Calculate 28 = 128 + 127 + 1 =

The other Integer Variables short

Occupies 16 bits or 2 bytes int

Occupies 32 bits or 4 bytes long

Occupies 64 bits or 8 bytes Can you guess the range of values

each one stores?

Why so many? It’s a tradeoff The greater number of bytes each

one stores, the greater the range of values it can represent.

But the larger number of bytes used put more demand on memory resources.

Real Numbers float

Occupies 32 bits or 4 bytes, with 6 or 7 significant digits.

double Occupies 64 bits or 8 bytes, with 14

or 15 significant digits.

Characters & Booleans char

Contains a standard ASCII character boolean

Contains a TRUE/FALSE value

Declaration variable_type variable_name; Example:

int x; double temperature; char initial; boolean light;

Naming Rules Must start with a letter, underscore(_),

or dollar sign($) After 1st letter, you can use numbers Can NOT be a JAVA keyword White Space is not permitted (space). No Special Characters They are case sensitive so “BOX” is

not the same name as “box”

JAVA keywords

abstract continue for new switchassert default goto package synchronizedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transientcatch extends int short trychar final interface static voidclass finally long strictfp volatileconst float native super while

Naming Convention The first letter of the variable is

lower case. When using more than one word in

the variable name, the first letter of each word after the first is capitalized.

Be descriptive (to a limit)

Example If we wanted to have a double that

stores the room’s temperature: The Gooddouble roomTemperature;

The Baddouble ZED;double

theTemperatureInOurComputerRoomOnSeptemberTheTenth;

The Ugly (Illegal)double case;double room Temperature;double 9/10/07Temperature;

Initialization Variables can be initialized when they

are declared: int butterbeerBottles = 99; double bodyTemperature = 98.6; char display = ‘a’; boolean light = false;

A variable can be turned into a constant by adding the word final at the beginning of the declaration.

Unary Operators + Unary plus operator; indicates positive

value (numbers are positive without this, however)

- Unary minus operator; negates an expression

++ Increment operator; increments a value by 1

-- Decrement operator; decrements a value by 1

! Logical compliment operator; inverts the value of a boolean

Operators Assignment operator

= Simple Assignment operator Arithmetic Operators

+ Additive operator - Subtraction operator * Multiplication operator / Division operator % Remainder operator

Order of Operations Order is determined by the

precedence assigned to the operators in use within the expression. Example: 4 + 5 * 9 evaluates to 49

Still always use parenthesis!!!!…. even if it feels redundant.

Online resourceshttp://java.sun.com/docs/books/tutorial/java/

index.html