16

Primitive Variables

  • Upload
    nash

  • View
    62

  • Download
    2

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Primitive Variables
Page 2: Primitive Variables

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 NumbersTemperature and WeightNames and Places

Page 3: Primitive Variables

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, &)

Page 4: Primitive Variables

Binary Value (boolean) Numbers

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

Character (char) Text (String) – not a regular primitive variable!

Page 5: Primitive Variables

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.

Calculate28 = 128 + 127 + 1 =

Page 6: Primitive Variables

shortOccupies 16 bits or 2 bytes

intOccupies 32 bits or 4 bytes

longOccupies 64 bits or 8 bytes

Can you guess the range of values each one stores?

Page 7: Primitive Variables

It’s a tradeoffThe 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.

Page 8: Primitive Variables

floatOccupies 32 bits or 4 bytes, with 6 or 7

significant digits.double

Occupies 64 bits or 8 bytes, with 14 or 15 significant digits.

Page 9: Primitive Variables

charContains a standard ASCII character

booleanContains a TRUE/FALSE value

StringContains a line of text

Page 10: Primitive Variables

The only ones that you need to be familiar with for the AP Exam are:

1. int2. double3. boolean4. char5. String

Page 11: Primitive Variables

variable_type variable_name;Example:

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

Page 12: Primitive Variables

Must start with a letter, underscore(_), or dollar sign($)

After 1st letter, you can use numbersCan NOT be a JAVA keywordWhite Space is not permitted (space).No Special CharactersThey are case sensitive so “BOX” is not the

same name as “box”

Page 13: Primitive Variables

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

Page 14: Primitive Variables

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)

Page 15: Primitive Variables

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;

Page 16: Primitive Variables

Variables can be initialized when they are declared:int butterbeerBottles = 99;double bodyTemperature = 98.6;char display = ‘a’;boolean light = false;String insult = “You Strink!”;

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