Java, Java, Java: Object-Oriented Problem Solving by R. Morelli & R. Walde
Author:R. Morelli & R. Walde
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2018-05-08T22:00:00+00:00
9.5.4 Passing a Value and Passing a Reference
Recall from Chapter 3 that when an Object is passed to a method, a copy of the reference to theObject is passed. Because an array is an object, a reference to the array is passed toinsertionSort(), rather than the whole array itself. This is in contrast to how a value of a primitive type is passed. In that case, a copy of the actual value is passed.
Passing a primitive value
Passing an object JAVA LANGUAGE RULE Primitive vs. Object Parameters. When a value of a primitive data type—int, double, char, boolean— is passed as an argument to a method, a copy of the value is passed; when a reference to anObject is passed, a copy of the reference is passed.
One implication of this distinction is that for arguments of primitive type, the original argument cannot be changed from within the method because the method has only a copy of its value. For example, the following method takes anint parameter n, which is incremented within the method:
¨ p u b l i c void add1 ( i n t n ) {
System . out . p r i n t ( ”n = ” + n ) ;
n = n + 1 ;
System . out . p r i n t l n ( ” , n = ” + n ) ;
}
© But because n is a parameter of primitive type, incrementing it within the method has no effect on its associated argument. Thus, in the following segment, the value of Num—n’s associated argument—will not be affected by what goes on inside theadd() method. The output produced by the code segment is shown in the comments:
¨ i n t Num = 5 ;
System . out . p r i n t l n ( ”Num = ” + Num) ; / / P r i n t s Num = 5 add1 (Num ) ; / / P r i n t s n = 5 , n = 6 System . out . p r i n t l n ( ”Num = ” + Num) ; / / P r i n t s Num = 5
© Note that while n’s value has changed inside the method, Num’s value remains unaffected.
The case is much different when we pass a reference to an object. In that case, the object itself can be manipulated from within the method. TheinsertionSort() method is a good illustration. In the following code segment, the arrayanArr is printed, then sorted, and then printed again:
¨ S o r t s o r t e r = new S o r t e r ( ) ;
i n t anArr [ ] = { 5 , 1 0 , 1 6 , −2 , 4 , 6 , 1 };
s o r t e r . p r i n t ( anArr ) ; / / P r i n t s 5 1 0 1 6 −2 4 6 1 s o r t e r .
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12571)
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Dependency Injection in .NET by Mark Seemann(9340)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8299)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7082)
Microservices with Go by Alexander Shuiskov(6849)
Practical Design Patterns for Java Developers by Miroslav Wengner(6769)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6708)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6416)
Angular Projects - Third Edition by Aristeidis Bampakos(6114)
The Art of Crafting User Stories by The Art of Crafting User Stories(5644)
NetSuite for Consultants - Second Edition by Peter Ries(5576)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5381)
Kotlin in Action by Dmitry Jemerov(5065)
