JAVA PRIMITIVE TYPES
DECLARATIONS Unit 2 Day 1
ASSIGNMENTS
EXPRESSIONS
THINKABOUT
Where have you seen
variables being used in
our programs to date?
VARIABLE EXAMPLE
for(int x = 0; x < 5; x = x + 1)
{
sally.move();
}
x is an integer variable.
What IS an integer, anyway?
Natural numbers (1, 2, 3, …), their
opposites (negatives), and zero.
VARIABLES
A variable is a storage location which contains a known or
unknown value, such as a number or a letter.
A variable’s value can change throughout the course of a
program.
A variable can be thought of as a “container” that holds a
value.
VARIABLES, CONT’D
A variable’s identifier is the name of the variable.
A variable’s type determines what kind of value
the variable can hold.
Consider:
int x = 5;
The variable’s identifier is x.
The variable’s type is int.
The variable’s value is 5.
JAVA T YPES
Class Type
(Later!!)
Type
Primitive
Type
PRIMITIVE T YPES IN JAVA
Primitive types are the most basic data
types available in Java.
They are “indecomposable” – this means
that they are unable to be broken down
into any “simpler” types.
Java has eight primitive types. Six are
numerical.
INTEGER PRIMATIVES
byte
(8-bit)
-128 to +127
short
(16-bit)
-32768 to +32769
int (32-bit) Use int
-231 to 231 – 1 (~2 billion)
long (64-bit)
-263 to 263 – 1 (~19 digits)
FLOATING-POINT PRIMITIVES
A floating-point value is a limited-precision
rational number (decimal number).
Rational number – a number that can be
represented as a quotient of two integers.
float
(32-bit)
double (64-bit) Use double
OTHER PRIMITIVES
char stores a single character
Pronounced as in “charcoal”
boolean stores a value of true or
false
DECLARING PRIMITIVES
A declaration must contain both the type and the identifier.
int x;
Declares x as an int.
double myVal;
Declares myVal as a double.
A single declaration can declare two or more variables of the
same type!
int something, somethingElse;
Declares something and somethingElse as ints.
double earth, wind, fire;
Declares earth, wind and fire as doubles.
RULES FOR IDENTIFIERS
Can contain:
Letters
Digits 0 – 9
First character CANNOT be a digit, though
The underscore _
“No Limit” on name length – best practice is to use
meaningful variable names
Case Sensitive
myval and myVal refer to two different variables!
Variables should begin with a lower case letter (instead of upper
case), but it is not a requirement.
Exceptions
Java reserved words cannot be used as identifiers:
Ex. for, while, public, static, etc.
IDENTIFIERS
Which of these are legal variable names?
My.Class
7eleven
inputStream
YourClass
google.com
car_Wash
hotCar
go-team
theTimeOfDay
ASSIGNING VALUES
In an assignment, the identifier must be set equal to
the value.
In Java, the = symbol is the assignment operator.
int x;
x = 100;
It is also possible to combine a declaration and an
assignment into one line!
double myVal = 3.14;
MORE ON ASSIGNING VALUES
If all variables are being initialized to the same value, you can
accomplish this in as little as two lines.
int strength, speed, wisdom;
strength = speed = wisdom = 5;
//because heroes begin their journey
//with low stats
Note: You could also have:
int strength=5, speed=5, wisdom=5;
Values of variables can also be set to values of other variables.
double x, y, z;
x = 16.5;
y = 25.0;
z = x;
ASSIGNING CHAR VALUES
char values always take “singular quotes.”
char myChar = ‘k’;
This can get confusing when the identifiers of
chars are just one character.
char c, k;
c = ‘k’;
k = c;
What are the values of c and k?
Use meaningful variable names for the char type!
SIMPLE ARITHMETIC EXPRESSIONS
Consider this code:
int x = 8;
x = x + 1;
What will be the value of x?
int a;
double b, c;
a = 3;
b = 5.1;
c = a + b;
What will be the value of c?
double mealTotal = 30.00;
double tip = mealTotal * 0.20;
mealTotal = mealTotal + tip;
What will be the value of mealTotal?