1 / 12

IB Computer Science Unit 1 – Introduction to Java Variables

IB Computer Science Unit 1 – Introduction to Java Variables. Variable Properties. Creating a Variable in Java. The Assignment Operator. Constant Variables. Variable Properties. A Variable is a named container that stores a specific piece of data.

Download Presentation

IB Computer Science Unit 1 – Introduction to Java Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Variable Properties 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: Integer Character Double Byte Float Long Boolean Short

  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: • Which variable are we changing • 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: Any problems with this?? int userAge; int userInput; userInput = JOptionPane.showInputDialog(“AGE:”); userAge = userInput;

  11. Constant Variables There are many instances in a program where the value of a variable never changes For example, a variable called dblPi might be assigned the value of dblPi = 3.14159 In these cases, it is best practice to use a special kind of variable called a Constant. 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

More Related