02 Lesson - Basic Input output_
02 Lesson - Basic Input output_
Minds On…
With only using one System.out.println()Write a program that produces the following
output:
In your program, substitute ??? with your own name. If necessary, adjust the positions and the
Action …
Variables
Key Point
Variables are used to represent values that may be changed in the program.
Variables are used to store values to be used later in a program. They are called variables because
their values can be changed. Variables are used to represent data of a certain type. To use a variable,
you declare it by telling the compiler its name as well as the type of data it can store. The variable
declaration tells the compiler to allocate appropriate memory space for the variable based on its data
type. The syntax for declaring a variable is:
If variables are of the same type, they can be declared together, as follows:
dataType variable1, variable2, variable3;
For example,
int i, j, k;
Note
By convention, variable names are in lowercase. If a name consists of several words, concatenate all of
them and capitalize the first letter of each word except the first. Examples of variables are radius,
numberOfStudents, and interestRate.
Variables often have initial values. You can declare a variable and initialize it in one step. Consider, for
instance, the following code:
int count = 1;
int count;
count = 1;
You can also use shorthand to declare and initialize multiple variables of the same type together like this:
int i = 1, j=2;
Key Point
An assignment statement designates a value for a variable. An assignment statement can be used as
an expression in Java.
After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the
equal sign ( = ) is used as the assignment operator. The syntax for assignment statements is as follows:
variable = expression;
An expression represents a computation involving values, variables, and operators that, taking them
together, evaluates to a value. In an assignment statement, the expression on the right-hand side of
the assignment operator is evaluated, and then the value is assigned to the variable on the left-hand
side of the assignment operator. For example, consider the following code:
Named Constants
Key Point
A named constant is an identifier that represents a permanent value.
The value of a variable may change during the execution of a program, but a named constant, or
simply constant, represents permanent data that never changes. Here is the syntax for declaring a
constant using the keyword final:
final dataType CONSTANT_NAME = value;
For example,
Assessment
2. Write a statement to declare a variable named x of the double type with an initial value 4.5.
3. Which of the following are correct names for variables according to the naming conventions
adopted in this course?
4. What are the benefits of using constants? Declare an int constant SIZE with a value of 20.
Step 1: Declare a double variable named miles with initial value 100.
Step 3: Declare a double variable named kilometers, multiply miles and KILOMETERS_PER_MILE, and
assign the result to kilometers
6. To declare a constant MAX_LENGTH inside a function with value 99.98, you write:
a. final MAX_LENGTH = 99.98;
b. constant MAX_LENGTH= 99.98;
c. constant double MAX_LENGTH= 99.98;
d. final double MAX_LENGTH= 99.98;
7. Which of the following is a constant according to the convention adapted in this course?