JAVA Variables Declaring Variables Over writing Variables Data

  • Slides: 23
Download presentation
JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable

JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings

What is a Variable? �When a Java program is running, it would need memory

What is a Variable? �When a Java program is running, it would need memory storage to store values �RAM is used to store these temporary values �These small memory locations are called variables.

How do Variables work? � As programmers we can create variables that have a

How do Variables work? � As programmers we can create variables that have a particular data type to store values �All the different variables have to have unique names �If we imagine the RAM as a set of pigeon holes, to put a pigeon in its pigeon hole we would need to give the hole a name and then the pigeon/data which it can home must be set/declared.

Example Lets say we need to store three pigeons we need to create two

Example Lets say we need to store three pigeons we need to create two pigeon holes (variables) called P 1 which is set to hole Billy, P 2 set to hold Henry and P 3 to hold Milly. � Billy NOTE: A variable can only hold one value, you cannot have two pigeons in one pigeon hole P 1 Henry P 2 P 3 Milly

Declaring a Variable �Before using a we have to declare/set it � When declaring

Declaring a Variable �Before using a we have to declare/set it � When declaring a variable we must include two aspects; 1. Variable name 2. Variable data type variable_name;

Example �Lets say we want to declare to variables to hole numbers N 2

Example �Lets say we want to declare to variables to hole numbers N 2 N 1 NOTE: Type int can only hold integers. We will learn different data types soon. INT int N 1; int N 2;

class Variables. Example { public static void main (String args[]){ //variables are declared The

class Variables. Example { public static void main (String args[]){ //variables are declared The memory locations for three int N 1; variables are created. These can int N 2; only store whole numbers. int tot; //assign values to variables N 1 = 50; Numbers are stored in the N 2 = 13; variables //the total of variables N 1 and N 2 //is stored in tot = N 1 + N 2; Instead of storing a particular value in tot, the sum of N 1 and } N 2 is stored. } Copy this program

What we have … N 1 50 N 2 13 tot 63

What we have … N 1 50 N 2 13 tot 63

Overtyping �If we take the example pf before where we have the following variables

Overtyping �If we take the example pf before where we have the following variables 1. N 1 = 50 2. N 2 = 13 � If we change N 1 to hold the number 33 (N 1 = 33) then the integer 50 will be overwritten but 33 and the 50 will be lost

Direct initialisation �We can decide to set a value to the variables as soon

Direct initialisation �We can decide to set a value to the variables as soon as we create them �This �The is call Direct initialisation program would work the same it would just be shorter

class Variables. Example { public static void main (String args[]){ //variables are declared are

class Variables. Example { public static void main (String args[]){ //variables are declared are assigned N 1 and N 2 are declared as int N 1 = 50; and assigned a value int N 2 = 13; immediately. int tot; //the total of variables N 1 and N 2 //is stored in tot = N 1 + N 2; } } Copy this program

Data Types – Integers �The following are examples of integer data types you need

Data Types – Integers �The following are examples of integer data types you need to know

Data Types – Real (numbers) �The following are examples of real data types you

Data Types – Real (numbers) �The following are examples of real data types you need to know

Data Types – �The following are examples of some other data types you need

Data Types – �The following are examples of some other data types you need to know

Important information �When assigning a value to a character variable, the character to be

Important information �When assigning a value to a character variable, the character to be assigned must be enclosed in single quotes. class Char. Ex { public static void main (String args[]){ char label; label = ‘B’; } } Copy this program

Important information �When assigning a value to a boolean variable, we can only store

Important information �When assigning a value to a boolean variable, we can only store true or false; these are not to be placed in single quotes. class Boolean. Ex { public static void main (String args[]){ boolean can. Drive; can. Drive=false; } } Copy this program

Displaying Contents of a Variable �We print the contents of a variable using System.

Displaying Contents of a Variable �We print the contents of a variable using System. out. ptintln() System. out. println(variable_name) �In order to print the contents of a variable it is important that the variable name is not placed inside quotes � If we put the variable name in quotes the name of the variable will appear

Example char letter; letter = ‘C’; System. out. println(“letter”); Output = letter char letter;

Example char letter; letter = ‘C’; System. out. println(“letter”); Output = letter char letter; letter = ‘C’; System. out. println(letter); Output = C

Activity �Go back to the programs we have writing and output the contents of

Activity �Go back to the programs we have writing and output the contents of the variables

Giving Variables a Name These are some of the things to remember: �It must

Giving Variables a Name These are some of the things to remember: �It must start with a lowercase letter, �It cannot be a keyword used in Java, �It must be unique, �It cannot contain blank spaces (use Camel. Case) �It must reflect the contents that it will store

Strings �In some programs we would want to store text � The char data

Strings �In some programs we would want to store text � The char data type cannot be used since it can only store one character �We use what is called a String which stores characters, numbers, a mixture of both and also symbols

Example �Lets say we want to store out name String full. Name = “Ruby

Example �Lets say we want to store out name String full. Name = “Ruby Borg”; �To output a string we use System. out. print() System. out. println(full. Name); �To combine a String with more text, the + operator is used System. out. println (“The next person is

Activity �Create a program to hold and output your name and class. �Out put

Activity �Create a program to hold and output your name and class. �Out put should be as follows My name is Ruth Borg I am in form 4 One