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. 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. 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. Reference Types
Java Anything that inherits from Object
Objective-C Anything that inherits from NSObject
Swift Classes
8. Note:
• In Java, references to object are passed by value (e.g.
a new reference is created when passed)