Why Do We Need Variables?
Why Do We Need Variables?
variables?
Why do we need
variables?
Java Basics:
Variables & Data Types
Variables
Variables have two attributes: a name and a type
We will consider these two attributes one at a time. First
let us consider names
Java Identifiers
An identifier is a name, such as the name of a variable, a
method, or a class.
netscape.com
Naming Conventions
Java uses the following conventions:
Variable names and method names, regardless of
their type, begin with a lowercase letters (e.g. myName,
processData)
Multiword names are punctuated using uppercase letters
(usually called camel case)
Variables
Variables have two attributes: a name and a type
Next let us consider types
Data types
Internally, computers store everything as 1s and 0s
154
"hi"
0000000010011010
0110100001101001
Types that are not primitive are called object types (seen later)
-1
365
12000
Floating-point types
0.99
-22.0
3.14159
-0.25
9.4e3
Character type
'a'
'A'
'#'
boolean type
true
false
Variables
Variables have two attributes: a name and a type
Now that we know about names and types, we can
combine them to declare variables
Declaring Variables
When you declare a variable, you provide its type and
name:
int numberOfRings;
Declaring Variables
When you declare a variable, you provide its type and
name:
int numberOfRings;
Declaring Variables
When you declare a variable, you provide its type and
name:
int numberOfRings;
Declaring Variables
Syntax:
type
variable_1, variable_2, ;
Java Basics:
Variables and Assignment
Assignment Statements
An assignment statement is used to assign a value
to a variable
answer = 42;
Assignment Examples
numOfStacks = 4;
topCard = 'Q';
points = 10 * cardsPlayed;
Assignment Evaluation
1.
Assignment Evaluation
1.
2.
Assignment Evaluation
1.
2.
cardsInHand = cardsInHand - 1;
Before assignment
cardsInHand
Assignment Evaluation
1.
2.
cardsInHand = cardsInHand - 1;
After assignment
cardsInHand
Using variables
Once given a value, a variable can be used in expressions,
and can assign a value more than once :
int
x =
y =
z =
x =
...
x, y, z;
3;
2 * x;
5 + x*y;
4 + y;
//
//
//
//
x
y
z
x
is
is
is
is
3
6
23
now 10
Java Basics:
Expressions
Expressions
An expression is value or operation that computes a value
Examples:
5 + 14
8 / 2 * (4 + 3)
17
Arithmetic Operators
Arithmetic expressions can be formed using operators
which allow us to combine values referred to as the
operands
Arithmetic operators include:
+
*
/
%
addition
subtraction (or negation)
multiplication
division
modulus (a.k.a. mod or remainder)
Precedence Rules
Precedence Rules
When binary operators have equal precedence, they are
evaluated left-to-right.
1+2-3+4 is the same as ((1+2)-3)+4
Mixing types
When int and double are mixed, the result is a double
4.2 * 3 is 12.6
Java Basics:
More on Assignment
can be written as
time += 10;
expr;
expr;
expr;
expr;
expr;
Also:
count--;
--count;
count = count - 1;
count -= 1;
Post-increment: count++
Increments the variable but returns the original value
Initializing Variables
A variable that has been declared, but not yet
given a value is said to be uninitialized
The compiler will not allow you to use an
uninitialized variable
To protect against an uninitialized variable (and to
keep the compiler happy), it is good practice to
assign a value at the time the variable is declared
grade
score
12
Assignment Compatibilities
Java is said to be strongly typed
You cant, for example, assign a floating point value to a variable
declared to store an integer
int myNumber = 7.5; // Error: Compiler will not allow
Assignment Compatibilities
Sometimes conversions between numbers are possible
double myVariable = 7;
// this is OK
Assignment Compatibilities
A value of one type can be assigned to a variable of any
type further to the right
byte --> short --> int --> long --> float --> double
Type Casting
A type cast temporarily changes the value of a variable
from the declared type to some other type. It does not
change the variable.
For example,
double distance;
distance = 9.0;
int points;
points = (int)distance;