You are on page 1of 20

Variables

A variable
• named memory location whose value can vary
• Temporary storage of data
• is a symbol which works as a placeholder for
expression or quantities that may vary or change
• symbolic name for (or reference to) information
Can hold different
numeric or alpha
numeric values.
Naming conventions

variables like humans need names to be


identified.

The name of a variable usually consists of


alphanumeric characters and symbols
Naming conventions
• All variables names should begin with letter of the alphabet
or underscore

FitRate First name


Lname 23_Jordan
C150
_firstName
Mak5
Naming conventions
Variable names can range from 1 to 255 characters

maxHealthAvailableForDamageIncrease.897_12

criticalRateMultiplierForStandardNumbers_001
Naming conventions
Do not use reserved words as variable names
Reserved words is a word that cannot be used as an
identifier(name)

if public void catch


true break switch static
import short double byte
Naming conventions
Tips:

Must be descriptive and significant to the data,


problem and its solution

The variable's name represents what information the variable


contains
Data types
Data types
• is a classification that specifies which type of value a variable
has

• is a classification of data which tells the compiler or interpreter


how the programmer intends to use the data

• Type of data being processed in the program


Data types
• Primitive data types are data types that are considered to be
the basic and are predefined in most programming
languages available nowadays

• built into a programming language

• data type provided by a programming language as a basic


building block
Data types [primitive]
Boolean - data type has only two possible values
true and false.
Data type

boolean user = true


boolean user = true
boolean isSingle = false
boolean capsize = true
Variable name
Data types [primitive]
• byte: 8-bit signed integer. Can hold values from -128 to 127.

• short: 16-bit signed integer. Can hold values from -32,768 to 32,767.

• int: a 32-bit signed integer. Can hold values from -2,147,483,648 to


2,147,483,647.

• long: a 64-bit integer. Can hold really big numbers (-2^63 to 2^63–1).
Data types [primitive]

The difference between


all the above is the
capacity & range of
values these data types
can hold in them.
Data types [primitive]
Declaration samples: byte, short, int, long

byte num1 = 24
short myPoint = 345
int userAge = 18
long urLDR = 9999994547
Data types [primitive]
• Float - this data type accepts number with a decimal point. The value
of the float ranges from 1.4013 x 10-45 to 3.4028 x 10+38

• Double - this data type accepts number with a decimal point and its
value ranges from 4.9407 x 10-324 to 1.7977 x 10+308
Data types [primitive]
Precision describes the level of exactness in a number's digits.

54.6 having precision 1 (one decimal digit)

87.65 having precision 2 (two decimal digit)


Data types [primitive]
In general,
float has 7 decimal digits of precision;
double has 15 decimal digits of precision

float a = 1.55
double b = 1.3543
Data types [primitive]
The CHAR data type stores character data in a
fixed-length field.

char data type is used to store a single character.


Char myChar = ‘a’ int urchar = 5
Char urchar = ‘5’
X = urchar + urchar
Data types [primitive]
• Boolean
• Byte
• Short
• Int
• Long
• Float
• Double
• char

You might also like