Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pass by value and pass by reference

Reference and Value Types

  • Login to see the comments

Pass by value and pass by reference

  1. 1. Pass by value and pass by reference
  2. 2. Example
  3. 3. Example 2 website
  4. 4. Pass by value • Creates a copy of the value when passed to a function • Better in multi-threaded environment • Required in distributed and remote systems. • Simple memory management • In some implementations, takes less memory than reference types because there is no pointer, inherited properties. • Some languages reduce memory overhead of copies by using copy-on-write (Swift)
  5. 5. Pass by reference • No copying of data (only creation of a pointer) which can means less memory • Many functions/classes can share a single object • Memory must be managed (reference counting or garbage collection) • Can cause problems with multithreading • Can return ‘multiple values’ from a function
  6. 6. Value types Java PrimitiveType (NumericType and boolean) Booleans, integers and floats (of their various widths) *broad generalization Objective-C C scalars (int, float, char) Objective-C scalars (NSInteger, CGFloat, BOOL) C structures (NSRange, CLLocationCoordinate2D) Swift Almost all built-in types including Dictionary, Array and String. In Swift 2, 87 of 99 types are value types.
  7. 7. Reference Types Java Anything that inherits from Object Objective-C Anything that inherits from NSObject Swift Classes
  8. 8. Note: • In Java, references to object are passed by value (e.g. a new reference is created when passed)