CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.

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

1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Fundamental Programming Structures in Java: Strings.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Fall 2013.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Lecture 04 – Models of memory Mutable and immutable data.
Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Peyman Dodangeh Sharif University of Technology Spring 2014.
More than just a song and dance.. They are objects that store primitive variable values: Integer – stores an int Double – stores a double Character –
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
CSC 142 Computer Science II Zhen Jiang West Chester University
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Declaring console static and global import java.util.*; public class Test { static Scanner console = new Scanner (System.in); public static void main(String[]
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 7 User-Defined Methods.
Methods and Parameters
Computers & Programming Languages
Principles of Computer Programming (using Java) Chapter 2, Part 1
Unit-2 Objects and Classes
An Introduction to Java – Part II
Object-Oriented Programming
Parameter Passing in Java
Building Java Programs
Reference semantics, variables and names
Java Programming Review 1
CSC 142 Arrays [Reading: chapter 12].
CSC 111 Exam 3 Review Fall 2005.
Variables and Computer Memory
Names of variables, functions, classes
A+ Computer Science PARAMETERS
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

CSC 142 F 1 CSC 142 References and Primitives

CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object is defined by a class, e.g. Car myCar = new Car("sedan"); //myCar is a reference to an object of //type Car  Primitive: a variable to store a simple piece of information, such as an integer, a floating point number, a character, or a boolean. The type is defined by one of the java keywords, int, double, char, boolean, etc… int i = 10; // don't use new

CSC 142 F 3 Memory view  All program variables are stored in memory. Think of memory as a huge pile of boxes. Each box is known to the computer by its location (=address).  The memory box of a reference variable contains the memory address where the object that the variable refers to is stored. myCar object address memory for myCar Car object referred to by the variable myCar memory for the myCar object  The memory box of a primitive variable contains the value of the variable i 10 memory for i

CSC 142 F 4 Method call  When calling a method, the values of the actual parameters are copied in the formal parameters. We say that java calls by value.  If the actual parameter is  a primitive: the value of the primitive is copied  a reference: the value of the reference is copied (=address of the object). Actual and formal parameters refer to the same object.

CSC 142 F 5 Example // foo method public void foo(StringBuffer sb, int j){ sb.reverse();//reverse the order j = 2*j; } // in some method bar calling foo int i = 10; StringBuffer s = new StringBuffer("Hello"); foo(s,i); System.out.println("s="+s+" i="+i);  What is printed? Like the String class. But a StringBuffer comes with methods to modify its content (the content of a String can't be modified. String are immutable). s=olleH i=10

CSC 142 F 6 What is going on? i s bar j sb foo Hello StringBuffer 10 1AEF address of the StringBuffer object 10 1AEF copy the actual parameters in the formal parameters 20 X olleH X

CSC 142 F 7 Using references and primitives  An object can be huge in memory  passing an object to a method by copying the object would be prohibitively expensive  A primitive never uses lots of memory. It can be passed as a copy.  Note: a primitive in one method can't be modified by another method when passed as an actual parameter to that other method.  But sometimes we want to do so!  Answer: store the primitive inside of an object (e.g. an array) and call the method with a reference to that object as an actual parameter.