lec9 - CSE 8A: Lecture 9 More on objects vs. primitive...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 33 CSE 8A, UCSD LEC 9 CSE 8A: Lecture 9 More on objects vs. primitive types Variable visibility and lifetime Classes and ADT’s (Reading: Savitch, Ch. 4 and 5)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 33 CSE 8A, UCSD LEC 9 Review: reference types, primitive types, and objects in Java When you declare a variable of primitive type in Java, memory for the variable is allocated; how much memory depends on the type double d; // this creates a 64-bit floating point variable When you declare a variable of reference type in Java, memory for a “pointer” or “handle” or “reference” to an instance of the type is allocated; this pointer holds a memory address Circle c; // this does NOT create a Circle object! It only // creates a pointer which can point to a Circle object To actually create an instance of a class (an object) in Java, you use the operator new c = new Circle(); // creates a Circle, address stored in c (Strings are somewhat special: evaluating a string literal constant, or invoking the String concatenation operator, creates a String object. .. but you can also use new )
Background image of page 2
Page 3 of 33 CSE 8A, UCSD LEC 9 Dereferencing pointers and assigning pointers A variable declared to be of reference type is just a pointer; it is not an actual object Circle c; // create a Circle pointer, not a Circle object You create an object with new and a constructor call. The pointer value returned by new can be assigned to a pointer variable, which then can be used to refer to the object c = new Circle(); // create a Circle object, make c point to it The dot operator . takes 2 arguments, a pointer and a member name. It dereferences the pointer (goes to the object pointed to by the pointer) and accesses the named member of that object c.setRadius(100.0); The assignment operator = does not dereference pointer arguments! It does not copy the object pointed to! It copies and assigns the pointer value itself, which is just the memory address of an object Circle b; // create another Circle pointer b = c; // copy memory address in pointer c and store it in b // this makes b and c point to the same Circle object // for now, but b and c are still separate pointer // variables
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 of 33 CSE 8A, UCSD LEC 9 Primitive type variables vs. reference type variables Primitive type variables are not objects in Java, and they behave differently in many contexts A variable declared to be of primitive type is an actual variable of that type double d; // create a double variable A declaration creates a primitive type variable. You cannot create a primitive type variable using new d = new double(); // error! The dot operator cannot be used with a primitive type variable d.anything; // error! The assignment operator
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 33

lec9 - CSE 8A: Lecture 9 More on objects vs. primitive...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online