Professional Documents
Culture Documents
MODULE 5
COMPUTER PROGRAMMING 1
(JAVA)
Content Standard:
The learners demonstrate understanding of the:
1. Data Types
2. Primitive Data vs. Objects
3. The Eight Primitive Data Types of Java
4. Numeric Data Types
5. Character and Boolean Data Types
Performance Standard:
The learners should be able to design a Java Program that utilizes
primitive data types.
Objectives:
After the lesson, the learners should be able to:
1. differentiate primitive data vs objects
2. enumerate the 8 primitive data types of Java
3. explain the importance of the different data types
PRELIMINARY ACTIVITY
Say that you came across the following torn slip of paper. What does it
mean?
DATA TYPES
You would like to see the rest of the paper, or know where it came
from. Without knowing the context, it is hard to say what MIX means.
It could be 1009 in Roman numerals, or it could be the English word "mix"
(which itself has several meanings), or it could be the last name of the
old-time radio hero Tom Mix. It could be part of a label on a music CD,
"MTV Dance MIX", or part of a label on a bottle, "BLOODY MARY MIX".
Or maybe you are looking at it upside down and it should be "XIW". Of
course, it might not be English at all. Without knowing the context, a
string of letters has little meaning.
Computer memory stores arbitrary bit patterns.
As with a string of letters, the meaning of a string of bits depends
on how it is used. The scheme being used for a particular string of bits is
its data type.
For example
0000000001100111
is a pattern of 16 bits that might be found somewhere in computer memory. What does
it represent?
Without knowing more about how the above pattern is being used, it is
impossible to say what it represents. The type short is one of Java's data types. If the
pattern is regarded as data type short, then it represents the value 103 (one hundred
and three). If the pattern is regarded as data type char, then it represents the character
'g'.
Answer: Without knowing more about how the pattern is being used, it is impossible to say.
You might be tempted say that the pattern 0000000000000000 represents "zero".
But it doesn't necessarily. Even such an obvious pattern has no automatic meaning.
If you were told that the above pattern were of type short, then you would know
that it represents the integer zero. Here is another pattern:
1111111110011001
As a short, this pattern represents -103 (negative one hundred three).
There are unaccountably many types of data that can be represented in the
memory of a computer. If specific patterns always had specific meanings, then only a
few types of data could be represented. This would be much too restrictive. Many data
types are invented by programmers as they write programs. Without this flexibility
computers would be much less useful.
Not all machines use memory this way. A simple electronic calculator, for
example, uses memory for one purpose only: to store floating point numbers. It uses
only one data type, and can do only those few things with that data type that it has been
wired to do. The engineers who designed the calculator decided how to represent
numbers with bit strings, and then designed the electronics to work with just those
strings. This is too restrictive for a general-purpose computer.
Question 2: Do you imagine that the computers of the 1960's were built to handle
audio data?
Answer: No — certainly this was not a common use for computers then.
Recall the fourth advantage of binary (from chapter 2): Anything that can be
represented with some sort of pattern can be represented with patterns of bits. Music can
certainly be represented with patterns of symbols, and so can be represented with
patterns of bits. (This is obvious now, but was not quite so obvious in 1960.)
It would be awkward if every time you used data you had to invent your own
scheme to represent it with bits. There are types of data that are so fundamental that
ways to represent them are built into Java. These are the primitive data types. The eight
primitive data types are: byte, short, int, long, float, double, char, and boolean.
Upper- and lower-case characters are important in these names. So "byte" is the
name of a primitive data type, but "BYTE" is not. Computer languages where case is
important are called case sensitive. Some languages are not case sensitive, especially
old languages that were designed when data entry equipment did not have lower case
characters.
In the phrase primitive data type the word primitive means "a fundamental
component that is used to create other, larger parts." This word is used frequently in
computer science. To solve a large problem, you look for the primitive operations that
are needed, then use them to build the solution.
OBJECTS
An object is a big block of data. An object may use many bytes of memory.
An object usually consists of many internal pieces.
The data type of an object is called its class.
Many classes are already defined in Java.
A programmer can invent new classes to meet the particular needs of a program.
A (crude) analogy is that a primitive data value is like a nut or a bolt, but an
object is like a whole machine.
Numbers are so important in Java that six of the eight primitive data types are
numeric types.
Each primitive type uses a fixed number of bits. This means that if you are using a
particular data type then the same number of bits will be used no matter what value is
represented.
For example, all values represented using the short data type use 16 bits. The
value zero (as a short) uses 16 bits and the value thirty thousand uses 16 bits.
All values represented using the long data type use 64 bits. The value zero (as
a long) uses 64 bits, the value thirty thousand uses 64 bits, and the value eight trillion
uses 64 bits.
Values that are large in magnitude (negative or positive) need more bits to be
represented. This is similar to writing out numbers on paper: large numbers need more
digits. If a value needs more bits than a particular data type uses, then it cannot be
represented using that data type.
Question 3: Say that you want to deal with the number 1,023,004 in your
computer program. Would data type short be an appropriate choice?
Answer: No. Data of type short can be only in the range -32,768 to +32,767.
0.333333333333333333
There is no limit to the number of 3's required for complete accuracy. However,
with a limited amount of paper, you cannot be completely accurate. With a data
type, there is a limited number of bits. Those bits cannot accurately represent a value
that requires more than that number of bits.
The number of places of precision for float is the same no matter what the size of
the number. Data type float can represent numbers as big as about 3.4E+38. But the
precision of these large numbers will also be about 7 decimal digits.
Remember: data type float has about the range and precision of a cheap pocket
calculator. This is usually not sufficient.
PRECISION OF DOUBLE
You might wish to argue that there are only five places used in the above number: the
places used by the digits 1, 2, 3, 8, and 9. However, the four 0's in the middle do
count. It takes bits to represent them, even if they are zeros.
0000000001100111
If you know that these 16 bits are of data type char, then you could look in a table
and discover that they represent the character 'g'. If you have a really good memory,
you might recall that the same 16 bits represent the integer 103 if they are regarded as
data type short. Knowing the data type of a pattern is necessary to make sense of it.
Control characters are bit patterns that show the end of a line or where to start
pages. Other control characters represent the mechanical activities of old
communications equipment (such as Teletypes) that are rarely used these days. Many of
these control characters are no longer used for their original purpose.
CHARACTER LITERALS
'\n' '\t'
Each of these represents a single control character. The first one represents the
newline character and the second one represents the tabulation character. You will
rarely use any control characters other than these two. Several others are listed in the
Java documentation.
"Hello"
true false
The billions of bytes of main storage in your home computer are used to store
both machine instructions and data. The electronic circuits of main memory (and all
other types of memory) make no distinction between the two. It just holds bit patterns.
Using the same memory for both instructions and data was the idea of John von
Neumann, a computer pioneer.
Recall that a data type is a scheme for using bit patterns to represent a value.
Think of a variable as a little box made of one or more bytes that can hold a value using
a particular data type.
To put a value in memory, and later to get it back, a program must have a name
for each variable. Variables have names such as payAmount. (Details will be given in a
few pages.)
Answer: Yes. Otherwise it would not be clear what its bits represent.
DECLARATION OF VARIABLEE
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. In a high level language (such as Java)
the programmer does not need to worry about how the computer hardware actually
does what was asked. If you ask for a variable of type long, you get it. If you ask for the
value 123 to be placed in the variable, that is what happens. Details about bytes, bit
patterns, and memory addresses are up to the Java compiler.
dataType variableName;
This declares a variable, declares its data type, and reserves memory for it. It says
nothing about what value is put in memory. (Later in these notes you will learn
that in some circumstances the variable is automatically initialized, and that in
other circumstances the variable is left uninitialized.)
This 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 data
type.
This 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.
This declares two variables, both of the same data type, reserves memory, and
puts an initial value in each variable. You can do this all on one line if there is
room. Again, you can do this for more than two variables as long as you follow
the pattern.
The programmer picks a name for each variable in a program. Various things in
a program are given names. A name chosen by a programmer is called
an identifier. Here are the rules for identifiers:
Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', and
characters '$' and '_'.
An identifier cannot contain the space character.
Do not start with a digit.
An identifier can be any length.
Upper and lower case count as different characters.
o SUM and Sum are different identifiers.
An identifier cannot be a reserved word.
An identifier must not already be in use in this part of the program.
So far, the example programs have been using the value initially put into a
variable. Programs can change the value in a variable. An assignment
statement changes the value that is held in a variable. The program uses an assignment
statement.
The assignment statement puts the value 123 into the variable. In other words,
while the program is executing there will be a 64 bit section of memory that holds the
value 123.
Remember that the word "execute" is often used to mean "run". You speak of
"executing a program" or "executing" a line of the program.
variableName = expression ;
Here are some example assignment statements (assume that the variables have
already been declared):
total = 3 + 5;
price = 34.56;
tax = total*0.05;
In the source file, the variable must be declared before any assignment statement
that uses that variable.
Example
value = 2*3
ASSESSMENT
MULTIPLE CHOICE
Direction: For each question, choose the correct answer. Encircle the letter only.
1. Which one of the following is a reserved word?
A. apple B. 7up
D. boolean
C. grandTotal
2. Which one of the following is NOT a correct variable name?
A. 2bad C. theLastValueButOne
B. zero D. year2000
4. Which of the following is NOT the name of a Java primitive data type?
A. int C. double
B. float D. String
5. What is an expression?
A. The same thing as a statement.
B. An expression is a list of statements that make up a program
C. An expression is a combination of literals, operators, variables, and
parentheses used to calculate a value.
D. An expression is a number expressed in digits.
6. Say that a particular item of data in a Java program does NOT use a primitive data
type. What must it be?
A. An object. C. A literal.
B. A number. D. A boolean.
7. What are the two steps that take place when an assignment statement is executed?
A. (i) Evaluate the Expression, and (ii) Store the value in the variable.
B. (i) Store the value in the variable, and (ii) Evaluate the Expression.
C. (i) Reserve memory, and (ii) fill it with a number.
D. (i) Evaluate the variable, and (ii) store the results.
FEEDBACK
________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
__________________________________________________________
REFERENCES
1. http://programmedlessons.org/Java9/chap08/ch08_01.html
2. https://www.youtube.com/watch?v=qUXbJziVs_o
3. http://programmedlessons.org/Java9/chap09/ch09_01.html
4. https://www.youtube.com/watch?v=RA7wkTV6z4k