Reference Data Types - Object Oriented Programming PDF
Document Details

Uploaded by WellPositionedCarnelian7944
NU East Ortigas
Bernardith Batoy-Cruz
Tags
Summary
This module explores reference data types in Java, covering local variables and call by value. It also describes the difference between primitive and reference data types. The module uses the logo of 'NU East Ortigas'.
Full Transcript
OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Module 2 Reference Data Types Course Facilitator: Engr. Bernardith Batoy-Cruz OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Objectives: After studying this module, students should be ab...
OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Module 2 Reference Data Types Course Facilitator: Engr. Bernardith Batoy-Cruz OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Objectives: After studying this module, students should be able to: Introduce to reference data types, local variables, this keyword, and call by value. Describe the difference between primitive and reference data types. Be able to differentiate between: object reference and primitive variable, object reference and object in memory. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Primitive Data Types char char OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Primitive Data Types Primitive types are byte, char, int, long, double, short, and boolean. When it comes to initialization of primitive types, they are initialized by default for example boolean is initialized to false and the others are initialized to 0. They also can store one value of their declared type. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) 8 Types of Primitive Data Types boolean data type byte data type char data type short data type int data type long data type float data type double data type OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Reference Data Types OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Reference Data Types Beyond all primitive types are reference data types. A reference variable contains the address of the object and not the object itself. The object’s address is assigned by the operating system during the object instantiation stage commonly known as the creation stage; therefore hard-coding the object’s address in your source code is not possible. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Primitive Types in Memory Reference Types in Memory OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Both Primitive and Reference Types in Memory OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Following are the reference types in Java: class types − This reference type points to an object of a class. array types − This reference type points to an array. interface types − This reference type points to an object of a class which implements an interface. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Once we create a variable of these types (i.e. when we create an array or object, class or interface). These variables only store the address of these values. Default value of any reference variable is null. A reference variable can be used to refer any object of the declared type or any compatible type. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Reference vs Primitive Data Types OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Variables A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type. A variable is the name of a reserved area allocated in memory. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Types of Variables OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Example: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Local Variables in Java Variables declared within a scope will exist only within the scope where they have been declared in. It can be declared in a method or as an argument of a method. Requires explicit initialization. Automatically created when you enter the method and automatically destroyed when you exit the method. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Call by Value If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method. When you pass a value to a method, you are passing the variable’s value. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Example: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Passing Object as a Value: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) this keyword in Java In Java, this is a reference variable that refers to the current object. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) this to refer current class instance variable The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Not using the keyword this OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Using the keyword this to refer current class instance variable. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) this to invoke current class method You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) this to invoke current class constructor The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Calling default constructor from parameterized constructor: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Calling parameterized constructor from default constructor: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) References: https://www.javatpoint.com/static-keyword-in- java https://www.tutorialspoint.com/java/index.html https://www.developer.com/java/