Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com


Pass-by-Reference vs Pass-by-Value



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə7/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   2   3   4   5   6   7   8   9   10   ...   47
Java Interview Guide

Pass-by-Reference vs Pass-by-Value
The manner in which arguments are passed to methods depends on whether
a programming language is pass-by-reference or pass-by-value. For example,
imagine flying a kite and passing control of the kite to a friend. Pass-by-
reference would be akin to handing the string over to them, but pass-by-value
would require tying a duplicate string to the same kite and passing that instead.
In both scenarios your friend could exert control over the kite, but with pass-by-
reference your friend has the freedom to change what kite the string points to (or
to let it go completely).
Every argument in Java is pass-by-value. With a primitive argument, the value
of the primitive is copied and passed to the method. With an object argument,
a pointer to the object is copied and passed to the method.


The Final Keyword
The final keyword determines whether an object reference can be changed
after being assigned. In the kite example, using the final keyword would
be akin to supergluing the string to your hand. The final keyword is often
used in conjunction with the static keyword to create constants. However, it’s
important to note that the final keyword does not prevent an object’s internal
state from changing. The final keyword can also be applied at the class level
to prevent subclasses from extending a class, or at the method level to prevent
subclasses from overriding a method.


The Static Keyword
The static keyword determines whether a property belongs to a class or to
an object instance. For example, imagine holding a blueprint for a house.
Properties that can be unique between houses, such as the number of windows
or doors, are examples of instance properties. Properties that can be written
on the blueprint, such as the number of houses built, are examples of static
properties. An object can access static properties on a class, but a class cannot
access instance properties unless it holds a reference to an object.



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   10   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin