0% found this document useful (0 votes)
7 views

Chapter 2b Variables Assignment Expressions (Fas)

introduction to variable assignment in java

Uploaded by

sarahayeesha1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Chapter 2b Variables Assignment Expressions (Fas)

introduction to variable assignment in java

Uploaded by

sarahayeesha1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Variables, Assignment,

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?

Yes. Otherwise it would not be clear what


its bits represent.
Declaration of a Variable
• The line
int payAmount = 123;

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.

word which has a predefined meaning in Java. For example


int, double, true
Quiz

Which of the following variable declarations are


correct?

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;

• initial = ‘X’; // character assignment

• foundBook = false; // boolean assignment


Arithmetic
Operator Meaning
Operators
- Unary minus
An arithmetic
operator is + Unary plus
a symbol that asks for
doing some arithmetic * multiplicatio
n
/ division

% Remainder

+ Addition

- minus
Arithmetic Operators

• All of these operators can be used on floating point


numbers and on integer numbers.
– However, the % operator is rarely used on floating point.
• / means
– "integer division" if both operands are integers,
– "floating point division" if one or both operands are floating
point.

• An integer operation is always done with 32 bits or


more.
– If one or both operand is 64 bits (data type long) then the
operation is done with 64 bits.
• Otherwise the operation is done with 32 bits, even if
both operands are smaller.
Arithmetic Operators
• For example, with 16 bit short variables, the
arithmetic is done using 32 bits:
short x = 12; // 16 bit short
int result; // 32 bit
int result = x / 3; // arithmetic done using 32 bits

• The expression x / 3 divides a 32 bit 12 by


a 32 bit 3 and put the 32 bit answer in result.
• The literal 3 automatically represents a 32
bit value.
Weird Integer Arithmetic
• The division operator "/" means integer
division if there is an integer on both
sides of it.
• The result of integer division is always
an integer;
• If the exact result calls for a decimal
fraction, that part of the result is
dropped (not rounded.)
Arithmetic operations and data
types

System.out.println(“integer: " + (1/2 + 1/2) );


integer:

System.out.println(“floating: " + (1/2.0 + 1/2.0) );


floating:

System.out.println(“mixed: " + (1/2 + 1.0/2) );


mixed:
What type (integer or floating point) of
operator is the / in the following:
(12 + 0.0) / 7

• Floating point. Adding floating point 0.0


to the integer 12 results in a floating
point 12.0. Now the division is floating
point because one of its operands is.
( 1/2 + 3.5 ) / 2.0 = ?
( 1/2 + 3.5 ) / 2.0
--- do first
Since both operands are integer, the
operation is integer division, resulting
in:
( 0 + 3.5 ) / 2.0

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;

• The java compiler accepts the implicit casting


as the possible values for long are grater than
the possible values for int.
Casting cont.
• Explicit casting is required when we covert
from a larger data type to a smaller data
type.
long x = 100;
int y = x;
• The compiler would report an error as their
may be a loss of precision, as the value in the
long variable may be bigger than what can be
represented by int.
int y = (int)x;
• The casting forces the compiler to accept the
type conversion
Casting expressions
• We can cast the individual variables or we can
cast the results of an expression.
• double x = ( 1/2 + 3.5 ) / 2.0
• The result in x would be 1.75
• double y = ((int)( 1/2 + 3.5 )) / 2.0
• The result in y would be 1.5
• double z = (int)(( 1/2 + 3.5 ) / 2.0)
• The result in y would be 1.0
Constants
public class CalculateTax {
public static void main ( String[] arg ) {
final double DURABLE = 0.045;
final double NONDURABLE = 0.038;
. . . . . . The reserved word final tells the
} compiler that the value will not
change
} tax = gross * DURABLE ;

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

• The solution is an algorithm. A set of


steps which solves the problem.
Solution
int digit; Integer division will
int number = 987; drop the decimal
digit = number/100; fraction
number = number%100;

System.out.println(digit);
// produces output of 9
System.out.println(number);
// produces output of 87
//CONTINUE CODE…..

You might also like