IB Computer Science Unit 1 – Introduction to Java Variables.

12
IB Computer Science Unit 1 – Introduction to Java Variables

Transcript of IB Computer Science Unit 1 – Introduction to Java Variables.

Page 1: IB Computer Science Unit 1 – Introduction to Java Variables.

IB Computer Science

Unit 1 – Introduction to Java

Variables

Page 2: IB Computer Science Unit 1 – Introduction to Java Variables.

Creating a Variable in Java

The Assignment Operator

Constant Variables

Variable Properties

Page 3: IB Computer Science Unit 1 – Introduction to Java Variables.

Variable Properties

A Variable is a named container that stores a specific piece of data

Every Variable has three main properties:

Name

Type

Contents

Page 4: IB Computer Science Unit 1 – Introduction to Java Variables.

Variable Properties

Name

Every variable must have a name

A variable name should be descriptive (unlike variables in Mathematicswhich are usually single letters)

Variable names should start with lower case letters and cannot contain spaces

More on variable naming conventions later…

Page 5: IB Computer Science Unit 1 – Introduction to Java Variables.

Variable Properties

Type

Every variable has a specific data type

Since the programmer creates every variable for a purpose, he/she should know in advance what type of variable to create

Basic variable types are called Primitive Data Types

In Java, the Primitive Data Types are:

Integer CharacterDouble ByteFloat LongBoolean Short

Page 6: IB Computer Science Unit 1 – Introduction to Java Variables.

Variable Properties

Content

Every variable stores some data, this is commonly referred to asthe variables value, or contents.

However, before we can store and use the contents of a variable,we must first create the variable in our program. This is known asdeclaring a variable.

Page 7: IB Computer Science Unit 1 – Introduction to Java Variables.

Creating a Variable in Java

To create a variable in Java, enter the keyword for its TYPE, followed by the variable name, and then a semi-colon.

For example, if we want to declare a variable of type Integerthat represents the users age, we would enter:

int userAge;

Note: At this point, the contents or value of the variable userAge is null.

Page 8: IB Computer Science Unit 1 – Introduction to Java Variables.

The Assignment Operator

The Assignment Operator is one of the most commonly used operators inComputer Science

It is represented by the mathematical equals symbol: =

However, this equals symbol is used differently than in Mathematics, whichuses it exclusively to indicate that two sides of an equation are the same

In Computer Science, the = is used to change the contents of a variable byassigning a new value to it

Page 9: IB Computer Science Unit 1 – Introduction to Java Variables.

The Assignment Operator

We need to be aware of two things:

i) Which variable are we changingii) What value are we changing the variable to

The LEFT side of the = tells us the variable being changed

The RIGHT side of the = tells us the new value being assigned

Note: The right side of the = can be represented in a number of ways:a raw value, another variable, an equation, etc…

Page 10: IB Computer Science Unit 1 – Introduction to Java Variables.

The Assignment Operator

For example, if we wish to assign the value of 17 to the variable we createdcalled userAge, we would enter the following code:

userAge = 17;

If we wish to assign the value of userAge to be retrieved from the user,we could use the JOptionPane to get the users input:

userAge = JOptionPane.showInputDialog(“AGE:”);

Or we could use a second variable:

int userAge;int userInput;userInput = JOptionPane.showInputDialog(“AGE:”);userAge = userInput;

Any problems with this??

Page 11: IB Computer Science Unit 1 – Introduction to Java Variables.

Constant Variables

There are many instances in a program where the value of a variablenever changes

In these cases, it is best practice to use a special kind of variablecalled a Constant.

For example, a variable called dblPi might be assigned the value of dblPi = 3.14159

When we declare a constant variable, we set its value in our code andit does not change values throughout the time the program is running

To distinguish constants from regular variables, we use all capital lettersto name them

dbl_INTEREST_RATE is an example of a possible name fora constant variable

Page 12: IB Computer Science Unit 1 – Introduction to Java Variables.

Constant Variables

Why use constant variables at all?

To avoid hard-coded “magic” numbers which makes a program difficult totrace and debug

So that changes to a constant value only need to be done in one place

Programming style