Title: CS2173 Lecture 04
1CS2173Lecture 04
- Variables, Formatted Output
2Variables and Declarations (2.4) All the
data in our program must be stored in the
computers memory somewhere. Computer memory is
divided into blocks, each of which has an
address. Once upon a time, if you wanted to
store data in your program, you had to use these
addresses to do it (see fig 2.5 in book) What
we would like to do is to be able to give a name
to a memory address so that we (humans) can
remember it better. This is a variable
3Variable a name given by the programmer that
refers to computer storage locations. (We call it
variable because the value stored at that
location can change, or vary). A good variable
name should be a mnemonic. (book error fig 2.6
the address of total should be 3000, not 45)
4We must tell the compiler the names and data
types of all the variables we will use in our
program. This is known as variable declaration.
A declaration statement looks like this int
myInteger double average In general
(syntax) dataType varName
5New Template for Programs import
statements public class className public
static void main(String args) //
Declaration block declaration statements //
Body other statements
6Assignment statements num1 42 // Variable on
left of the , // value or expression on the
right. NOTE The operator in Java does NOT
mean equal to It means is assigned. This is
called the assignment operator. We will see the
equality operator later.
7public class variablesEx1
public static void main(String args)
// Declaration block //
Declare numExams as an integer int
numExams // number of exams taken. double
score1 // first test grade. double
score2 // Second test grade. double
average // Average of all exams.
// Body
numExams 2
score1 95.0
score2 80.0
average (score1 score2) /
numExams // Print the average (should be
90.0) System.out.println("The average is "
average ".")
8Multiple Declarations In Java, if you want to
declare several variables of the same type, you
can group them together in a single list
declaration with commas, like this double
score1, score2, average This is the same
as double score1 double score2 double average
9You can also give a variable a value (initialize
it) at the same time as you declare it.
Examples double score1 95.0 int
numExams 2 char firstChar 'a' General
Syntax dataType varName value
10String Declarations Strings in Java are not
primitive data types. They are actually
reference data types, meaning that the memory
location we name (with a variable name) actually
contains the address of where in memory the
String actually resides (it doesnt contain the
String itself).
11A String is actually an object. So we should
need to create a new instance of a String before
we use it. like this String myString myString
new String("Hello World") You can also
combine this into one statement String myString
new String("Hello World")
12But, since we use Strings so much in Java, the
designers of the language gave us a shortcut way
to do this so we dont have to say new every
time we want a String We can just do
this String myString myString Hello
World or this String myString Hello World
13Recall last time we said that you can print
numerical results by using the same print
statements that print strings System.out.print(T
he sum is (2 2)) System.out.println(5 /
4 is (5 / 4)) // Prints 5 / 4 is
1 JOptionPane.showMessageDialog(null, 5 4
is (5 4), Math, JOptionPane.PLAIN_MESSAGE
) What if we want to format the output
14import java.text. // _at_ top of program! // Now
set up a DecimalFormat object! DecimalFormat num
new DecimalFormat(000) // Now if we use the
DecimalFormat objects// format() method, we can
format our // numerical output // Prints
006System.out.println(num.format(6)) //
Prints 943 System.out.println(num.format(943))
15- So to do formatted output with DecimalFormat, we
have to - import java.text.
- Create a new DecimalFormat object with the new
operator. - Initialize it to the desired formatting string.
- Call the objects format() method with the
numeric data we want to format.
16Formatting strings - a digit placeholder.
Zero shows as absent, not a space. 0 - A digit
placeholder and padding character. . - Decimal
placeholder , - Grouping separator - Separate
positive and negative format strings. - Treat
value as a percentage (multiply by 100 and add a
sign).
17Examples Format so that there is a comma every
three digits (like 1,227,429) Format so
that there is a comma every three digits AND
there are at most two decimal digits
(rounded)Look at table 2.9.
18Examples (continued) Format so that there is a
comma every three digits and exactly two decimal
digits of precision (rounded) Remember
that even when you set up the DecimalFormat
object, you must still call the objects format()
method with the number you want to format as an
argument.
19NOTE If you put other characters into your
format string, they will be output in the
formatted numbers Thats why the symbols
are in table 2.9. A good example DecimalFormat
money new DecimalFormat(,.00) Would
allow you to do money formatting with the dollar
sign, commas, and two decimal places.
20(Optional) Storage Allocation (pg. 73) Garbage
Collection