Pass by value and pass by reference

turntotech 3,919 views 8 slides Jul 26, 2016
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Reference and Value Types


Slide Content

Pass by value and pass by reference

Example

Example 2 website value reference

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)

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

Value types Java PrimitiveType ( Numeric Type 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.

Reference Types Java Anything that inherits from Object Objective-C Anything that inherits from NSObject Swift Classes

Note: In Java, references to object are passed by value (e.g. a new reference is created when passed)