Java Primitive Types Declarations Assignments Expressions: Unit 2 Day 1
Java Primitive Types Declarations Assignments Expressions: Unit 2 Day 1
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
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
byte
(8-bit)
-128 to +127
short
(16-bit)
-32768 to +32769
long (64-bit)
-263 to 263 – 1 (~19 digits)
FLOATING-POINT PRIMITIVES
float
(32-bit)
int x;
Declares x as an int.
double myVal;
Declares myVal as a double.
Can contain:
Letters
Digits 0 – 9
First character CANNOT be a digit, though
The underscore _
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
int x;
x = 100;
If all variables are being initialized to the same value, you can
accomplish this in as little as two lines.
double x, y, z;
x = 16.5;
y = 25.0;
z = x;
ASSIGNING CHAR VALUES
int a;
double b, c;
a = 3;
b = 5.1;
c = a + b;
What will be the value of c?