+ All Categories
Home > Documents > JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a...

JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a...

Date post: 14-Dec-2015
Category:
Upload: seth-solley
View: 222 times
Download: 2 times
Share this document with a friend
23
JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings
Transcript
Page 1: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

JAVAVariables Declaring VariablesOver writing VariablesData types Displaying contents of a Variable Naming a Variable Strings

Page 2: 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 storage to store values

RAM is used to store these temporary values

These small memory locations are called variables.

Page 3: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

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.

Page 4: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Example Lets say we need to store three

pigeons we need to create two pigeon holes (variables) called P1which is set to hole Billy, P2 set to hold Henry and P3 to hold Milly .

P1 P2 P3

Billy Henry Milly

NOTE: A variable can only hold one value, you cannot have two pigeons in one pigeon hole

Page 5: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

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

type variable_name;

Page 6: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Example Lets say we want to declare to

variables to hole numbers

INT INT

N1 N2

int N1;int N2;

NOTE: Type int can only hold integers. We will learn different data types soon.

Page 7: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

class VariablesExample { public static void main (String

args[]){ //variables are declared int N1; int N2; int tot;//assign values to variables N1 = 50; N2 = 13; //the total of variables N1 and N2 //is stored in tot tot = N1 + N2; }

}

The memory locations for three variables are created. These can only store whole numbers.

Numbers are stored in the variables

Instead of storing a particular value in tot, the sum of N1 and N2 is stored.

Copy this program

Page 8: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

What we have …

50 13 63

N1 N2 tot

Page 9: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Overtyping If we take the example pf before

where we have the following variables

1. N1 = 502. N2 = 13

If we change N1 to hold the number 33 (N1 = 33) then the integer 50 will be overwritten but 33 and the 50 will be lost

Page 10: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Direct initialisation We can decide to set a value to

the variables as soon as we create them

This is call Direct initialisation

The program would work the same it would just be shorter

Page 11: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

class VariablesExample { public static void main (String

args[]){ //variables are declared are

assigned int N1 = 50; int N2 = 13; int tot; //the total of variables N1 and N2 //is stored in tot tot = N1 + N2;

} }

N1 and N2 are declared as int and assigned a value immediately.

Copy this program

Page 12: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Data Types – Integers The following are examples of

integer data types you need to know

Page 13: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Data Types – Real (numbers)The following are examples of

real data types you need to know

Page 14: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Data Types – The following are examples of

some other data types you need to know

Page 15: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Important information When assigning a value to a

character variable, the character to be assigned must be enclosed in single quotes.

class CharEx { public static void main (String

args[]){ char label; label = ‘B’;

} }

Copy this program

Page 16: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

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 BooleanEx { public static void main (String

args[]){ boolean canDrive; canDrive=false;

} }

Copy this program

Page 17: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Displaying Contents of a Variable We print the contents of a

variable using System.out.ptintln()

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

System.out.println(variable_name)

Page 18: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Example

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

Output = letter

char letter; letter = ‘C’; System.out.println(letter);

Output = C

Page 19: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

Activity Go back to the programs we

have writing and output the contents of the variables

Page 20: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

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 CamelCase)It must reflect the contents that it

will store

Page 21: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

StringsIn 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

Page 22: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

ExampleLets say we want to store out name

To output a string we use System.out.print()

To combine a String with more text, the + operator is used

String fullName = “Ruby Borg”;

System.out.println(fullName);

System.out.println (“The next person is “+fullName);

Page 23: JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.

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


Recommended