Value Types and Reference Types

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Arrays.
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Pointers Prasun Dewan Comp 114.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Road Map Introduction to object oriented programming. Classes
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
Chapter 9 Introduction to Arrays
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
In Java.... Variables contain the values of primitive data types Value Types.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
More than just a song and dance.. They are objects that store primitive variable values: Integer – stores an int Double – stores a double Character –
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Arrays, cont MONDAY – APRIL 6, Review from lab Deep vs Shallow copy array1array array2 = array1; What happens?
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 9 Introduction to Arrays Fundamentals of Java.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Strings A String is a sequence of letters
Fundamentals 2.
Functions + Overloading + Scope
Topic: Classes and Objects
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Arrays (review) CSE 2011 Winter May 2018.
Java for Android is specific
INPUT STATEMENTS GC 201.
CMSC 202 Static Methods.
Unit-2 Objects and Classes
Computers & Programming Languages
Classes and Objects 5th Lecture
Arrays and Collections
Java Classes and Objects 3rd Lecture
Local Variables, Global Variables and Variable Scope
Sridhar Narayan Java Basics Sridhar Narayan
Parameter Passing in Java
Simple Classes in Java CSCI 392 Classes – Part 1.
Data Types Imran Rashid CTO at ManiWeber Technologies.
Programs and Classes A program is made up from classes
Java Programming Review 1
Arrays in Java.
Classes and Objects Static Methods
Java Programming Language
Java’s Central Casting
C Data Types and Variable
Chapter 3 Introduction to Classes, Objects Methods and Strings
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Subtype Substitution Principle
Lec 17 Using Nested Loops and Objects in an Applet Class
Presentation transcript:

Value Types and Reference Types In Java . . . Value Types and Reference Types

Value Types Variables contain the values of primitive data types

Value types of variables contain their values In Java, primitive data types such as int, float, double, char, and boolean are called value types A variable of one of these types is a container for one value of its appropriate type counter price foundIt 7 5.99 false Value types of variables contain their values

Value Types Because a variable of a value type is a container for ONE value of the corresponding type, giving it a second value replaces the first value entirely with the second dependents 2 dependents 2 3

Reference Types in Java Objects of Classes

Reference Types A variable declared as a reference to an object of some class (either a built-in class or a user-defined class) is a reference type A reference variable DOES NOT contain the the object to which it refers – instead, it refers to that object (i.e., it points to that object) A reference variable is null (refers to nothing) until it is given an object for it to reference Actual Object Reference

Reference Types personA null personB null Called a shallow copy because only the reference is copied new creates an actual object of type Person initialized to Rachel Walling Rachel Walling personA personB Two different references now refer to a single actual object

Reference types Just as value types may only have one value at a time, reference types may only refer to one actual instance at a time Bob Smith After line 5 bob

Reference types As value types may only have one value at a time, reference types may only refer to one actual instance at a time Since nothing refers to this object now, it is destroyed (“garbage collected”) by Java Bob Smith bob Bob Jones After line 7