12

Click here to load reader

Variables - Value and Reference Type

Embed Size (px)

DESCRIPTION

Variables - Value and Reference Type, Non-Static Vs Static Classes

Citation preview

Page 1: Variables - Value and Reference Type

Software Programming and Fundamentals

Recap what we covered in week #1

Page 2: Variables - Value and Reference Type

Variable

• Variable: imagine like a box to store one thing (data)

• Eg: int age; age = 5;

Page 3: Variables - Value and Reference Type

Variable

• Variable: imagine like a box to store one thing (data)

• Eg: int age; age = 5;

int - Size of variable must be big enough to store Integer

Page 4: Variables - Value and Reference Type

Variable

• Variable: imagine like a box to store one thing (data)

• Eg: int age; age = 5;

int - Size of variable must be big enough to store Integer

age - Name of variable

age

Page 5: Variables - Value and Reference Type

Variable

• Variable: imagine like a box to store one thing (data)

• Eg: int age; age = 5;

int - Size of variable must be big enough to store Integer

age - Name of variable

age = 5 – Put a data 5 into the variable

age

5

Page 6: Variables - Value and Reference Type

Value Type Vs Reference Type

• Variables: two types– Value type (simple type like what you just saw)• Only need to store one thing (5, 3.5, true/false, ‘C’ and

“string”)

– Reference type (complex type for objects)• Need to store more than one thing (age + height + run()

+ … )

Page 7: Variables - Value and Reference Type

Reference Type

• Reference type (complex type for objects)• Eg: Human john; john = new Human();

Compare this to int age;age = 5;

Page 8: Variables - Value and Reference Type

Reference Type

• Reference type (complex type for objects)• Eg: Human john; john = new Human();

Human - Size of variable must be big enough to store an Address

Page 9: Variables - Value and Reference Type

john

Reference Type

• Reference type (complex type for objects)• Eg: Human john; john = new Human();

Human - Size of variable must be big enough to store an Address

john - Name of variable

Page 10: Variables - Value and Reference Type

Reference Type

• Reference type (complex type for objects)• Eg: Human john; john = new Human();

Human - Size of variable must be big enough to store an Address

john - Name of variable

john = new Human() – Get a house with enough space for john (age, height, etc)

john

D403

age

height

Page 11: Variables - Value and Reference Type

Non-Static Vs Static Class

• Non-Static: need New() to instantiate / create an object – like what you see just now

• Static: no need to use New() to use, there is just one copy of the class. This type of class basically to provide special functions for other objects

• So if you see a class being used without New(): it is a static class

• Eg Math class age = Math.Round(18.5); // Math rounding

Page 12: Variables - Value and Reference Type

Using Console Program for problem solving

• Console program:– Simple– Procedural (from top to bottom)– Getting inputs:

• From arguments: How? • From keyboard: How?

• Exercise 2.1 – 2.5– Language fundamental: int, double, string, Console.WriteLine(),

Console.Write(), Console.ReadLine(), int.Parse(), double.Parse(), simple if then statement

– Problem solving: repeating, swopping of 2 variables, simple sorting