Chapter 2b Variables Assignment Expressions (Fas)
Chapter 2b Variables Assignment Expressions (Fas)
Expressions
Variables
• In order to put data in memory, and
later get it back again, a program must
have a name for each section of memory
that it uses:
– variable
• a name for a location in main memory which uses
a particular data type to hold a value.
Question
Must a variable always have a data type?
is a declaration of a variable.
• A declaration of a variable is where a
program says that it needs a variable.
• For our small programs, declaration
statements will be placed between the two
braces of the main method.
• The declaration gives a name and a data type
for the variable.
• It may also ask that a particular value be
placed in the variable
public class Example
{
public static void main ( String[] args )
{
// declaration of a variable and initialises
// the value.
int payAmount = 123;
System.out.println("The variable contains: “
+ payAmount );
} The line The + joins
the two
} int payAmount = 123; items in
is a declaration of a variable. the output
The effect of declaring a
variable in Java
Computer Memory Java Instruction
score
int score ;
Syntax of Variable Declaration
• dataType variableName;
– declares a variable, declares its data type, and
reserves memory for it. It says nothing about what
value is put in memory.
• dataType variableName = initialValue ;
– The second way declares a variable, declares its
data type, reserves memory for it, and puts an
initial value into that memory. The initial value
must be of the correct dataType.
• dataType variableNameOne, variableNameTwo ;
– The third way declares two variables, both of the
same data type, reserves memory for each, but
puts nothing in any variable. You can do this with
more than two variables, if you want
Names for Variables
• Use only the characters 'a' through 'z', 'A' through
'Z', '0' through '9', character '_', and character '$'.
• A name can not contain the space character.
• Do not start with a digit.
• A name can be any length.
• Upper and lower case count as different characters.
– So SUM and Sum are different names.
• A name must not already be in use in this part of the
program
• A name can not be a reserved word.
long good-by ;
double bubble = 0, toil= 9, trouble = 8
double the bullet ;
int double;
char thisMustBeTooLong ;
int 8ball;
Example Program
public class Example
{
public static void main ( String[] args )
{
int hoursWorked = 40;
double payRate = 10.0, taxRate = 0.10;
System.out.println("Hours Worked: " + hoursWorked );
System.out.println("pay Amount : " +
(hoursWorked * payRate) );
System.out.println("tax Amount : " +
(hoursWorked * payRate * taxRate) );
}
} Order is important we need to
declare a variable before we
use it.
Java Naming Convention
1. the name of a class starts with a
Capital case letter
2. the name of a variable starts with a
lower case letter
3. if a name consists of several words the
first word starts with a lower case and
each of the rest an upper case, e.g
myMarkForTheModule
Assignment Statements
• So far, we have been using the value
initially put into a variable, but not
changing the value that a variable holds.
• Of course, variables are expected to
vary by having new values placed into
them as the program runs.
• An assignment statement changes the
value that is held in a variable
public class Example3
{
public static void main ( String[] args )
{
int payAmount ; //a declaration
payAmount = 123; //an assignment statement
System.out.println("The variable contains: "
+ payAmount );
}
}
Assignment Statement Syntax
• variableName = expression ;
– The equal sign "=" means "assignment."
– variableName is the name of a variable that
has been declared somewhere in the
program.
– expression is a collection of characters that
calls for a value.
Assignment Statement Syntax
• An assignment statement asks for the
computer to perform two steps, in
order:
– Evaluate the expression (that is: calculate a
value.)
– Store the value in the variable.
– For example, the assignment statement:
• sum = 32 + 8 ; asks for two actions:
– Evaluate the Expression — 32 + 8 is calculated,
yielding 40.
– Store the value in the variable. — 40 is placed in sum
Example Assignment Statements
• weeklyPay = 200.59 ;
• totalPay = 52 * weeklyPay;
• extraPay = totalPay / 12 + weeklyPay * bonus;
• extraPay = (totalPay / 12 + weeklyPay )*
bonus;
% Remainder
+ Addition
- minus
Arithmetic Operators
3.5/2.0 = 1.75
Casting
• Variables and expressions can be covered
from one data type into another data type by
using casting.
• We have implicit casting and explicit casting.
• Implicit casting is when we move from a
smaller data type to a larger one. For example
int x = 100;
long y = x;
DURABLE = 0.441;
How many 100’s 10’s 1’s
• We can use simple mathematics to help
solve everyday problems.
• How do we split the number
• 453 into 4, 5, 3 by using mathematics
System.out.println(digit);
// produces output of 9
System.out.println(number);
// produces output of 87
//CONTINUE CODE…..