Presentation is loading. Please wait.

Presentation is loading. Please wait.

Phil Tayco Slide version 1.0 Created Sep 10, 2017

Similar presentations


Presentation on theme: "Phil Tayco Slide version 1.0 Created Sep 10, 2017"— Presentation transcript:

1 Phil Tayco Slide version 1.0 Created Sep 10, 2017
Simple Objects Phil Tayco Slide version 1.0 Created Sep 10, 2017

2 Primitive Data Types All variables are defined with a data type and variable name int age = 21; double gpa = 3.9; Memory for age and gpa is instantly allocated and contents are initialized Process is automatically done because the language already knows these simple data types All variables in memory have a location, but knowing exactly where they are is not important for primitives Advantages arise when knowing how these memory locations can be used age 21 gpa 3.9

3 Memory References Memory locations can be referred to by other variables (in C/C++, this is called a “pointer”) A memory reference is essentially a variable that contains a memory address as its value The value refers to a memory location where information is stored When memory references are defined, the location it is pointing to must be reserved (a.k.a. memory must be “allocated”) Modern programming languages have “no pointers”, but in fact they are handling a lot of the pointer process automatically for you C and C++ gives you more memory management control, which can be both a blessing and a curse!

4 Memory References Memory references are useful when handling complex data In Java, memory reference management is automatically done by default when you use data types that are not primitive Exception: String data types are technically treated as complex data types, but can be used the same way as primitives in many ways Java is often referred to as a language with “no pointers”, but in fact it does use memory reference management…it just does a lot of the hard work for you Nevertheless, it is important to know the process that occurs behind the scenes as it will become useful to understand later with advanced concepts

5 Memory References When we declare a variable with a complex data type, the memory location it is referring to is yet to be defined Student me; me ?

6 Memory References To allocate the memory, we request it with the “new” keyword Student me = new Student(); When the compiler sees “new Student()” it uses what it knows a Student looks like and looks in memory for a location large enough to store that data 0x1234abcd is an example of a numeric location where the Student data could reside “me” is the variable that now refers to the location of the Student data. In Java, this is transparent to us, but we should know that this structure exists me 0x1234abcd (Student data)

7 Complex Data Types But how does Java know what “a Student looks like”?
Integers and doubles are simple data types because they store one value. (age stores 21 and gpa stores 3.9) Conceptually, a Student can store much more types of information. Think of every Student that you may want to model in a program. Every Student has: First name Last name Age Gpa ID Each one of these elements of a Student is called a “property” (also called an “attribute”)

8 Complex Data Types These properties make up the template for defining a Student In the example, we declared a variable “me” whose data type is “Student” (this is the same as when we declared a variable “age” whose data type is an integer) We assigned age a value of 21 after we declared it. Similarly, we will want to assign values to me’s properties This means Student’s properties are like variables grouped together within (or “encapsulated”) the overall Student variable (“me”) me has its own first name, last name, ID, etc. As such, each property is defined like a variable

9 Complex Data Types public class Student { public String firstName; public String lastName; public int age; public int id; public double gpa; } These properties are defined in a separate Java file called a “class” The file is separate because a class is designed to be a “stand-alone” data type. Put another way, the class is intended to be used by any number of other programs

10 Complex Data Types In another program (such as in the “main” function), we can assign values to these properties for the “me” variable In memory, technically the location where the Student data was allocated is found through the memory address – a process called “dereferencing” Student me = new Student(); me.id = 1234; me.age = 21; me.firstName = “Phil”; me 0x1234abcd firstName “Phil” lastName id 1234 age 21 gpa

11 Complex Data Types You can define as many different variables defined with primitive data types as memory allows (int x, int y, int z, etc.) Similarly, you can define as many different variables with classes (Student me, Student you, Student[] roster, etc.) Variables with primitive types are commonly referred to as just variables. A variable whose data type is a class is called an “object” Objects are seen as “instances” of a class. Creating objects is also called “instantiation” Object-oriented programming is the study of designing solutions primarily through the manipulation of such objects (variables with complex data types)

12 Exercise 1 Design a simple class with 4 properties. Remember to think of a property as an attribute of your class. Ask yourself “what properties does every instance of this class have?” Create a program with 2 files One file must implement your class design The other file is the main program that creates 2 objects of the class In your main program, demonstrate using the objects by assigning and displaying different values of each object’s properties You can be as simple or as clever as you like with your program as long as you show using class design and object creation and management


Download ppt "Phil Tayco Slide version 1.0 Created Sep 10, 2017"

Similar presentations


Ads by Google