Exam 1 CS-1440 Java Programming
Intro
- The difference between a variable and a literal - A literal is notation for representing a fixed ( const )
value. A variable is storage location associated with a symbolic name (pointed to, if you'd like). In any
programming language a Literal is a constant value, where as identifiers can change their values.
Identifiers can store literals and process them further.
How to declare a variable - To declare (create) a variable, you will specify the type, leave at least one
space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int
for integer, double for a floating point number (a double precision number), and boolean for a Boolean
value (true or false).
int score;
How to assign a value to a variable. - Java is pass-by-value. That means pass-by-copy
Declare an int variable and assign it the value '7'. The bit pattern for 7 goes into the variable named x.
int x = 7;
How to initialize a variable. - Initializing a variable. Initializing a variable means specifying an initial value
to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a
defined value, hence it cannot be used until it is assigned such a value.
- What a data type is (i.e., what makes up a data type) - A data type is a set of values and a set of
operations defined on them. For example, we are familiar with numbers and with operations defined on
them such as addition and multiplication. There are eight different built-in types of data in Java, mostly
different kinds of numbers. We use the system type for strings of characters so frequently that we also
consider it here.
,The eight primitive data types, and which ones we are using in this course - Variables and the 8 Primitive
Data Types
The Purpose of a Variable (and some vocabulary)
You can think of a simple program as a list of instructions to be read and acted upon sequentially
Example:
Read a value representing the radius of a circle from the standard input source/stream
Compute the area of a circle with this radius
Print the area to the standard output stream (i.e., the console window)
Remember: a computer will read and act upon these instructions one at a time - it is not aware of what
is coming up until it gets there!
Looking at step 1 in the program above, we will need to tell the computer that it needs to remember the
value it is reading in - it needs to store this value in its memory somewhere so we can use it in a
computation later.
We need to tell the computer how much memory will be needed to store the value in question.
Different kinds of numbers require different amounts of memory (more on this in a minute). Of course,
we sometimes need to store things other than numbers. These things too, come in different sizes. For
example, it will certainly take more memory to store the Declaration of Independence than it will to
store a single letter (i.e., a "character"). Further, we also need to tell the computer how the value should
be stored in memory (i.e., what method of "encoding" should be employed to turn the value into a
string of 1's and 0's). Examples of types of encodings used include Two's Complement, IEEE 754 Form,
ASCII, Unicode, etc...
The computer also needs to have some reference to where it stored the value, so it can find it again.
The concept of a variable solves all of our problems here. A variable in Java gives us a way to store
values (or other kinds of information) for later use, addressing all of the aforementioned considerations.
,The amount of memory allocated for a given v
What the arithmetic operators are, and how they work at each data type. - Arithmetic Operators
The Java programming language supports various arithmetic operators for all floating-point and integer
numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and %
(modulo). The following table summarizes the binary arithmetic operations in the Java programming
language.
Operator Use Description
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
- op1 - op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
Here's an example program, ArithmeticDemo (in a .java source file), that defines two integers and two
double-precision floating-point numbers and uses the five arithmetic operators to perform different
arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are
shown in boldface:
public class ArithmeticDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
, System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
Intro
- The difference between a variable and a literal - A literal is notation for representing a fixed ( const )
value. A variable is storage location associated with a symbolic name (pointed to, if you'd like). In any
programming language a Literal is a constant value, where as identifiers can change their values.
Identifiers can store literals and process them further.
How to declare a variable - To declare (create) a variable, you will specify the type, leave at least one
space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int
for integer, double for a floating point number (a double precision number), and boolean for a Boolean
value (true or false).
int score;
How to assign a value to a variable. - Java is pass-by-value. That means pass-by-copy
Declare an int variable and assign it the value '7'. The bit pattern for 7 goes into the variable named x.
int x = 7;
How to initialize a variable. - Initializing a variable. Initializing a variable means specifying an initial value
to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a
defined value, hence it cannot be used until it is assigned such a value.
- What a data type is (i.e., what makes up a data type) - A data type is a set of values and a set of
operations defined on them. For example, we are familiar with numbers and with operations defined on
them such as addition and multiplication. There are eight different built-in types of data in Java, mostly
different kinds of numbers. We use the system type for strings of characters so frequently that we also
consider it here.
,The eight primitive data types, and which ones we are using in this course - Variables and the 8 Primitive
Data Types
The Purpose of a Variable (and some vocabulary)
You can think of a simple program as a list of instructions to be read and acted upon sequentially
Example:
Read a value representing the radius of a circle from the standard input source/stream
Compute the area of a circle with this radius
Print the area to the standard output stream (i.e., the console window)
Remember: a computer will read and act upon these instructions one at a time - it is not aware of what
is coming up until it gets there!
Looking at step 1 in the program above, we will need to tell the computer that it needs to remember the
value it is reading in - it needs to store this value in its memory somewhere so we can use it in a
computation later.
We need to tell the computer how much memory will be needed to store the value in question.
Different kinds of numbers require different amounts of memory (more on this in a minute). Of course,
we sometimes need to store things other than numbers. These things too, come in different sizes. For
example, it will certainly take more memory to store the Declaration of Independence than it will to
store a single letter (i.e., a "character"). Further, we also need to tell the computer how the value should
be stored in memory (i.e., what method of "encoding" should be employed to turn the value into a
string of 1's and 0's). Examples of types of encodings used include Two's Complement, IEEE 754 Form,
ASCII, Unicode, etc...
The computer also needs to have some reference to where it stored the value, so it can find it again.
The concept of a variable solves all of our problems here. A variable in Java gives us a way to store
values (or other kinds of information) for later use, addressing all of the aforementioned considerations.
,The amount of memory allocated for a given v
What the arithmetic operators are, and how they work at each data type. - Arithmetic Operators
The Java programming language supports various arithmetic operators for all floating-point and integer
numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and %
(modulo). The following table summarizes the binary arithmetic operations in the Java programming
language.
Operator Use Description
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
- op1 - op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2
Here's an example program, ArithmeticDemo (in a .java source file), that defines two integers and two
double-precision floating-point numbers and uses the five arithmetic operators to perform different
arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are
shown in boldface:
public class ArithmeticDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
, System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));