Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

11
Variables and Constants Objectives To understand Identifiers, Variables, and Constants

Transcript of Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Page 1: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Variables and Constants Objectives

To understand Identifiers, Variables, and Constants

Page 2: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

IdentifiersYou need to give a name to something that you will refer to

in your program – variables, constants, methods, classes, packages

An identifier is a sequence of characters that consist 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. (See Appendix A, “Java Keywords,” for a list of reserved words).

An identifier cannot be true, false, ornull.

An identifier can be of any length. Examples: $2, computeArea, area, radius, showMessageDialog

Java is case sensitive. An x and X are different variables

Page 3: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

VariablesVariables are used to store data input,data output, or intermediate data // Compute the first arearadius = 1.0;area = radius * radius * 3.14159;System.out.println("The area is “ + area + "

for radius “ + radius);

// Compute the second arearadius = 2.0;area = radius * radius * 3.14159;System.out.println("The area is “ + area + "

for radius “ + radius);

Note the + symbol is a concatenation operator

Page 4: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Declaring VariablesVariables are used for representing data of a

certain type. To use a variable, you must first declare it.

int x; // Declare x to be an // integer variable;

double radius; // Declare radius to // be a double variable;

char a; // Declare a to be a // character variable;

Page 5: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Assignment StatementsAfter a variable is declared, you can assign a

value to it using the assignment statement

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

Page 6: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Declaring and Initializinga Variable in One Step

int x = 1;

double d = 1.4;

float f = 1.4;

Is this statement correct?

If incorrect, what should it be?

Page 7: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Note: In an assignment statement, the data type of the variable on the left must be compatible with the data type on the right

int x = 1.4; //double assigned

// to an int

Is this statement correct?

If incorrect, what should the statement be?

Page 8: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Tip:

A variable must be declared before it can be assigned a value.

A variable declared in a method (local variable) must be assigned a value before it is used

Whenever possible declare a variable and assign its initial value in one step.

int x = 1;

Page 9: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Constants

The value of a variable may change

during the execution of the program.

But a constant represents permanent

data that never change.

final datatype CONSTANT_NAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;

Page 10: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Constants

Caution

A constant must be declared and initialized

before it can be used. You cannot change a

constant’s value once it is declared.

By convention, constants are named in upper

case: PI, not pi or Pi

Page 11: Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.

Benefits of Constants

There are three benefits of using constants: You don’t have to repeatedly type the same

value The value can be changed in a single

location, if necessary The program is easier to read