0% found this document useful (0 votes)
7 views8 pages

Java Essentials: Identifiers and Variables

The document provides an overview of Java programming essentials, covering identifiers, variables, assignment statements, constants, numeric data types, and operations. It explains the rules for naming identifiers, variable declaration and initialization, as well as the use of assignment and augmented assignment operators. Additionally, it introduces input reading from the console using the Scanner class and demonstrates how to handle different numeric types and operations in Java.

Uploaded by

ghasaqworld2661
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Java Essentials: Identifiers and Variables

The document provides an overview of Java programming essentials, covering identifiers, variables, assignment statements, constants, numeric data types, and operations. It explains the rules for naming identifiers, variable declaration and initialization, as well as the use of assignment and augmented assignment operators. Additionally, it introduces input reading from the console using the Scanner class and demonstrates how to handle different numeric types and operations in Java.

Uploaded by

ghasaqworld2661
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

College of Information Technology October 5 2025

Department of Information Networks Prog. Lang. Lect. 2


Year 2, Course 1 Harith AK

Java Essentials
1. Identifiers
Identifiers are the names that identify the elements such as classes, methods, and variables in a
program.
All identifiers must obey the following rules:
■ An identifier is a sequence of characters that consists of letters, digits, underscores (_), and
dollar signs ($).
■ An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with
a digit.
■ An identifier cannot be a reserved word such as this, enum, break, do, etc. Reserved words
have specific meaning in the Java language. Keywords are reserved words.
■ An identifier can be of any length.

2. Variables
Variables are used to represent values that may be changed in the program.
Variables are for representing data of a certain type. To use a variable, you declare it by telling the
compiler its name as well as what 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:
datatype variableName;

Here are some examples of variable declarations:


int count; // Declare count to be an integer variable
double radius; // Declare radius to be a double variable
double interestRate; // Declare interestRate to be a double variable
These examples use the data types int and double. Later you will be introduced to additional data
types, such as byte, short, long, float, char, and boolean. If variables are of the same type, they
can be declared together, as follows:
datatype variable1, variable2, . . . , variablen;

The variables are separated by commas. For example,


int i, j, k; // Declare i, j, and k as int variables

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;

1
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

This is equivalent to the next two statements:


int count;
count = 1;

You can also use a shorthand form to declare and initialize variables of the same type together.
For example,
int i = 1, j = 2;

Every variable has a scope. The scope of a variable is the part of the program where the variable
can be referenced. The rules that define the scope of a variable will be gradually introduced later
in the course. For now, all you need to know is that a variable must be declared and initialized
before it can be used.

3. Assignment Statements and Assignment Expressions


An assignment statement assigns a value to a variable. An assignment statement can also 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:
int y = 1; // Assign 1 to variable y
double radius = 1.0; // Assign 1.0 to variable radius
int x = 5 * (3 / 2); // Assign the value of the expression to x
x = y + 1; // Assign the addition of y and 1 to x
double area = radius * radius * 3.14159; // Compute area

You can use a variable in an expression. A variable can also be used in both sides of the =
operator. For example,
x = x + 1;

In this assignment statement, the result of x + 1 is assigned to x. If x is 1 before the statement is


executed, then it becomes 2 after the statement is executed. To assign a value to a variable, you
must place the variable name to the left of the assignment operator. Thus, the following
statement is wrong:
1 = x; // Wrong
In Java, an assignment statement is essentially an expression that evaluates to the value to be
assigned to the variable on the left side of the assignment operator. For this reason, an

2
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

assignment statement is also known as an assignment expression. For example, the following
statement is correct:

[Link](x = 1);

which is equivalent to
x = 1;
[Link](x);
If a value is assigned to multiple variables, you can use chained assignments like this:
i = j = k = 1;

which is equivalent to
k = 1;
j = k;
i = j;

4. Named Constants
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. A constant is also known as a
final variable in Java.
pi is a constant. If you use it frequently, you don’t want to keep typing 3.14159; instead, you can
declare a constant for pi. Here is the syntax for declaring a constant:
final datatype CONSTANTNAME = value;

A constant must be declared and initialized in the same statement. The word final is a Java
keyword for declaring a constant. By convention, all letters in a constant are in uppercase.

A variable declared with final becomes a constant, which means unchangeable and read-only.
final int myNum = 15;
myNum = 20; // Error: cannot assign a value to final variable 'myNum'

5. Numeric Data Types and Operations


Java has six numeric types for integers and floating-point numbers with operators +, -, *, /, and
%.
Every data type has a range of values. The compiler allocates memory space for each variable or
constant according to its data type. Java provides eight primitive data types for numeric values,
characters, and Boolean values.

3
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

Java uses four types for integers: byte, short, int, and long. Choose the type that is most
appropriate for your variable. For example, if you know an integer stored in a variable is within a
range of a byte, declare the variable as a byte. For simplicity and consistency, we will use int for
integers most of the time in this course.
Java uses two types for floating-point numbers: float and double. The double type is twice as
big as float, so the double is known as double precision, and float as single precision. Normally,
you should use the double type, because it is more accurate than the float type.

6. Numeric Operators
The operators for numeric data types include the standard arithmetic operators: addition (+),
subtraction (–), multiplication (*), division (/), and remainder (%), as listed below. The operands
are the values operated by an operator.

When both operands of a division are integers, the result of the division is the quotient and the
fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields –2, not –2.5. To
perform a floating-point division, one of the operands must be a floating-point number. For
example, 5.0 / 2 yields 2.5.

4
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

The % operator, known as remainder, yields the remainder after division. The operand on the
left is the dividend, and the operand on the right is the divisor. Therefore, 7 % 3 yields 1, 3 % 7
yields 3, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.

7. Exponent Operations
The [Link](a, b) method can be used to compute ab. The pow method is defined in the
Math class in the Java API. You invoke the method using the syntax [Link](a, b) (e.g.,
[Link](2, 3)), which returns the result of ab (23). Here, a and b are parameters for the pow
method and the numbers 2 and 3 are actual values used to invoke the method. For example,
[Link]([Link](2, 3)); // Displays 8.0
[Link]([Link](4, 0.5)); // Displays 2.0
[Link]([Link](2.5, 2)); // Displays 6.25
[Link]([Link](2.5, –2)); // Displays 0.16

8. Augmented Assignment Operators


The operators +, -, *, /, and % can be combined with the assignment operator to form
augmented operators.
Very often, the current value of a variable is used, modified, then reassigned back to the same
variable. For example, the following statement increases the variable count by 1:
count = count + 1;

Java allows you to combine assignment and addition operators using an augmented (or
compound) assignment operator. For example, the preceding statement can be written as
count += 1;

The += is called the addition assignment operator. Table below shows other augmented
assignment operators.

5
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

9. Increment and Decrement Operators


The increment operator (++) and decrement operator (--) are for incrementing and
decrementing a variable by 1.
The ++ and -- are two shorthand operators for incrementing and decrementing a variable by 1.
These are handy because that’s often how much the value needs to be changed in many
programming tasks. For example, the following code increments i by 1 and decrements j by 1.
int i = 3, j = 3;
i++; // i becomes 4
j--; // j becomes 2

i++ is pronounced as "i plus plus" and i-- as "i minus minus." These operators are known as
postfix increment (or postincrement) and postfix decrement (or postdecrement), because the
operators ++ and -- are placed after the variable. These operators can also be placed before the
variable. For example,
int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2

++i increments i by 1 and --j decrements j by 1. These operators are known as prefix increment
(or preincrement) and prefix decrement (or predecrement). As you see, the effect of i++ and ++i
or i-- and --i are the same in the preceding examples. However, their effects are different when
they are used in statements that do more than just increment and decrement. Table below
describes their differences and gives examples.

10. Reading Input from the Console


Java uses [Link] to refer to the standard output device, and [Link] to the standard input
device. By default, the output device is the display monitor, and the input device is the keyboard.
To perform console output, you simply use the println method to display a primitive value or a
string to the console. To perform console input, you need to use the Scanner class to create an
object to read input from [Link], as follows:
Scanner input = new Scanner([Link]);

6
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

The syntax new Scanner([Link]) creates an object of the Scanner type. The syntax Scanner
input declares that input is a variable whose type is Scanner. The whole line Scanner input =
new Scanner([Link]) creates a Scanner object and assigns its reference to the variable
input. An object may invoke its methods. To invoke a method on an object is to ask the object to
perform a task. You can invoke the nextDouble() method to read a double value as follows:
double number = [Link]();

Below, an example of reading multiple inputs from the keyboard. The program reads three
numbers and displays their average.

The codes for importing the Scanner class (line 1) and creating a Scanner object (line 6) are the
same as in the preceding example, as well as in all new programs you will write for reading input
from the keyboard. Line 9 prompts the user to enter three numbers. The numbers are read in lines
10–12. You may enter three numbers separated by spaces, then press the Enter key, or enter each
number followed by a press of the Enter key, as shown in the sample runs of this program.

7
College of Information Technology October 5 2025
Department of Information Networks Prog. Lang. Lect. 2
Year 2, Course 1 Harith AK

11. Reading Numbers from the Keyboard


You know how to use the nextDouble() method in the Scanner class to read a double value
from the keyboard. You can also use the methods listed in Table 2.2 to read a number of the
byte, short, int, long, and float type.

Here are examples for reading values of various types from the keyboard:

If you enter a value with an incorrect range or format, a runtime error would occur. For example,
if you enter a value 128 for line 3, an error would occur because 128 is out of range for a byte
type integer.

You might also like