Basic HTML version of Foils prepared 18 May 97

Foil 51 By value and By reference

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. by Nancy J. McCracken,Geoffrey C. Fox


1 Many languages are confusing as they differ in often unstated distinction between the value and "handle" -- Java is no exception! (reference,address,pointer) of an entity
2 Consider assignment: a = b; // sets value of a to value of b
3 If a and b are primitive types, then they hold "actual literals" and so if b=66, then a is set to 66
  • In this case if you change b, then a is left unchanged
4 However if a or b is an object, b is in fact a reference and so one sets a to refer to same object as b (i.e. same "location" in memory)
  • if you change b in some way, then a will be changed accordingly
  • Note null is value of an object which has not been assigned (constructed) and so does not point anywhere
5 Arguments to Methods are always passed by value BUT if an object is an argument, then that value is ALWAYS a reference and so in practice
  • Primitive types are passed by value
  • Objects are passed by reference
6 Arrays reflect properties of what they are arrays of!

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Thu Jan 8 1998