Presentation is loading. Please wait.

Presentation is loading. Please wait.

IB Computer Science Unit 1 – Introduction to Java Variables.

Similar presentations


Presentation on theme: "IB Computer Science Unit 1 – Introduction to Java Variables."— Presentation transcript:

1 IB Computer Science Unit 1 – Introduction to Java Variables

2 Creating a Variable in Java The Assignment Operator Constant Variables Variable Properties

3 A Variable is a named container that stores a specific piece of data Every Variable has three main properties: Name Type Contents

4 Variable Properties Name Every variable must have a name A variable name should be descriptive (unlike variables in Mathematics which are usually single letters) Variable names should start with lower case letters and cannot contain spaces More on variable naming conventions later…

5 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: IntegerCharacter DoubleByte FloatLong BooleanShort

6 Variable Properties Content Every variable stores some data, this is commonly referred to as the 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 as declaring a variable.

7 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 Integer that represents the users age, we would enter: int userAge; Note: At this point, the contents or value of the variable userAge is null.

8 The Assignment Operator The Assignment Operator is one of the most commonly used operators in Computer Science It is represented by the mathematical equals symbol: = However, this equals symbol is used differently than in Mathematics, which uses 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 by assigning a new value to it

9 The Assignment Operator We need to be aware of two things: i)Which variable are we changing ii)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…

10 The Assignment Operator For example, if we wish to assign the value of 17 to the variable we created called 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??

11 Constant Variables There are many instances in a program where the value of a variable never changes In these cases, it is best practice to use a special kind of variable called 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 and it does not change values throughout the time the program is running To distinguish constants from regular variables, we use all capital letters to name them dbl_INTEREST_RATE is an example of a possible name for a constant variable

12 Constant Variables Why use constant variables at all? To avoid hard-coded “magic” numbers which makes a program difficult to trace and debug So that changes to a constant value only need to be done in one place Programming style


Download ppt "IB Computer Science Unit 1 – Introduction to Java Variables."

Similar presentations


Ads by Google