of 30
8/19/2019 Java: Intro to Variables
1/30
Variables
Chapter 2.1
8/19/2019 Java: Intro to Variables
2/30
Variables• Variables store data such as numbers and
letters. – Think of them as places to store data. –
They are implemented as memory locations.• The data stored by a variable is called its
value. – The value is stored in the memory location.
• Its value can be changed.
8/19/2019 Java: Intro to Variables
3/30
Variables and Values• Variables
numberOfBasketseggsPerBasket
totalEggs• Assigning values
eggsPerBasket = 6;eggsPerBasket = eggsPerBasket - 2;
• What is the value of eggs er!asket no"#
8/19/2019 Java: Intro to Variables
4/30
$aming and %eclaringVariables
• Choose names that are helpful &moreabout documentation in a later "eek'
•
When you declare a variable( you provideits name and type as a statement.int numberOfBaskets,eggsPerBasket ;
• A variable)s type determines "hat kinds ofvalues it can hold & int , double , char , etc .'.
• A variable must be declared before it is
used.
8/19/2019 Java: Intro to Variables
5/30
*ynta+ and ,+amples• *ynta+
type ariable!", ariable!2, #;• ,+amples
int style$hoice, numberOf$hecks;double balance, interest%ate;char &ointOr'ndi idual;
8/19/2019 Java: Intro to Variables
6/30
%ata Types• A class type is used for a class of ob-ects
and has both data and methods. – ()a a is fun( is a value of class type
*tring• A primitive type is used for simple(
nondecomposable values such as anindividual number or individual character.
– int , double , and char are primitive types.
8/19/2019 Java: Intro to Variables
7/30
rimitive Types
8/19/2019 Java: Intro to Variables
8/30
ava Identi/ers• An identifer is a name( such as the name
of a variable.• Identi/ers may contain only
– 0etters – %igits & through ' – The underscore character &3' – And the dollar sign symbol &4' "hich has a
special meaning• The /rst character cannot be a digit.
8/19/2019 Java: Intro to Variables
9/30
,+ercise5• Identi/ers may not contain any spaces(
dots & . '( asterisks & * '( or other characters6+-"" oracle com util ¬ allo"ed'
• Identi/ers can be arbitrarily long.• *ince ava is case sensitive ( stuff ,
*tuff , and *./00 are di7erent identi/ers.
8/19/2019 Java: Intro to Variables
10/30
,+ercise5• What is the value of totalValue#stu7 8 19
*tu7 8 29*tu7 8 :9stu7 8 ;9
totalValue 8 stu7 < *tu79
8/19/2019 Java: Intro to Variables
11/30
=ey"ords or >eserved
Words• Words such as if are called keywords or
reserved words and have special(
prede/ned meanings. – Cannot be used as identi/ers. – *ee Appendi+ 1 for a complete list of ava
key"ords.•
,+ample key"ords6 int , public , class• If the font changes color in an editor
it’s probably a reserved word and youneed to change the variable name
8/19/2019 Java: Intro to Variables
12/30
$aming Conventions• Class types begin "ith an uppercase letter
&e.g. *tring '.• rimitive types begin "ith a lo"ercase
letter &e.g. int '.• Variables of both class and primitive types
begin "ith a lo"ercase letters&e.g. my1ame , myBalance '.
• ?ulti"ord names are @punctuated@ usinguppercase letters.
8/19/2019 Java: Intro to Variables
13/30
,+ercise5
In a program "e "ant to store6• A person s Bull $ame•
A person s middle initial• Their "hole number age• Their hourly "age
%eclare legal variables in order to dothis.
8/19/2019 Java: Intro to Variables
14/30
Where to %eclare Variables
• %eclare a variable – ust before it is used or – At the beginning of the section of your program
that is enclosed in 3 . public static oid main4*tring5 args7
8 declare ariables here 8 3
8/19/2019 Java: Intro to Variables
15/30
rimitive Types• Bour integer types & byte , short , int ,
and long ' – int is most common
• T"o oatingDpoint types & float anddouble '
– double is more common•
Ene character type & char '• Ene boolean type & boolean '
8/19/2019 Java: Intro to Variables
16/30
,+amples of rimitive 0iteral Values
• Integer types9 -" :6 "2999
• BloatingDpoint types9
8/19/2019 Java: Intro to Variables
17/30
Assignment *tatements• An assignment statement is used to assign
a value to a variable.ans er = >2;
• The @eFual sign@ is called the assignmentoperator.
• We say( @The variable named ans er is
assigned a value of ;2(@ or more simply(@ans er is assigned ;2.@
8/19/2019 Java: Intro to Variables
18/30
Assignment *tatements• *ynta+
ariable = eCpression;"here eCpression can be anothervariable( a literal or constant &such as anumber'( or something more complicated"hich combines variables and literalsusing operators &such as < and D'
8/19/2019 Java: Intro to Variables
19/30
Assignment ,+amples
amount = :
8/19/2019 Java: Intro to Variables
20/30
InitialiGing Variables• A variable that has been declared( but not
yet given a value is said to beuninitialized.
• HninitialiGed class variables have the valuenull .
• HninitialiGed primitive variables may havea default value.
• It)s good practice not to rely on a defaultvalue.
8/19/2019 Java: Intro to Variables
21/30
InitialiGing Variables• To protect against an uninitialiGed variable
&and to keep the compiler happy'( assign avalue at the time the variable is declared.
• ,+amples6int count = 9;char grade = ?@?;
8/19/2019 Java: Intro to Variables
22/30
InitialiGing Variables• synta+
type ariable!" = eCpression!",ariable!2 = eCpression!2, #;
8/19/2019 Java: Intro to Variables
23/30
Assignment ,valuation• The e+pression on the rightDhand side of the
assignment operator & 8 ' is evaluated /rst.• The result is used to set the value of the
variable on the leftDhand side of theassignment operator.score = numberOf$ards handicap;
eggsPerBasket = eggsPerBasket - 2;
8/19/2019 Java: Intro to Variables
24/30
,+ercise5• %eclare and assign values to each of
the variables per line.
Variable Valuefull$ame ohn %oeJ
middleInitial KC
age 21
hourlyWage ; .LfullTime true
8/19/2019 Java: Intro to Variables
25/30
*imple Input
• *ometimes the data needed for acomputation are obtained from the userat run time.
• =eyboard input reFuiresimport &a a util *canner;at the beginning of the /le.
8/19/2019 Java: Intro to Variables
26/30
*imple Input• %ata can be entered from the keyboard
using *canner keyboard =ne *canner4*ystem in7;
follo"ed( for e+ample( byeggsPerBasket = keyboard neCt'nt47;"hich reads one int value from the
keyboard and assigns it to eggsPerBasket .
8/19/2019 Java: Intro to Variables
27/30
*imple *creen Eutput
*ystem out println4(.he count is ( count7;
• Eutputs the sting literal F.he count is ( • Bollo"ed by the current value of the variable
count .
8/19/2019 Java: Intro to Variables
28/30
Constants• 0iteral e+pressions such as 2, : +, or ?y?
are called constants .• Integer constants can be preceded by a
or - sign( but cannot contain commas.• BloatingDpoint constants are "ritten "ith
digits before and after a decimal point
8/19/2019 Java: Intro to Variables
29/30
Imprecision in BloatingD oint$umbers• BloatingDpoint numbers often are only
appro+imations since they are stored "ith a/nite number of bits.
• Mence " 98: 9 is slightly less than 1N:. –
The real mathematical representation is.::::::::::5 – This is truncated to the number of places that
can be stored•
,+ample of issue – As a fraction 1N: < 1N: < 1N: 8 :N: 8 1 – This becomes .::::: < .::::: < .::::: 8 .
8/19/2019 Java: Intro to Variables
30/30
$amed Constants• ava provides mechanism to 5
– %e/ne a variable – InitialiGe it – Bi+ the value so it cannot be changed
public static final .ype Gariable = $onstant;
–
,+ample public static final double P' = : ">"